Example usage for org.apache.ibatis.datasource.unpooled UnpooledDataSource setAutoCommit

List of usage examples for org.apache.ibatis.datasource.unpooled UnpooledDataSource setAutoCommit

Introduction

In this page you can find the example usage for org.apache.ibatis.datasource.unpooled UnpooledDataSource setAutoCommit.

Prototype

public void setAutoCommit(Boolean autoCommit) 

Source Link

Usage

From source file:org.fastcatsearch.analytics.db.CommonDBHandler.java

License:Open Source License

public boolean load() {

    if (driverProperties == null) {
        driverProperties = new Properties();
    }/*  w w  w .j  a v  a2s . c o  m*/

    String dbType = settings.getString("type");
    driverProperties.setProperty("user", settings.getString("user"));
    driverProperties.setProperty("password", settings.getString("password"));
    driverProperties.setProperty("driver.encoding", "UTF-8");

    boolean isAutoCommit = settings.getBoolean("autocommit", true);
    boolean usePooling = settings.getBoolean("usePooling", true);

    DataSource dataSource = null;
    if (usePooling) {
        PooledDataSource pooledDataSource = new PooledDataSource(settings.getString("driver"),
                settings.getString("url"), driverProperties);
        boolean poolPingEnabled = settings.getBoolean("poolPingEnabled", true);
        String poolPingQuery = settings.getString("poolPingQuery");
        int poolPingConnectionsNotUsedFor = settings.getInt("poolPingConnectionsNotUsedFor", -1);
        int poolTimeToWait = settings.getInt("poolTimeToWait", -1);
        int poolMaximumActiveConnections = settings.getInt("poolMaximumActiveConnections", -1);
        int poolMaximumIdleConnections = settings.getInt("poolMaximumIdleConnections", -1);

        pooledDataSource.setPoolPingEnabled(poolPingEnabled);

        if (poolPingQuery != null) {
            pooledDataSource.setPoolPingQuery(poolPingQuery);
        }
        if (poolPingConnectionsNotUsedFor != -1) {
            pooledDataSource.setPoolPingConnectionsNotUsedFor(poolPingConnectionsNotUsedFor);
        }
        if (poolTimeToWait != -1) {
            pooledDataSource.setPoolTimeToWait(poolTimeToWait);
        }
        if (poolMaximumActiveConnections != -1) {
            pooledDataSource.setPoolMaximumActiveConnections(poolMaximumActiveConnections);
        }
        if (poolMaximumIdleConnections != -1) {
            pooledDataSource.setPoolMaximumIdleConnections(poolMaximumIdleConnections);
        }
        //autocommit
        pooledDataSource.setDefaultAutoCommit(isAutoCommit);

        dataSource = pooledDataSource;

    } else {
        UnpooledDataSource unpooledDataSource = new UnpooledDataSource(settings.getString("driver"),
                settings.getString("url"), driverProperties);
        unpooledDataSource.setAutoCommit(isAutoCommit);
        dataSource = unpooledDataSource;
    }

    org.apache.ibatis.mapping.Environment environment = new org.apache.ibatis.mapping.Environment("ID",
            new JdbcTransactionFactory(), dataSource);
    Configuration configuration = new Configuration(environment);
    if (globalParam != null) {
        configuration.getVariables().putAll(globalParam);
    }

    if (mapperList != null) {
        List<URL> mapperFileList = new ArrayList<URL>();
        for (Class<?> mapperDAO : mapperList) {
            try {
                String mapperFilePath = mapperDAO.getName().replace('.', '/') + "_" + dbType + ".xml";
                URL mapperFile = Resources.getResourceURL(mapperFilePath);
                mapperFileList.add(mapperFile);
            } catch (IOException e) {
                logger.error("error load MapperFile", e);
            }
        }

        for (URL mapperFile : mapperFileList) {
            addSqlMappings(configuration, mapperFile);
        }
    }

    sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration);
    return true;
}