Example usage for org.apache.ibatis.transaction.managed ManagedTransactionFactory ManagedTransactionFactory

List of usage examples for org.apache.ibatis.transaction.managed ManagedTransactionFactory ManagedTransactionFactory

Introduction

In this page you can find the example usage for org.apache.ibatis.transaction.managed ManagedTransactionFactory ManagedTransactionFactory.

Prototype

ManagedTransactionFactory

Source Link

Usage

From source file:com.gf.components.mybatis.AbstractTest.java

License:Apache License

@Before
public void init() throws Exception {

    DataSourceManagerImpl manager = new DataSourceManagerImpl();
    manager.setUser("sa");
    manager.setPassword("");
    manager.setDriverClass("org.h2.Driver");
    manager.setUrl("jdbc:h2:" + tmpDirPath + "/db-" + sessionId);
    manager.setPoolSize(10);/*w  ww. ja  va 2  s  .  c  o m*/
    manager.init();

    dataSource = manager.getDataSource();

    TransactionFactory transactionFactory = new ManagedTransactionFactory();
    Environment environment = new Environment("default-env", transactionFactory, dataSource);
    Configuration configuration = new Configuration(environment);
    configuration.addMappers("com.gf", AbstractMapper.class);
    sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration);

}

From source file:com.raise.orgs.impl.cfg.ProcessEngineConfigurationImpl.java

License:Apache License

protected void initTransactionFactory() {
    if (transactionFactory == null) {
        if (transactionsExternallyManaged) {
            transactionFactory = new ManagedTransactionFactory();
        } else {/*w  ww.java  2 s  .  c o  m*/
            transactionFactory = new JdbcTransactionFactory();
        }
    }
}

From source file:org.activiti.engine.AbstractEngineConfiguration.java

License:Apache License

public void initTransactionFactory() {
    if (transactionFactory == null) {
        if (transactionsExternallyManaged) {
            transactionFactory = new ManagedTransactionFactory();
            Properties properties = new Properties();
            properties.put("closeConnection", "false");
            this.transactionFactory.setProperties(properties);
        } else {/*from w w  w.j  a va 2s.com*/
            transactionFactory = new JdbcTransactionFactory();
        }
    }
}

From source file:org.activiti.engine.impl.persistence.db.DbSqlSessionFactory.java

License:Apache License

public void configurationCompleted(ProcessEngineConfiguration processEngineConfiguration) {
    this.databaseName = processEngineConfiguration.getDatabaseName();
    this.idGenerator = processEngineConfiguration.getIdGenerator();
    this.statementMappings = databaseSpecificStatements.get(processEngineConfiguration.getDatabaseName());

    DataSource dataSource = processEngineConfiguration.getDataSource();
    if (dataSource == null) {

        String jdbcDriver = processEngineConfiguration.getJdbcDriver();
        String jdbcUrl = processEngineConfiguration.getJdbcUrl();
        String jdbcUsername = processEngineConfiguration.getJdbcUsername();
        String jdbcPassword = processEngineConfiguration.getJdbcPassword();

        if ((jdbcDriver == null) || (jdbcUrl == null) || (jdbcUsername == null)) {
            throw new ActivitiException(
                    "DataSource or JDBC properties have to be specified in a process engine configuration");
        }//from ww w. j av a  2s  . c  om

        dataSource = new PooledDataSource(Thread.currentThread().getContextClassLoader(), jdbcDriver, jdbcUrl,
                jdbcUsername, jdbcPassword);
    }

    TransactionFactory transactionFactory = null;
    if (processEngineConfiguration.isLocalTransactions()) {
        transactionFactory = new JdbcTransactionFactory();
    } else {
        transactionFactory = new ManagedTransactionFactory();
    }

    this.sqlSessionFactory = createSessionFactory(dataSource, transactionFactory);
}

From source file:org.activiti.engine.impl.transaction.TransactionContextAwareTransactionFactory.java

License:Apache License

public TransactionContextAwareTransactionFactory(Class<T> transactionContextClass) {
    this.transactionContextClass = transactionContextClass;

    this.jdbcTransactionFactory = new JdbcTransactionFactory();

    this.managedTransactionFactory = new ManagedTransactionFactory();
    Properties properties = new Properties();
    properties.put("closeConnection", "false");
    this.managedTransactionFactory.setProperties(properties);
}

From source file:org.activiti.impl.persistence.IbatisPersistenceSessionFactory.java

License:Apache License

public IbatisPersistenceSessionFactory(VariableTypes variableTypes, IdGenerator idGenerator,
        String databaseName, DataSource dataSource, boolean localTransactions) {
    this.variableTypes = variableTypes;
    this.idGenerator = idGenerator;
    this.databaseName = databaseName;
    sqlSessionFactory = createSessionFactory(dataSource,
            localTransactions ? new JdbcTransactionFactory() : new ManagedTransactionFactory());
}

From source file:org.activiti.MultiTenantSqlSessionFactory.java

License:Apache License

private TransactionFactory getTransactionFactoryFromEnvironment(Environment environment) {
    if (environment == null || environment.getTransactionFactory() == null) {
        return new ManagedTransactionFactory();
    }//from www. ja v a  2 s  .  c  o m
    return environment.getTransactionFactory();
}

From source file:org.flowable.engine.impl.cfg.ProcessEngineConfigurationImpl.java

License:Apache License

public void initTransactionFactory() {
    if (transactionFactory == null) {
        if (transactionsExternallyManaged) {
            transactionFactory = new ManagedTransactionFactory();
        } else {// www  . j av  a  2s  .  c om
            transactionFactory = new ContextAwareJdbcTransactionFactory(); // Special for process engine! ContextAware vs regular JdbcTransactionFactory
        }
    }
}

From source file:org.foxbpm.engine.impl.mybatis.MyBatisSqlSessionFactory.java

License:Apache License

public void init(ProcessEngineConfigurationImpl processEngineConfig) throws SQLException {

    DataSource dataSource = processEngineConfig.getDataSource();
    if (dataSource == null) {
        throw ExceptionUtil.getException("00002003");
    }/* w w  w.  j  a  v a 2 s.  c  o m*/
    Connection connection = null;
    String databaseType = null;
    try {
        connection = dataSource.getConnection();
        DatabaseMetaData databaseMetaData = connection.getMetaData();
        String databaseProductName = databaseMetaData.getDatabaseProductName();
        log.debug("database product name: '{}'", databaseProductName);
        databaseType = databaseTypeMappings.getProperty(databaseProductName);
        if (databaseType == null) {
            throw new FoxBPMException(
                    "couldn't deduct database type from database product name '" + databaseProductName + "'");
        }

    } catch (SQLException e) {
        log.error("Exception while initializing Database connection", e);
    } finally {
        try {
            if (connection != null) {
                connection.close();
            }
        } catch (SQLException e) {
            log.error("Exception while closing the Database connection", e);
            throw e;
        }
    }
    if (sqlSessionFactory == null) {
        InputStream inputStream = null;
        try {
            inputStream = Resources.getResourceAsStream("mybatis/mapping/mappings.xml");
            TransactionFactory transactionFactory = new ManagedTransactionFactory();
            Environment environment = new Environment("default", transactionFactory, dataSource);
            Reader reader = new InputStreamReader(inputStream);
            Properties properties = new Properties();
            if (databaseType != null) {
                properties.put("limitBefore", databaseSpecificLimitBeforeStatements.get(databaseType));
                properties.put("limitAfter", databaseSpecificLimitAfterStatements.get(databaseType));
                properties.put("limitBetween", databaseSpecificLimitBetweenStatements.get(databaseType));
                properties.put("limitOuterJoinBetween",
                        databaseOuterJoinLimitBetweenStatements.get(databaseType));
                properties.put("orderBy", databaseSpecificOrderByStatements.get(databaseType));
                properties.put("prefix", processEngineConfig.getPrefix());
            }
            XMLConfigBuilder parser = new XMLConfigBuilder(reader, "", properties);
            Configuration configuration = parser.getConfiguration();
            configuration.setEnvironment(environment);
            configuration = parser.parse();

            Iterator<FoxbpmMapperConfig> mapperIterator = processEngineConfig.getCustomMapperConfig()
                    .iterator();
            while (mapperIterator.hasNext()) {
                FoxbpmMapperConfig tmpMapper = mapperIterator.next();
                log.debug("?mapperConifg{};", tmpMapper.getClass());
                List<String> mapperPaths = tmpMapper.getMapperConfig();
                if (mapperPaths != null) {
                    for (String mapPath : mapperPaths) {
                        InputStream input = Resources.getResourceAsStream(mapPath);
                        if (input != null) {
                            XMLMapperBuilder xmlMapperBuilder = new XMLMapperBuilder(input, configuration,
                                    mapPath, configuration.getSqlFragments());
                            xmlMapperBuilder.parse();
                            log.debug("?mapper{};", mapPath);
                        } else {
                            log.warn("mapper:" + mapPath + "?;");
                        }
                    }
                }
            }
            sqlSessionFactory = new DefaultSqlSessionFactory(configuration);
        } catch (Exception e) {
            throw new RuntimeException("Error while building ibatis SqlSessionFactory: " + e.getMessage(), e);
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

From source file:org.mybatis.spring.MyBatisSpringTest.java

License:Apache License

@Test
public void testWithJtaTxManagerAndNonSpringTxManager() throws java.sql.SQLException {
    Environment original = sqlSessionFactory.getConfiguration().getEnvironment();

    MockDataSource mockDataSource = new MockDataSource();
    mockDataSource.setupConnection(createMockConnection());

    Environment nonSpring = new Environment("non-spring", new ManagedTransactionFactory(), mockDataSource);
    sqlSessionFactory.getConfiguration().setEnvironment(nonSpring);

    JtaTransactionManager jtaManager = new JtaTransactionManager(new MockUserTransaction());

    DefaultTransactionDefinition txDef = new DefaultTransactionDefinition();
    txDef.setPropagationBehaviorName("PROPAGATION_REQUIRED");

    TransactionStatus status = jtaManager.getTransaction(txDef);

    try {// w ww . ja  v a  2  s .  c  o m
        session = SqlSessionUtils.getSqlSession(sqlSessionFactory);
        session.getMapper(TestMapper.class).findTest();
        // Spring is not managing SqlSession, so commit is needed
        session.commit(true);
        SqlSessionUtils.closeSqlSession(session, sqlSessionFactory);

        jtaManager.commit(status);

        // assume a real JTA tx would enlist and commit the JDBC connection
        assertNoCommitJdbc();
        assertCommitSession();

        MockConnection mockConnection = (MockConnection) mockDataSource.getConnection();
        assertEquals("should call commit on Connection", 0, mockConnection.getNumberCommits());
        assertEquals("should not call rollback on Connection", 0, mockConnection.getNumberRollbacks());

        assertEquals("should not call DataSource.getConnection()", 0, dataSource.getConnectionCount());

    } finally {
        SqlSessionUtils.closeSqlSession(session, sqlSessionFactory);

        sqlSessionFactory.getConfiguration().setEnvironment(original);

        // null the connection since it was not used
        // this avoids failing in validateConnectionClosed()
        connection = null;
    }
}