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();
}
}
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();
}
}
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);
}
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();
}
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");
}
Aggregations