Search in sources :

Example 1 with TransactionFactory

use of org.apache.ibatis.transaction.TransactionFactory in project mybatis-3 by mybatis.

the class DefaultSqlSessionFactory method openSessionFromConnection.

private SqlSession openSessionFromConnection(ExecutorType execType, Connection connection) {
    try {
        boolean autoCommit;
        try {
            autoCommit = connection.getAutoCommit();
        } catch (SQLException e) {
            // Failover to true, as most poor drivers
            // or databases won't support transactions
            autoCommit = true;
        }
        final Environment environment = configuration.getEnvironment();
        final TransactionFactory transactionFactory = getTransactionFactoryFromEnvironment(environment);
        final Transaction tx = transactionFactory.newTransaction(connection);
        final Executor executor = configuration.newExecutor(tx, execType);
        return new DefaultSqlSession(configuration, executor, autoCommit);
    } catch (Exception e) {
        throw ExceptionFactory.wrapException("Error opening session.  Cause: " + e, e);
    } finally {
        ErrorContext.instance().reset();
    }
}
Also used : Executor(org.apache.ibatis.executor.Executor) Transaction(org.apache.ibatis.transaction.Transaction) SQLException(java.sql.SQLException) TransactionFactory(org.apache.ibatis.transaction.TransactionFactory) ManagedTransactionFactory(org.apache.ibatis.transaction.managed.ManagedTransactionFactory) Environment(org.apache.ibatis.mapping.Environment) SQLException(java.sql.SQLException)

Example 2 with TransactionFactory

use of org.apache.ibatis.transaction.TransactionFactory in project mybatis-3 by mybatis.

the class DefaultSqlSessionFactory method openSessionFromDataSource.

private SqlSession openSessionFromDataSource(ExecutorType execType, TransactionIsolationLevel level, boolean autoCommit) {
    Transaction tx = null;
    try {
        final Environment environment = configuration.getEnvironment();
        final TransactionFactory transactionFactory = getTransactionFactoryFromEnvironment(environment);
        tx = transactionFactory.newTransaction(environment.getDataSource(), level, autoCommit);
        final Executor executor = configuration.newExecutor(tx, execType);
        return new DefaultSqlSession(configuration, executor, autoCommit);
    } catch (Exception e) {
        // may have fetched a connection so lets call close()
        closeTransaction(tx);
        throw ExceptionFactory.wrapException("Error opening session.  Cause: " + e, e);
    } finally {
        ErrorContext.instance().reset();
    }
}
Also used : Executor(org.apache.ibatis.executor.Executor) Transaction(org.apache.ibatis.transaction.Transaction) TransactionFactory(org.apache.ibatis.transaction.TransactionFactory) ManagedTransactionFactory(org.apache.ibatis.transaction.managed.ManagedTransactionFactory) Environment(org.apache.ibatis.mapping.Environment) SQLException(java.sql.SQLException)

Example 3 with TransactionFactory

use of org.apache.ibatis.transaction.TransactionFactory in project mybatis-3 by mybatis.

the class ManagedTransactionFactoryTest method shouldEnsureThatCallsToManagedTransactionAPIDoNotForwardToManagedConnectionsAndDoesNotCloseConnection.

@Test
void shouldEnsureThatCallsToManagedTransactionAPIDoNotForwardToManagedConnectionsAndDoesNotCloseConnection() throws Exception {
    TransactionFactory tf = new ManagedTransactionFactory();
    Properties props = new Properties();
    props.setProperty("closeConnection", "false");
    tf.setProperties(props);
    Transaction tx = tf.newTransaction(conn);
    assertEquals(conn, tx.getConnection());
    tx.commit();
    tx.rollback();
    tx.close();
    verifyNoMoreInteractions(conn);
}
Also used : Transaction(org.apache.ibatis.transaction.Transaction) TransactionFactory(org.apache.ibatis.transaction.TransactionFactory) Properties(java.util.Properties) Test(org.junit.jupiter.api.Test) BaseDataTest(org.apache.ibatis.BaseDataTest)

Example 4 with TransactionFactory

use of org.apache.ibatis.transaction.TransactionFactory in project mybatis-3 by mybatis.

the class ManagedTransactionFactoryTest method shouldEnsureThatCallsToManagedTransactionAPIDoNotForwardToManagedConnections.

@Test
void shouldEnsureThatCallsToManagedTransactionAPIDoNotForwardToManagedConnections() throws Exception {
    TransactionFactory tf = new ManagedTransactionFactory();
    tf.setProperties(new Properties());
    Transaction tx = tf.newTransaction(conn);
    assertEquals(conn, tx.getConnection());
    tx.commit();
    tx.rollback();
    tx.close();
    verify(conn).close();
}
Also used : Transaction(org.apache.ibatis.transaction.Transaction) TransactionFactory(org.apache.ibatis.transaction.TransactionFactory) Properties(java.util.Properties) Test(org.junit.jupiter.api.Test) BaseDataTest(org.apache.ibatis.BaseDataTest)

Example 5 with TransactionFactory

use of org.apache.ibatis.transaction.TransactionFactory in project mybatis-3 by mybatis.

the class BlobInputStreamTypeHandlerTest method setupSqlSessionFactory.

@BeforeAll
static void setupSqlSessionFactory() throws Exception {
    DataSource dataSource = BaseDataTest.createUnpooledDataSource("org/apache/ibatis/type/jdbc.properties");
    TransactionFactory transactionFactory = new JdbcTransactionFactory();
    Environment environment = new Environment("Production", transactionFactory, dataSource);
    Configuration configuration = new Configuration(environment);
    configuration.addMapper(Mapper.class);
    sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration);
    BaseDataTest.runScript(sqlSessionFactory.getConfiguration().getEnvironment().getDataSource(), "org/apache/ibatis/type/BlobInputStreamTypeHandlerTest.sql");
}
Also used : Configuration(org.apache.ibatis.session.Configuration) TransactionFactory(org.apache.ibatis.transaction.TransactionFactory) JdbcTransactionFactory(org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory) Environment(org.apache.ibatis.mapping.Environment) SqlSessionFactoryBuilder(org.apache.ibatis.session.SqlSessionFactoryBuilder) JdbcTransactionFactory(org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory) DataSource(javax.sql.DataSource) BeforeAll(org.junit.jupiter.api.BeforeAll)

Aggregations

TransactionFactory (org.apache.ibatis.transaction.TransactionFactory)15 Environment (org.apache.ibatis.mapping.Environment)12 DataSource (javax.sql.DataSource)9 JdbcTransactionFactory (org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory)7 Configuration (org.apache.ibatis.session.Configuration)6 BeforeAll (org.junit.jupiter.api.BeforeAll)6 SqlSessionFactoryBuilder (org.apache.ibatis.session.SqlSessionFactoryBuilder)5 Transaction (org.apache.ibatis.transaction.Transaction)5 Properties (java.util.Properties)3 SQLException (java.sql.SQLException)2 BaseDataTest (org.apache.ibatis.BaseDataTest)2 Executor (org.apache.ibatis.executor.Executor)2 ManagedTransactionFactory (org.apache.ibatis.transaction.managed.ManagedTransactionFactory)2 Test (org.junit.jupiter.api.Test)2 BaseBuilder (org.apache.ibatis.builder.BaseBuilder)1 BuilderException (org.apache.ibatis.builder.BuilderException)1 DataSourceFactory (org.apache.ibatis.datasource.DataSourceFactory)1 ExecutorException (org.apache.ibatis.executor.ExecutorException)1 XNode (org.apache.ibatis.parsing.XNode)1 SqlSessionFactory (org.apache.ibatis.session.SqlSessionFactory)1