Example usage for org.apache.ibatis.datasource.pooled PooledDataSource PooledDataSource

List of usage examples for org.apache.ibatis.datasource.pooled PooledDataSource PooledDataSource

Introduction

In this page you can find the example usage for org.apache.ibatis.datasource.pooled PooledDataSource PooledDataSource.

Prototype

public PooledDataSource(String driver, String url, Properties driverProperties) 

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();
    }//  www .j ava2  s. com

    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;
}

From source file:org.fastcatsearch.db.InternalDBModule.java

License:Open Source License

@Override
protected boolean doLoad() throws ModuleException {
    Properties driverProperties = new Properties();
    driverProperties.setProperty("driver.encoding", "UTF-8");
    //******* driverProperties *****
    //poolMaximumActiveConnections
    //poolMaximumIdleConnections
    //poolMaximumCheckoutTime
    //poolTimeToWait
    //poolPingQuery
    //poolPingEnabled
    //poolPingConnectionsNotUsedFor
    //////////////////////////////////

    PooledDataSource dataSource = new PooledDataSource(derbyEmbeddedDriver, dbPath, driverProperties);
    org.apache.ibatis.mapping.Environment environment = new org.apache.ibatis.mapping.Environment("ID",
            new JdbcTransactionFactory(), dataSource);
    Configuration configuration = new Configuration(environment);

    if (mapperFileList != null) {
        for (URL mapperFile : mapperFileList) {
            addSqlMappings(configuration, mapperFile);
        }//w  ww  .j a  va 2 s . co m
    }

    sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration);
    logger.info("DBModule[{}] Loaded! with {}", dbPath, mapperFileList);
    return true;
}