Example usage for org.apache.commons.dbcp2 PoolableConnectionFactory setPoolStatements

List of usage examples for org.apache.commons.dbcp2 PoolableConnectionFactory setPoolStatements

Introduction

In this page you can find the example usage for org.apache.commons.dbcp2 PoolableConnectionFactory setPoolStatements.

Prototype

public void setPoolStatements(boolean poolStatements) 

Source Link

Usage

From source file:edumsg.core.PostgresConnection.java

public static void initSource() {
    try {/*from   ww w .ja v a  2  s .c  o m*/
        try {
            Class.forName("org.postgresql.Driver");
        } catch (ClassNotFoundException ex) {
            LOGGER.log(Level.SEVERE, "Error loading Postgres driver: " + ex.getMessage(), ex);
        }
        try {
            readConfFile();
        } catch (Exception e) {
            e.printStackTrace();
        }
        Properties props = new Properties();
        //  System.out.println(DB_USERNAME);
        props.setProperty("user", DB_USERNAME);
        props.setProperty("password", DB_PASSWORD);
        props.setProperty("initialSize", DB_INIT_CONNECTIONS);
        props.setProperty("maxActive", DB_MAX_CONNECTIONS);

        ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(DB_URL, props);
        PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory,
                null);
        poolableConnectionFactory.setPoolStatements(true);

        GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
        poolConfig.setMaxIdle(Integer.parseInt(DB_INIT_CONNECTIONS));
        poolConfig.setMaxTotal(Integer.parseInt(DB_MAX_CONNECTIONS));
        ObjectPool<PoolableConnection> connectionPool = new GenericObjectPool<>(poolableConnectionFactory,
                poolConfig);
        poolableConnectionFactory.setPool(connectionPool);

        Class.forName("org.apache.commons.dbcp2.PoolingDriver");
        dbDriver = (PoolingDriver) DriverManager.getDriver("jdbc:apache:commons:dbcp:");
        dbDriver.registerPool(DB_NAME, connectionPool);

        dataSource = new PoolingDataSource<>(connectionPool);
    } catch (Exception ex) {
        LOGGER.log(Level.SEVERE, "Got error initializing data source: " + ex.getMessage(), ex);
    }
}

From source file:JDBCPool.dbcp.demo.sourcecode.BasicDataSource.java

/**
 * <p>Create (if necessary) and return the internal data source we are using to manage our connections.</p>
 *
 * @throws SQLException if the object pool cannot be created.
 *//*from w  w w.  j  a va2  s  . co  m*/
protected DataSource createDataSource() throws SQLException {
    if (closed) {
        throw new SQLException("Data source is closed");
    }

    // Return the pool if we have already created it
    // This is double-checked locking. This is safe since dataSource is
    // volatile and the code is targeted at Java 5 onwards.
    if (dataSource != null) {
        return dataSource;
    }
    synchronized (this) {
        if (dataSource != null) {
            return dataSource;
        }

        jmxRegister();

        // create factory which returns raw physical connections
        ConnectionFactory driverConnectionFactory = createConnectionFactory();

        // Set up the poolable connection factory
        boolean success = false;
        PoolableConnectionFactory poolableConnectionFactory;
        try {
            poolableConnectionFactory = createPoolableConnectionFactory(driverConnectionFactory);
            poolableConnectionFactory.setPoolStatements(poolPreparedStatements);
            poolableConnectionFactory.setMaxOpenPrepatedStatements(maxOpenPreparedStatements);
            success = true;
        } catch (SQLException se) {
            throw se;
        } catch (RuntimeException rte) {
            throw rte;
        } catch (Exception ex) {
            throw new SQLException("Error creating connection factory", ex);
        }

        if (success) {
            // create a pool for our connections
            createConnectionPool(poolableConnectionFactory);
        }

        // Create the pooling data source to manage connections
        DataSource newDataSource;
        success = false;
        try {
            newDataSource = createDataSourceInstance();
            newDataSource.setLogWriter(logWriter);
            success = true;
        } catch (SQLException se) {
            throw se;
        } catch (RuntimeException rte) {
            throw rte;
        } catch (Exception ex) {
            throw new SQLException("Error creating datasource", ex);
        } finally {
            if (!success) {
                closeConnectionPool();
            }
        }

        // If initialSize > 0, preload the pool
        try {
            for (int i = 0; i < initialSize; i++) {
                connectionPool.addObject(); //?Connection
            }
        } catch (Exception e) {
            closeConnectionPool();
            throw new SQLException("Error preloading the connection pool", e);
        }

        // If timeBetweenEvictionRunsMillis > 0, start the pool's evictor task
        startPoolMaintenance();

        dataSource = newDataSource;
        return dataSource;
    }
}

From source file:JDBCPool.dbcp.demo.sourcecode.BasicDataSource.java

/**
 * Creates the PoolableConnectionFactory and attaches it to the connection pool.  This method only exists
 * so subclasses can replace the default implementation.
 *
 * @param driverConnectionFactory JDBC connection factory
 * @throws SQLException if an error occurs creating the PoolableConnectionFactory
 *//*from w  w  w  .j av  a 2s  .  com*/
protected PoolableConnectionFactory createPoolableConnectionFactory(ConnectionFactory driverConnectionFactory)
        throws SQLException {
    PoolableConnectionFactory connectionFactory = null;
    try {
        connectionFactory = new PoolableConnectionFactory(driverConnectionFactory, registeredJmxName);
        connectionFactory.setValidationQuery(validationQuery);
        connectionFactory.setValidationQueryTimeout(validationQueryTimeout);
        connectionFactory.setConnectionInitSql(connectionInitSqls); //?Connection?SQL?
        connectionFactory.setDefaultReadOnly(defaultReadOnly);
        connectionFactory.setDefaultAutoCommit(defaultAutoCommit);
        connectionFactory.setDefaultTransactionIsolation(defaultTransactionIsolation);
        connectionFactory.setDefaultCatalog(defaultCatalog);
        connectionFactory.setCacheState(cacheState);
        connectionFactory.setPoolStatements(poolPreparedStatements);
        connectionFactory.setMaxOpenPrepatedStatements(maxOpenPreparedStatements);
        connectionFactory.setMaxConnLifetimeMillis(maxConnLifetimeMillis);
        connectionFactory.setRollbackOnReturn(getRollbackOnReturn());
        connectionFactory.setEnableAutoCommitOnReturn(getEnableAutoCommitOnReturn());
        connectionFactory.setDefaultQueryTimeout(getDefaultQueryTimeout());
        validateConnectionFactory(connectionFactory); //??
    } catch (RuntimeException e) {
        throw e;
    } catch (Exception e) {
        throw new SQLException("Cannot create PoolableConnectionFactory (" + e.getMessage() + ")", e);
    }
    return connectionFactory;
}