Java Code Examples for org.apache.ibatis.jdbc.ScriptRunner#setStopOnError()

The following examples show how to use org.apache.ibatis.jdbc.ScriptRunner#setStopOnError() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SchemaInitSystemStrategy.java    From zuihou-admin-cloud with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("AlibabaRemoveCommentedCode")
    public ScriptRunner getScriptRunner() {
        try {
            Connection connection = this.dataSource.getConnection();
            ScriptRunner runner = new ScriptRunner(connection);
            runner.setAutoCommit(false);
            //遇见错误是否停止
            runner.setStopOnError(true);
            /*
             * 按照那种方式执行 方式一:true则获取整个脚本并执行; 方式二:false则按照自定义的分隔符每行执行;
             */
            runner.setSendFullScript(true);
            // 设置是否输出日志,null不输出日志,不设置自动将日志输出到控制台
            // runner.setLogWriter(null);

            Resources.setCharset(Charset.forName("UTF8"));

//            设置分隔符 runner.setDelimiter(";");
            runner.setFullLineDelimiter(false);
            return runner;
        } catch (Exception ex) {
            throw new BizException(-1, "获取连接失败");
        }
    }
 
Example 2
Source File: SchemaInitSystemStrategy.java    From zuihou-admin-boot with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("AlibabaRemoveCommentedCode")
    public ScriptRunner getScriptRunner() {
        try {
            Connection connection = this.dataSource.getConnection();
            ScriptRunner runner = new ScriptRunner(connection);
            runner.setAutoCommit(false);
            //遇见错误是否停止
            runner.setStopOnError(true);
            /*
             * 按照那种方式执行 方式一:true则获取整个脚本并执行; 方式二:false则按照自定义的分隔符每行执行;
             */
            runner.setSendFullScript(true);
            // 设置是否输出日志,null不输出日志,不设置自动将日志输出到控制台
            // runner.setLogWriter(null);

            Resources.setCharset(Charset.forName("UTF8"));

//            设置分隔符 runner.setDelimiter(";");
            runner.setFullLineDelimiter(false);
            return runner;
        } catch (Exception ex) {
            throw new BizException(-1, "获取连接失败");
        }
    }
 
Example 3
Source File: DataSourceInitializerImpl.java    From studio with GNU General Public License v3.0 6 votes vote down vote up
private void createSchema(Connection conn) throws IOException {
    String createSchemaScriptPath = getCreateSchemaScriptPath();
    // Database does not exist
    logger.info("Database schema does not exists.");
    logger.info("Creating database schema from script " + createSchemaScriptPath);
    ScriptRunner sr = new ScriptRunner(conn);

    sr.setDelimiter(delimiter);
    sr.setStopOnError(true);
    sr.setLogWriter(null);
    InputStream is = getClass().getClassLoader().getResourceAsStream(createSchemaScriptPath);
    String scriptContent = IOUtils.toString(is);
    Reader reader = new StringReader(
            scriptContent.replaceAll(CRAFTER_SCHEMA_NAME, studioConfiguration.getProperty(DB_SCHEMA)));
    try {
        sr.runScript(reader);
    } catch (RuntimeSqlException e) {
        logger.error("Error while running create DB script", e);
    }
}
 
Example 4
Source File: DataSourceInitializerImpl.java    From studio with GNU General Public License v3.0 6 votes vote down vote up
private void createDatabaseTables(Connection conn, Statement statement) throws SQLException, IOException {
    String createDbScriptPath = getCreateDBScriptPath();
    // Database does not exist
    logger.info("Database tables do not exist.");
    logger.info("Creating database tables from script " + createDbScriptPath);
    ScriptRunner sr = new ScriptRunner(conn);

    sr.setDelimiter(delimiter);
    sr.setStopOnError(true);
    sr.setLogWriter(null);
    InputStream is = getClass().getClassLoader().getResourceAsStream(createDbScriptPath);
    String scriptContent = IOUtils.toString(is);
    Reader reader = new StringReader(
            scriptContent.replaceAll(CRAFTER_SCHEMA_NAME, studioConfiguration.getProperty(DB_SCHEMA)));
    try {
        sr.runScript(reader);

        if (isRandomAdminPasswordEnabled()) {
            setRandomAdminPassword(conn, statement);
        }

        integrityValidator.store(conn);
    } catch (RuntimeSqlException e) {
        logger.error("Error while running create DB script", e);
    }
}
 
Example 5
Source File: BartDBMSUtility.java    From BART with MIT License 6 votes vote down vote up
public static void deleteDB(AccessConfiguration accessConfiguration) {
        String script = "DROP DATABASE " + accessConfiguration.getDatabaseName() + ";\n";
        AccessConfiguration tempAccessConfiguration = getTempAccessConfiguration(accessConfiguration);
        Connection connection = null;
        try {
            if (logger.isDebugEnabled()) logger.debug("Deleting db: " + accessConfiguration);
//            connection = new SimpleDbConnectionFactory().getConnection(tempAccessConfiguration);
            connection = simpleDataSourceDB.getConnection(tempAccessConfiguration);
//            connection = QueryManager.getConnection(tempAccessConfiguration);
            ScriptRunner scriptRunner = getScriptRunner(connection);
            scriptRunner.setAutoCommit(true);
            scriptRunner.setStopOnError(true);
            scriptRunner.runScript(new StringReader(script));
        } catch (Exception daoe) {
            throw new DBMSException("Unable to drop database " + accessConfiguration.getDatabaseName() + ".\n" + tempAccessConfiguration + "\n" + daoe.getLocalizedMessage());
        } finally {
            QueryManager.closeConnection(connection);
        }
    }
 
Example 6
Source File: BartDBMSUtility.java    From BART with MIT License 6 votes vote down vote up
public static void createDB(AccessConfiguration accessConfiguration) {
        AccessConfiguration tempAccessConfiguration = getTempAccessConfiguration(accessConfiguration);
        Connection connection = null;
        try {
            if (logger.isDebugEnabled()) logger.debug("Creating db: " + accessConfiguration);
//            connection = new SimpleDbConnectionFactory().getConnection(tempAccessConfiguration);
            connection = simpleDataSourceDB.getConnection(tempAccessConfiguration);
//            connection = QueryManager.getConnection(tempAccessConfiguration);
            String createQuery = "create database " + accessConfiguration.getDatabaseName() + ";";
            ScriptRunner scriptRunner = getScriptRunner(connection);
            scriptRunner.setAutoCommit(true);
            scriptRunner.setStopOnError(true);
            scriptRunner.runScript(new StringReader(createQuery));
        } catch (Exception daoe) {
            throw new DBMSException("Unable to create new database " + accessConfiguration.getDatabaseName() + ".\n" + tempAccessConfiguration + "\n" + daoe.getLocalizedMessage());
        } finally {
            QueryManager.closeConnection(connection);
        }
    }
 
Example 7
Source File: MyBatisPluginTest.java    From HotswapAgent with GNU General Public License v2.0 5 votes vote down vote up
private static void runScript(DataSource ds, String resource) throws IOException, SQLException {
    try (Connection connection = ds.getConnection()) {
        ScriptRunner runner = new ScriptRunner(connection);
        runner.setAutoCommit(true);
        runner.setStopOnError(false);
        runner.setLogWriter(null);
        runner.setErrorLogWriter(null);
        runScript(runner, resource);
    }
}
 
Example 8
Source File: BaseDataTest.java    From mybatis with Apache License 2.0 5 votes vote down vote up
public static void runScript(DataSource ds, String resource) throws IOException, SQLException {
  Connection connection = ds.getConnection();
  try {
    ScriptRunner runner = new ScriptRunner(connection);
    runner.setAutoCommit(true);
    runner.setStopOnError(false);
    runner.setLogWriter(null);
    runner.setErrorLogWriter(null);
    runScript(runner, resource);
  } finally {
    connection.close();
  }
}
 
Example 9
Source File: MultipleResultSetTest.java    From mybatis with Apache License 2.0 5 votes vote down vote up
private static void runReaderScript(Connection conn, SqlSession session, Reader reader) throws Exception {
  ScriptRunner runner = new ScriptRunner(conn);
  runner.setLogWriter(null);
  runner.setSendFullScript(true);
  runner.setAutoCommit(true);
  runner.setStopOnError(false);
  runner.runScript(reader);
}
 
Example 10
Source File: BaseDataTest.java    From mybaties with Apache License 2.0 5 votes vote down vote up
public static void runScript(DataSource ds, String resource) throws IOException, SQLException {
  Connection connection = ds.getConnection();
  try {
    ScriptRunner runner = new ScriptRunner(connection);
    runner.setAutoCommit(true);
    runner.setStopOnError(false);
    runner.setLogWriter(null);
    runner.setErrorLogWriter(null);
    runScript(runner, resource);
  } finally {
    connection.close();
  }
}
 
Example 11
Source File: MultipleResultSetTest.java    From mybaties with Apache License 2.0 5 votes vote down vote up
private static void runReaderScript(Connection conn, SqlSession session, Reader reader) throws Exception {
  ScriptRunner runner = new ScriptRunner(conn);
  runner.setLogWriter(null);
  runner.setSendFullScript(true);
  runner.setAutoCommit(true);
  runner.setStopOnError(false);
  runner.runScript(reader);
}
 
Example 12
Source File: MyBatisTestHelper.java    From gossip with MIT License 5 votes vote down vote up
@SneakyThrows
private static void loadInitializeData(Injector injector, Reader reader) {
    DataSource dataSource = injector.getInstance(DataSource.class);

    try (final Connection connection = dataSource.getConnection()) {
        ScriptRunner runner = new ScriptRunner(connection);
        runner.setAutoCommit(true);
        runner.setStopOnError(true);
        runner.runScript(reader);
    } // Auto close connection
}
 
Example 13
Source File: GossipDataSourceProvider.java    From gossip with MIT License 5 votes vote down vote up
private void executeSQL(Connection connection, Reader reader) throws SQLException {
    ScriptRunner runner = new ScriptRunner(connection);
    runner.setAutoCommit(true);
    runner.setStopOnError(true);

    try {
        runner.runScript(reader);
    } catch (RuntimeSqlException e) {
        throw new SQLException(e); // Force to handle the exception by myself
    }
}
 
Example 14
Source File: BaseDataTest.java    From mybatis-typehandlers-postgis with Do What The F*ck You Want To Public License 5 votes vote down vote up
public static void runScript(DataSource ds, String resource) throws IOException, SQLException {
    Connection connection = ds.getConnection();
    try {
        ScriptRunner runner = new ScriptRunner(connection);
        runner.setAutoCommit(true);
        runner.setStopOnError(false);
        runner.setLogWriter(null);
        runner.setErrorLogWriter(null);
        runScript(runner, resource);
    } finally {
        connection.close();
    }
}
 
Example 15
Source File: DbWriter.java    From taskana with Apache License 2.0 5 votes vote down vote up
private ScriptRunner configScriptRunner(Connection connection) throws SQLException {
  LOGGER.debug(connection.getMetaData().toString());
  ScriptRunner runner = new ScriptRunner(connection);
  runner.setStopOnError(true);
  runner.setLogWriter(this.logWriter);
  runner.setErrorLogWriter(this.errorLogWriter);
  runner.setStopOnError(true);
  runner.setLogWriter(this.logWriter);
  runner.setErrorLogWriter(this.errorLogWriter);
  return runner;
}
 
Example 16
Source File: SampleDataGenerator.java    From taskana with Apache License 2.0 5 votes vote down vote up
public void generateSampleData(String schemaName) {
  final StringWriter outWriter = new StringWriter();
  final PrintWriter logWriter = new PrintWriter(outWriter);

  final StringWriter errorWriter = new StringWriter();
  final PrintWriter errorLogWriter = new PrintWriter(errorWriter);
  try (Connection connection = dataSource.getConnection()) {
    ScriptRunner runner = new ScriptRunner(connection);
    runner.runScript(selectSchemaScript(dbProductName, schemaName));
    runner.setStopOnError(false);
    runner.runScript(
        new BufferedReader(
            new InputStreamReader(
                SampleDataGenerator.class.getResourceAsStream(CLEAR), StandardCharsets.UTF_8)));

    runner.setStopOnError(true);
    runner.setLogWriter(logWriter);
    runner.setErrorLogWriter(errorLogWriter);

    runner.runScript(
        new BufferedReader(
            new InputStreamReader(
                SampleDataGenerator.class.getResourceAsStream(HISTORY_EVENT),
                StandardCharsets.UTF_8)));

  } catch (Exception e) {
    LOGGER.error("caught Exception {}", e, e);
  }

  LOGGER.trace(outWriter.toString());
  if (!errorWriter.toString().trim().isEmpty()) {
    LOGGER.error(errorWriter.toString());
  }
}
 
Example 17
Source File: DbSchemaCreator.java    From taskana with Apache License 2.0 5 votes vote down vote up
private ScriptRunner getScriptRunnerInstance(Connection connection) {
  ScriptRunner runner = new ScriptRunner(connection);
  runner.setStopOnError(true);
  runner.setLogWriter(logWriter);
  runner.setErrorLogWriter(errorLogWriter);
  return runner;
}