{"record":{"id":"f224145728698d17","repo":"mybatis/mybatis-3","slug":"error-executing-cause","errorCode":null,"errorMessage":"Error executing: {}.  Cause: {}","messagePattern":"Error executing: (.+?)\\.  Cause: (.+?)","errorType":"exception","errorClass":"RuntimeSqlException","httpStatus":null,"severity":"error","filePath":"src/main/java/org/apache/ibatis/jdbc/ScriptRunner.java","lineNumber":144,"sourceCode":"  }\n\n  private void executeFullScript(Reader reader) {\n    StringBuilder script = new StringBuilder();\n    try {\n      BufferedReader lineReader = new BufferedReader(reader);\n      String line;\n      while ((line = lineReader.readLine()) != null) {\n        script.append(line);\n        script.append(LINE_SEPARATOR);\n      }\n      String command = script.toString();\n      println(command);\n      executeStatement(command);\n      commitConnection();\n    } catch (Exception e) {\n      String message = \"Error executing: \" + script + \".  Cause: \" + e;\n      printlnError(message);\n      throw new RuntimeSqlException(message, e);\n    }\n  }\n\n  private void executeLineByLine(Reader reader) {\n    StringBuilder command = new StringBuilder();\n    try {\n      BufferedReader lineReader = new BufferedReader(reader);\n      String line;\n      while ((line = lineReader.readLine()) != null) {\n        handleLine(command, line);\n      }\n      commitConnection();\n      checkForMissingLineTerminator(command);\n    } catch (Exception e) {\n      String message = \"Error executing: \" + command + \".  Cause: \" + e;\n      printlnError(message);\n      throw new RuntimeSqlException(message, e);\n    }","sourceCodeStart":126,"sourceCodeEnd":162,"githubUrl":"https://github.com/mybatis/mybatis-3/blob/008069adb1b089579b5dcba87ee591908b263274/src/main/java/org/apache/ibatis/jdbc/ScriptRunner.java#L126-L162","documentation":"ScriptRunner.runScript reading the whole script as one statement (the non-line-by-line path) hit an exception — SQL failure, connection error, or missing terminator check — and wraps it as RuntimeSqlException embedding the entire accumulated script text and the cause. The message doubles as the error log line via printlnError.","triggerScenarios":"ScriptRunner.setSendFullScript(true) (or the single-command path) executing a script whose single statement fails: syntax error, missing permissions, unknown table, or a JDBC driver refusing multi-statement execution in one call.","commonSituations":"Running schema/seed scripts in tests or migrations; drivers (e.g. Oracle, older SQL Server) that do not accept multiple statements in one execute; statements containing semicolons inside strings when using full-script mode; encoding issues corrupting the script.","solutions":["Set sendFullScript=false (default line-by-line mode) unless the driver explicitly needs one call — most failures here are driver multi-statement limits.","Capture and read the cause (RuntimeSqlException.getCause()) to see the SQLSTATE and offending position.","Split the script at semicolons / enable allowMultipleStatements on drivers like MySQL when full-script mode is required.","Verify the script runs as-is in a SQL client with the same driver and credentials."],"exampleFix":"// before\nScriptRunner runner = new ScriptRunner(conn);\nrunner.setSendFullScript(true);\nrunner.runScript(new FileReader(\"db/all.sql\")); // driver rejects multi-statement\n\n// after\nScriptRunner runner = new ScriptRunner(conn);\nrunner.setSendFullScript(false);\nrunner.runScript(new FileReader(\"db/all.sql\"));","handlingStrategy":"try-catch","validationCode":"// pre-flight: driver must support multi-statement if full-script mode is used\ntry (Statement st = connection.createStatement()) {\n  boolean ok = st.execute(\"select 1; select 2\"); // throws on drivers without multi-statement support\n  // if it throws, use line-by-line mode instead of sendFullScript=true\n}","typeGuard":null,"tryCatchPattern":"try {\n  runner.runScript(reader);\n} catch (RuntimeSqlException e) {\n  log.error(\"Script failed, cause={}\", e.getCause() == null ? \"?\" : e.getCause().toString());\n  // scripts are not idempotent in general: abort, roll back the connection, do not blindly retry\n  connection.rollback();\n  throw e;\n}","preventionTips":["Prefer line-by-line mode (sendFullScript=false) for portability across drivers.","Run migration scripts in a transaction where the DB allows it and roll back on first failure."],"tags":["mybatis","scriptrunner","sql-script","migration","jdbc"],"backgroundTag":null,"analyzedSha":"008069adb1b089579b5dcba87ee591908b263274","analyzedAt":"2026-08-14T13:07:10.264Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}