Java Code Examples for org.apache.ibatis.transaction.TransactionFactory#newTransaction()

The following examples show how to use org.apache.ibatis.transaction.TransactionFactory#newTransaction() . 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: DynamicSqlSessionFactory.java    From hsweb-framework with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("all")
private SqlSession openSessionFromDataSource(ExecutorType execType, TransactionIsolationLevel level, boolean autoCommit) {
    Transaction tx = null;
    try {
        final Environment environment = getConfiguration().getEnvironment();
        final TransactionFactory transactionFactory = getTransactionFactoryFromEnvironment(environment);
        DataSource ds = DataSourceHolder.currentDataSource().getNative();
        if (ds == null) {
            ds = environment.getDataSource();
        }
        tx = transactionFactory.newTransaction(ds, level, autoCommit);
        final Executor executor = getConfiguration().newExecutor(tx, execType);
        return new DefaultSqlSession(getConfiguration(), executor, autoCommit);
    } catch (Exception e) {
        closeTransaction(tx); // may have fetched a connection so lets call close()
        throw ExceptionFactory.wrapException("Error opening session.  Cause: " + e, e);
    } finally {
        ErrorContext.instance().reset();
    }
}
 
Example 2
Source File: DefaultSqlSessionFactory.java    From mybaties with Apache License 2.0 6 votes vote down vote up
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);
    //然后产生一个DefaultSqlSession
    return new DefaultSqlSession(configuration, executor, autoCommit);
  } catch (Exception e) {
    //如果打开事务出错,则关闭它
    closeTransaction(tx); // may have fetched a connection so lets call close()
    throw ExceptionFactory.wrapException("Error opening session.  Cause: " + e, e);
  } finally {
    //最后清空错误上下文
    ErrorContext.instance().reset();
  }
}
 
Example 3
Source File: DefaultSqlSessionFactory.java    From mybatis with Apache License 2.0 6 votes vote down vote up
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);
    //然后产生一个DefaultSqlSession
    return new DefaultSqlSession(configuration, executor, autoCommit);
  } catch (Exception e) {
    //如果打开事务出错,则关闭它
    closeTransaction(tx); // may have fetched a connection so lets call close()
    throw ExceptionFactory.wrapException("Error opening session.  Cause: " + e, e);
  } finally {
    //最后清空错误上下文
    ErrorContext.instance().reset();
  }
}
 
Example 4
Source File: ResultLoader.java    From mybaties with Apache License 2.0 5 votes vote down vote up
private Executor newExecutor() {
  final Environment environment = configuration.getEnvironment();
  if (environment == null) {
    throw new ExecutorException("ResultLoader could not load lazily.  Environment was not configured.");
  }
  final DataSource ds = environment.getDataSource();
  if (ds == null) {
    throw new ExecutorException("ResultLoader could not load lazily.  DataSource was not configured.");
  }
  final TransactionFactory transactionFactory = environment.getTransactionFactory();
  final Transaction tx = transactionFactory.newTransaction(ds, null, false);
  //如果executor已经被关闭了,则创建一个新的SimpleExecutor
  return configuration.newExecutor(tx, ExecutorType.SIMPLE);
}
 
Example 5
Source File: ManagedTransactionFactoryTest.java    From mybaties with Apache License 2.0 5 votes vote down vote up
@Test
public 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();
}
 
Example 6
Source File: ManagedTransactionFactoryTest.java    From mybaties with Apache License 2.0 5 votes vote down vote up
@Test
public 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);
}
 
Example 7
Source File: ResultLoader.java    From mybatis with Apache License 2.0 5 votes vote down vote up
private Executor newExecutor() {
  final Environment environment = configuration.getEnvironment();
  if (environment == null) {
    throw new ExecutorException("ResultLoader could not load lazily.  Environment was not configured.");
  }
  final DataSource ds = environment.getDataSource();
  if (ds == null) {
    throw new ExecutorException("ResultLoader could not load lazily.  DataSource was not configured.");
  }
  final TransactionFactory transactionFactory = environment.getTransactionFactory();
  final Transaction tx = transactionFactory.newTransaction(ds, null, false);
  //如果executor已经被关闭了,则创建一个新的SimpleExecutor
  return configuration.newExecutor(tx, ExecutorType.SIMPLE);
}
 
Example 8
Source File: ManagedTransactionFactoryTest.java    From mybatis with Apache License 2.0 5 votes vote down vote up
@Test
public 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();
}
 
Example 9
Source File: ManagedTransactionFactoryTest.java    From mybatis with Apache License 2.0 5 votes vote down vote up
@Test
public 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);
}