Example usage for org.apache.ibatis.transaction TransactionFactory newTransaction

List of usage examples for org.apache.ibatis.transaction TransactionFactory newTransaction

Introduction

In this page you can find the example usage for org.apache.ibatis.transaction TransactionFactory newTransaction.

Prototype

Transaction newTransaction(DataSource dataSource, TransactionIsolationLevel level, boolean autoCommit);

Source Link

Document

Creates a Transaction out of a datasource.

Usage

From source file:com.raycloud.cobarclient.mybatis.spring.SqlSessionUtils.java

License:Apache License

/**
 * Environment?//from  www  . j  a v  a 2 s.  co m
 *
 * @param environment
 * @param sessionFactory
 * @param executorType
 * @return
 */
private static SqlSession openSession(Environment environment, SqlSessionFactory sessionFactory,
        ExecutorType executorType) {
    Transaction tx = null;
    try {
        final TransactionFactory transactionFactory = environment.getTransactionFactory();
        tx = transactionFactory.newTransaction(environment.getDataSource(), null, false);
        final Executor executor = sessionFactory.getConfiguration().newExecutor(tx, executorType);
        return new DefaultSqlSession(sessionFactory.getConfiguration(), executor, false);
    } 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();
    }
}

From source file:org.activiti.MultiTenantSqlSessionFactory.java

License:Apache License

private SqlSession openSessionFromDataSource(ExecutorType execType, TransactionIsolationLevel level,
        boolean autoCommit) {
    Transaction tx = null;//from w  w  w .j a  va  2  s  .c  om
    try {
        final Environment environment = getConfiguration().getEnvironment();
        final TransactionFactory transactionFactory = getTransactionFactoryFromEnvironment(environment);
        tx = transactionFactory.newTransaction(environment.getDataSource(), level, autoCommit);
        final Executor executor = getConfiguration().newExecutor(tx, execType, autoCommit);
        return new DefaultSqlSession(getConfiguration(), executor);
    } 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();
    }
}

From source file:org.hsweb.web.mybatis.dynamic.DynamicSqlSessionFactory.java

License:Apache License

private SqlSession openSessionFromDataSource(ExecutorType execType, TransactionIsolationLevel level,
        boolean autoCommit) {
    Transaction tx = null;//from  w w w  . j  a  v  a  2s .  c  o m
    try {
        final Environment environment = getConfiguration().getEnvironment();
        final TransactionFactory transactionFactory = getTransactionFactoryFromEnvironment(environment);
        DataSource ds = DataSourceHolder.getActiveSource();
        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();
    }
}

From source file:org.hswebframework.web.dao.mybatis.dynamic.DynamicSqlSessionFactory.java

License:Apache License

@SuppressWarnings("all")
private SqlSession openSessionFromDataSource(ExecutorType execType, TransactionIsolationLevel level,
        boolean autoCommit) {
    Transaction tx = null;//w w w. jav  a2s . c o  m
    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();
    }
}

From source file:org.solmix.datax.mybatis.MybatisDataService.java

License:Open Source License

private Connection getConnection(DataSource dataSource, boolean autoCommit) {
    TransactionFactory factory = sqlSessionFactory.getConfiguration().getEnvironment().getTransactionFactory();
    org.apache.ibatis.transaction.Transaction trans = factory.newTransaction(dataSource,
            TransactionIsolationLevel.NONE, true);
    try {// ww w  .ja  va 2 s.  c  o m
        Connection conn = trans.getConnection();
        conn.setAutoCommit(autoCommit);
        return conn;
    } catch (SQLException e) {
        throw new GetConnectionException("Could not get JDBC Connection", e);
    }
}