Example usage for org.apache.ibatis.jdbc ScriptRunner setAutoCommit

List of usage examples for org.apache.ibatis.jdbc ScriptRunner setAutoCommit

Introduction

In this page you can find the example usage for org.apache.ibatis.jdbc ScriptRunner setAutoCommit.

Prototype

public void setAutoCommit(boolean autoCommit) 

Source Link

Usage

From source file:com.collective.messages.persistence.dao.BaseDataTestCase.java

License:Apache License

public static void runScript(DataSource ds, String resource) throws IOException, SQLException {
    Connection connection = ds.getConnection();
    try {/*from ww w. ja  v a 2s  .c  o  m*/
        ScriptRunner runner = new ScriptRunner(connection);
        runner.setAutoCommit(true);
        runner.setStopOnError(false);
        runner.setLogWriter(null);
        runner.setErrorLogWriter(null);
        runScript(runner, resource);
    } finally {
        connection.close();
    }
}

From source file:com.persinity.common.db.SimpleRelDb.java

License:Apache License

private void configure(final ScriptRunner scriptRunner) {
    // All statements are separated by "/" (put on new line as is the Oracle script style)
    scriptRunner.setDelimiter("/");
    scriptRunner.setFullLineDelimiter(true);

    // Abort on error, as once deviated, subsequent stmts may lead the DB to unpredictable state
    scriptRunner.setStopOnError(true);//  w  w  w  . j a v  a  2  s.  c  o  m

    // Do not auto commit, leave that to the RelDb user
    scriptRunner.setAutoCommit(false);

    // Log script feedback
    // Aways set log writer otherwise it will dump on System.out
    scriptRunner.setLogWriter(new PrintWriter(new Log4jOutputStream(log.getLogger(), Level.DEBUG)));
    scriptRunner.setErrorLogWriter(new PrintWriter(new Log4jOutputStream(log.getLogger(), Level.ERROR)));
}

From source file:myguice.SampleBasicTest.java

License:Apache License

@Before
public void setupMyBatisGuice() throws Exception {

    // bindings//from   ww  w. j  av  a  2  s  . com
    this.injector = createInjector(new MyBatisModule() {

        @Override
        protected void initialize() {
            install(JdbcHelper.HSQLDB_IN_MEMORY_NAMED);

            bindDataSourceProviderType(PooledDataSourceProvider.class);
            bindTransactionFactoryType(JdbcTransactionFactory.class);
            addMapperClass(UserMapper.class);

            bindProperties(binder(), createTestProperties());
            bind(FooService.class).to(FooServiceMapperImpl.class);
            bind(UserDao.class).to(UserDaoImpl.class);
        }

    });

    // prepare the test db
    Environment environment = this.injector.getInstance(SqlSessionFactory.class).getConfiguration()
            .getEnvironment();
    DataSource dataSource = environment.getDataSource();
    ScriptRunner runner = new ScriptRunner(dataSource.getConnection());
    runner.setAutoCommit(true);
    runner.setStopOnError(true);
    runner.runScript(getResourceAsReader("sample/db/database-schema.sql"));
    runner.runScript(getResourceAsReader("sample/db/database-test-data.sql"));
    runner.closeConnection();

    this.fooService = this.injector.getInstance(FooService.class);
}

From source file:net.oletalk.dbexecutor.util.MyBatisUtil.java

private static boolean runFiles(List<String> filenames, Connection conn) throws VersionException {
    boolean success = true;
    if (conn == null) {
        return false; // bail if no connection in the first place
    }//  ww w.  jav  a 2s . com
    for (String sqlfile : filenames) {
        Reader reader;
        try {
            reader = new BufferedReader(new FileReader(sqlfile));
            ScriptRunner runner = new ScriptRunner(conn);
            runner.setStopOnError(true);
            runner.setAutoCommit(false);
            runner.runScript(reader);
            reader.close();
            System.out.println("No errors; committing changes.");
            conn.commit();

        } catch (FileNotFoundException ex) {
            System.err.println("Given file was not found: " + sqlfile + ", skipping!");
        } catch (Exception e) {
            success = false;
            System.out.println("Error encountered; rolling back!");
            try {
                conn.rollback();
            } catch (SQLException sqe) {
                sqe.printStackTrace();
            }
            throw new VersionException("Execution of " + sqlfile + " failed with " + e.getMessage());
        }
    }
    return success;
}

From source file:org.alfresco.repo.transfer.fsr.SchemaBootstrap.java

License:Open Source License

protected void executeSript(Resource resourceScript) {
    BufferedReader reader = null;
    try {//from w ww .  j  a  va 2 s  .  c o m
        // run the creation script with autocommit true and stop on error.
        ScriptRunner runner = new ScriptRunner(dataSource.getConnection());
        runner.setLogWriter(null);
        runner.setAutoCommit(true);
        runner.setStopOnError(true);
        reader = new BufferedReader(new InputStreamReader(resourceScript.getInputStream(), "UTF-8"));
        runner.runScript(reader);
    } catch (SQLException sql) {
        throw new AlfrescoRuntimeException("Creation script " + creationScript + " failed!", sql);
    } catch (IOException io) {
        if (reader != null) {
            try {
                reader.close();
            } catch (Exception e) {
            }
        }
        throw new AlfrescoRuntimeException("Creation script " + creationScript + " could not optain reader!!");
    }
}

From source file:org.mybatis.guice.AbstractGuiceTestExtension.java

License:Apache License

public AbstractGuiceTestExtension() throws SQLException {
    final Contact contact = new Contact();
    contact.setFirstName("John");
    contact.setLastName("Doe");
    contact.setCreated(new CustomType(currentTimeMillis()));
    contact.setAddress(null);/* w  w  w.  java2s .co  m*/

    final Contact contactWithAddress = new Contact();
    contactWithAddress.setFirstName("John");
    contactWithAddress.setLastName("Doe");
    contactWithAddress.setCreated(new CustomType(currentTimeMillis()));

    final Address address = new Address();
    address.setNumber(1234);
    address.setStreet("Elm street");
    contactWithAddress.setAddress(address);

    final Counter counter = new Counter();

    // bindings
    final List<Module> modules = this.createMyBatisModule();
    modules.add(new Module() {
        @Override
        public void configure(Binder binder) {
            bindProperties(binder, createTestProperties());
            binder.bind(Contact.class).toInstance(contact);
            binder.bind(Contact.class).annotatedWith(named("contactWithAddress"))
                    .toInstance(contactWithAddress);
            binder.bind(Counter.class).toInstance(counter);
        }
    });
    this.injector = createInjector(modules);

    // prepare the test db
    final Environment environment = this.injector.getInstance(SqlSessionFactory.class).getConfiguration()
            .getEnvironment();
    final DataSource dataSource = environment.getDataSource();
    final ScriptRunner runner = new ScriptRunner(dataSource.getConnection());
    runner.setAutoCommit(true);
    runner.setStopOnError(true);
    runner.runScript(new StringReader("DROP TABLE IF EXISTS contact;"
            + "CREATE TABLE contact (id int GENERATED BY DEFAULT AS IDENTITY (START WITH 1), "
            + "first_name VARCHAR(20) NOT NULL, " + "last_name VARCHAR(20) NOT NULL, " + "created TIMESTAMP, "
            + "address VARCHAR(100) DEFAULT NULL) ;"));
    runner.closeConnection();
}

From source file:org.mybatis.guice.AbstractGuiceTestRunner.java

License:Apache License

public AbstractGuiceTestRunner(Class<?> klass) throws InitializationError {
    super(klass);

    try {/*from   www. j  a v  a2  s.  c o m*/
        final Contact contact = new Contact();
        contact.setFirstName("John");
        contact.setLastName("Doe");
        contact.setCreated(new CustomType(currentTimeMillis()));
        contact.setAddress(null);

        final Contact contactWithAddress = new Contact();
        contactWithAddress.setFirstName("John");
        contactWithAddress.setLastName("Doe");
        contactWithAddress.setCreated(new CustomType(currentTimeMillis()));

        Address address = new Address();
        address.setNumber(1234);
        address.setStreet("Elm street");
        contactWithAddress.setAddress(address);

        final Counter counter = new Counter();

        // bindings
        List<Module> modules = this.createMyBatisModule();
        modules.add(new Module() {
            public void configure(Binder binder) {
                bindProperties(binder, createTestProperties());
                binder.bind(Contact.class).toInstance(contact);
                binder.bind(Contact.class).annotatedWith(named("contactWithAddress"))
                        .toInstance(contactWithAddress);
                binder.bind(Counter.class).toInstance(counter);
            }
        });
        this.injector = createInjector(modules);

        // prepare the test db
        Environment environment = this.injector.getInstance(SqlSessionFactory.class).getConfiguration()
                .getEnvironment();
        DataSource dataSource = environment.getDataSource();
        ScriptRunner runner = new ScriptRunner(dataSource.getConnection());
        runner.setAutoCommit(true);
        runner.setStopOnError(true);
        runner.runScript(new StringReader("DROP TABLE IF EXISTS contact;"
                + "CREATE TABLE contact (id int GENERATED BY DEFAULT AS IDENTITY (START WITH 1), "
                + "first_name VARCHAR(20) NOT NULL, " + "last_name VARCHAR(20) NOT NULL, "
                + "created TIMESTAMP, " + "address VARCHAR(100) DEFAULT NULL) ;"));
        runner.closeConnection();
    } catch (Exception e) {
        throw new InitializationError(e);
    }
}

From source file:org.mybatis.guice.CleanDatabaseRule.java

License:Apache License

public void evaluate() throws Exception {
    ScriptRunner runner = new ScriptRunner(
            sqlSession.getConfiguration().getEnvironment().getDataSource().getConnection());
    try {//ww w  . j a v a2 s.  com
        runner.setAutoCommit(true);
        runner.setStopOnError(true);
        runner.runScript(new StringReader(
                "DELETE FROM contact; " + "INSERT INTO contact (id, first_name, last_name, created) "
                        + "VALUES (1, '" + contact.getFirstName() + "', '" + contact.getLastName() + "', '"
                        + new Timestamp(contactWithAddress.getCreated().getValue()) + "'); "
                        + "INSERT INTO contact (id, first_name, last_name, created, address) " + "VALUES (2, '"
                        + contactWithAddress.getFirstName() + "', '" + contactWithAddress.getLastName() + "', '"
                        + new Timestamp(contactWithAddress.getCreated().getValue()) + "', '"
                        + addressConverter.convert(contactWithAddress.getAddress()) + "'); "));
        contact.setId(1);
        contactWithAddress.setId(2);
    } finally {
        runner.closeConnection();
    }
}

From source file:org.mybatis.guice.nestedtx.NestedTxTest.java

License:Apache License

@BeforeEach
public void setup() throws Exception {
    injector = Guice.createInjector(new MyBatisModule() {
        @Override//from ww w  .  ja va  2s .c  om
        protected void initialize() {
            bindDataSourceProviderType(PooledDataSourceProvider.class);
            bindTransactionFactoryType(JdbcTransactionFactory.class);

            install(JdbcHelper.HSQLDB_IN_MEMORY_NAMED);

            Properties connectionProps = new Properties();
            connectionProps.setProperty("mybatis.environment.id", "jdbc");
            connectionProps.setProperty("JDBC.username", "sa");
            connectionProps.setProperty("JDBC.password", "");
            connectionProps.setProperty("JDBC.autoCommit", "false");

            Names.bindProperties(binder(), connectionProps);

            addMapperClass(NestedTxMapper.class);
            bind(NestedTxService.class);
        }
    });

    // prepare the test db
    Environment environment = this.injector.getInstance(SqlSessionFactory.class).getConfiguration()
            .getEnvironment();
    DataSource dataSource = environment.getDataSource();
    ScriptRunner runner = new ScriptRunner(dataSource.getConnection());
    runner.setAutoCommit(true);
    runner.setStopOnError(true);
    runner.runScript(getResourceAsReader("org/mybatis/guice/nestedtx/setupdb.sql"));
    runner.closeConnection();

    service = injector.getInstance(NestedTxService.class);
}

From source file:org.mybatis.guice.sample.SampleBasicTest.java

License:Apache License

@BeforeEach
public void setupMyBatisGuice() throws Exception {

    // bindings//  ww w.  j a va  2s  .c  o m
    this.injector = createInjector(new MyBatisModule() {

        @Override
        protected void initialize() {
            install(JdbcHelper.HSQLDB_IN_MEMORY_NAMED);

            bindDataSourceProviderType(PooledDataSourceProvider.class);
            bindTransactionFactoryType(JdbcTransactionFactory.class);
            addMapperClass(UserMapper.class);

            bindProperties(binder(), createTestProperties());
            bind(FooService.class).to(FooServiceMapperImpl.class);
        }

    });

    // prepare the test db
    Environment environment = this.injector.getInstance(SqlSessionFactory.class).getConfiguration()
            .getEnvironment();
    DataSource dataSource = environment.getDataSource();
    ScriptRunner runner = new ScriptRunner(dataSource.getConnection());
    runner.setAutoCommit(true);
    runner.setStopOnError(true);
    runner.runScript(getResourceAsReader("org/mybatis/guice/sample/db/database-schema.sql"));
    runner.runScript(getResourceAsReader("org/mybatis/guice/sample/db/database-test-data.sql"));
    runner.closeConnection();

    this.fooService = this.injector.getInstance(FooService.class);
}