Example usage for org.apache.commons.dbcp PoolableConnectionFactory PoolableConnectionFactory

List of usage examples for org.apache.commons.dbcp PoolableConnectionFactory PoolableConnectionFactory

Introduction

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

Prototype

public PoolableConnectionFactory(ConnectionFactory connFactory, ObjectPool pool,
        KeyedObjectPoolFactory stmtPoolFactory, String validationQuery, boolean defaultReadOnly,
        boolean defaultAutoCommit) 

Source Link

Document

Create a new PoolableConnectionFactory.

Usage

From source file:org.eclipse.scada.utils.osgi.jdbc.pool.PoolConnectionAccessor.java

public PoolConnectionAccessor(final DataSourceFactory dataSourceFactory, final Properties paramProperties)
        throws SQLException {
    logger.debug("Creating pool connection accessor : {}", paramProperties);

    // first remove all our properties

    this.connectionPool = new GenericObjectPool<Object>(null);
    this.connectionPool.setMaxActive(getInteger(paramProperties, PREFIX + "maxActive", 8));
    this.connectionPool.setMaxIdle(getInteger(paramProperties, PREFIX + "maxIdle", 8));
    this.connectionPool.setMinIdle(getInteger(paramProperties, PREFIX + "minIdle", 1));
    this.connectionPool.setTestOnBorrow(getBoolean(paramProperties, PREFIX + "testOnBorrow", true));
    this.connectionPool.setTestOnReturn(getBoolean(paramProperties, PREFIX + "testOnReturn", true));

    this.connectionPool.setTimeBetweenEvictionRunsMillis(
            getLong(paramProperties, PREFIX + "timeBetweenEvictionRunsMillis", -1));
    this.connectionPool.setMinEvictableIdleTimeMillis(
            getLong(paramProperties, PREFIX + "minEvictableIdleTimeMillis", 30 * 60 * 1000));
    this.connectionPool.setTestWhileIdle(getBoolean(paramProperties, PREFIX + "testWhileIdle", false));
    this.connectionPool.setSoftMinEvictableIdleTimeMillis(
            getLong(paramProperties, PREFIX + "softMinEvictableIdleTimeMillis", -1));
    this.connectionPool
            .setNumTestsPerEvictionRun(getInteger(paramProperties, PREFIX + "numTestsPerEvictionRun", 3));

    final String connectionInitSql = getString(paramProperties, PREFIX + "connectionInitSql", null);
    final String validationQuery = getString(paramProperties, PREFIX + "validationQuery", null);
    final Integer validationQueryTimeout = getInteger(paramProperties, PREFIX + "validationQueryTimeout", -1);

    this.driverDataSource = dataSourceFactory.createDataSource(paramProperties);

    final ConnectionFactory connectionFactory = new DataSourceConnectionFactory(this.driverDataSource);
    this.poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory, this.connectionPool, null,
            null, false, true);/*  w  w w .j a  v  a 2  s  .  c  o  m*/

    if (connectionInitSql != null) {
        this.poolableConnectionFactory.setConnectionInitSql(Arrays.asList(connectionInitSql));
    }
    if (validationQuery != null) {
        this.poolableConnectionFactory.setValidationQuery(validationQuery);
    }
    if (validationQueryTimeout != null) {
        this.poolableConnectionFactory.setValidationQueryTimeout(validationQueryTimeout);
    }

    this.dataSource = new PoolingDataSource(this.connectionPool);
}

From source file:org.forumj.dbextreme.db.dao.FJDao.java

public static Connection getConnection() throws SQLException, ConfigurationException {
    if (dataSource == null) {
        synchronized (FJDao.class) {
            Configuration config = FJConfiguration.getConfig();
            if (dataSource == null) {
                Integer maxActive = config.getInteger("dbcp.maxActive", 10);
                ObjectPool connectionPool = new GenericObjectPool(null, maxActive);
                String connectURI = config.getString("jdbc.url");
                String userName = config.getString("jdbc.user.name");
                String userPassword = config.getString("jdbc.user.password");
                String driver = config.getString("jdbc.driver");
                String validationQuery = config.getString("dbcp.validationQuery");
                if (driver != null) {
                    try {
                        Class.forName(driver);
                    } catch (ClassNotFoundException e) {
                        e.printStackTrace();
                    }//w ww  . j  a  v  a2  s . co  m
                }
                ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(connectURI, userName,
                        userPassword);
                new PoolableConnectionFactory(connectionFactory, connectionPool, null, validationQuery, false,
                        true);
                dataSource = new PoolingDataSource(connectionPool);
            }
        }
    }
    return dataSource.getConnection();
}

From source file:org.imirsel.plugins.JndiInitializeServlet.java

private DataSource setupDataSource(String jdbc_url, String user, String password) {
    System.out.println("Setting up jdbc datasource: " + jdbc_url + " user: " + user);
    GenericObjectPool connectionPool = new GenericObjectPool(null);
    ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(jdbc_url, user, password);
    PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory,
            connectionPool, null, null, false, false);
    PoolingDataSource dataSource = new PoolingDataSource(connectionPool);
    if (dataSource == null) {
        logger.info("DataSource is null");
    }//from   www .  jav a  2 s.  c  o m
    return dataSource;
}

From source file:org.infoglue.cms.util.workflow.InfoGlueJDBCPropertySet.java

public void setupDriver(String connectURI, String userName, String password) throws Exception {
    String validationQuery = "select 1 from cmInfoGlueProperties";

    logger.info("Setting up driver.");
    Class.forName(this.driverClassName).newInstance();

    logger.info("dbcpWhenExhaustedAction:" + dbcpWhenExhaustedAction);
    logger.info("dbcpMaxActive:" + dbcpMaxActive);
    logger.info("dbcpMaxWait:" + dbcpMaxWait);
    logger.info("dbcpMaxIdle:" + dbcpMaxIdle);
    logger.info("dbcpValidationQuery:" + dbcpValidationQuery);

    int dbcpMaxActiveInt = 200;
    if (dbcpMaxActive != null && !dbcpMaxActive.equals(""))
        dbcpMaxActiveInt = Integer.parseInt(dbcpMaxActive);

    logger.info("dbcpMaxActiveInt:" + dbcpMaxActiveInt);

    connectionPool = new GenericObjectPool(null, dbcpMaxActiveInt);
    connectionPool.setTestOnBorrow(true);
    connectionFactory = new DriverManagerConnectionFactory(connectURI, userName, password);
    poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory, connectionPool, null,
            validationQuery, false, true);

    Class.forName("org.apache.commons.dbcp.PoolingDriver");
    driver = (PoolingDriver) DriverManager.getDriver("jdbc:apache:commons:dbcp:");

    driver.registerPool("infoGlueJDBCPropertySet", connectionPool);
}

From source file:org.integratedmodelling.sql.SQLServer.java

private DataSource setupDataSource(String connectURI) throws ThinklabStorageException {

    PoolingDataSource dataSource = null;
    ClassLoader clsl = null;//w  ww .j  a v  a  2 s.c o m

    try {
        clsl = SQLPlugin.get().swapClassloader();

        ObjectPool connectionPool = new GenericObjectPool(null);
        ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(connectURI, getUser(),
                getPassword());
        @SuppressWarnings("unused")
        PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory,
                connectionPool, null, null, readOnly, autoCommit);
        dataSource = new PoolingDataSource(connectionPool);
    } finally {
        SQLPlugin.get().resetClassLoader(clsl);
    }

    return dataSource;
}

From source file:org.jaffa.persistence.engines.jdbcengine.datasource.DbcpDataSourceConnectionFactory.java

private void createDbcpDataSource() throws SQLException {
    try {/*from   w w w .  ja  va  2  s . co m*/
        synchronized (dataSourceLock) {
            if (c_dataSource == null) {
                // First we load the underlying JDBC driver.
                Class.forName(getDriverClass());

                // Next, we'll need a ObjectPool that serves as the actual pool of connections.
                // We'll use a GenericObjectPool instance
                GenericObjectPool connectionPool = new GenericObjectPool(null);
                if (getMaximumConnections() != null)
                    connectionPool.setMaxActive(getMaximumConnections());
                if (getMinimumConnections() != null)
                    connectionPool.setMaxIdle(getMinimumConnections());
                if (getMaxWait() != null)
                    connectionPool.setMaxWait(getMaxWait());
                if (getTestOnBorrow() != null)
                    connectionPool.setTestOnBorrow(getTestOnBorrow());
                if (getTestOnReturn() != null)
                    connectionPool.setTestOnReturn(getTestOnReturn());

                // Next, we'll create a ConnectionFactory that the pool will use to create Connections.
                // We'll use the DriverManagerConnectionFactory
                ConnectionFactory connectionFactory = null;
                if (m_driverProperties == null || m_driverProperties.isEmpty())
                    connectionFactory = new DriverManagerConnectionFactory(getUrl(), getUser(), getPassword());
                else
                    connectionFactory = new DriverManagerConnectionFactory(getUrl(), getDriverProperties());
                // Now we'll create the PoolableConnectionFactory, which wraps the "real" Connections created by the ConnectionFactory with the classes that implement the pooling functionality.
                KeyedObjectPoolFactory stmtPoolFactory = new StackKeyedObjectPoolFactory();
                String validationQuery = getValidationQuery();
                boolean defaultReadOnly = false;
                boolean defaultAutoCommit = false;
                PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(
                        connectionFactory, connectionPool, stmtPoolFactory, validationQuery, defaultReadOnly,
                        defaultAutoCommit);

                // Finally, we create the PoolingDriver itself, passing in the object pool we created.
                c_dataSource = new PoolingDataSource(connectionPool);

                // This will allow us to access the underlying Connection objects, required by the JdbcSecurityPlugin
                ((PoolingDataSource) c_dataSource).setAccessToUnderlyingConnectionAllowed(true);

                if (log.isDebugEnabled())
                    log.debug("Created the Dbcp DataSource");
            }
        }
    } catch (Exception e) {
        String str = "Error in creating the Dbcp DataSource";
        log.error(str, e);
        throw new SQLException(str);
    }
}

From source file:org.jivesoftware.database.DbConnectionManager.java

public void setupDataSource(String dbString, String username, String password) {
    try {//from w  w w. j  a va  2s  .c  o  m
        Class.forName("com.mysql.jdbc.Driver");
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }

    StringBuilder connectURI = new StringBuilder();
    connectURI.append(dbString);
    if (dbString.contains("?")) {
        connectURI.append("&");
    } else {
        connectURI.append("?");
    }
    connectURI.append("user=");
    connectURI.append(username);
    connectURI.append("&password=");
    connectURI.append(password);

    connectionPool = new GenericObjectPool(null);

    ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(connectURI.toString(), null);

    new PoolableConnectionFactory(connectionFactory, connectionPool, null, null, false, true);

    dataSource = new PoolingDataSource(connectionPool);
}

From source file:org.jongo.jdbc.JDBCConnectionFactory.java

/**
 * Instantiates a new JDBCConnectionFactory if required and creates a connections pool for every database.
 * @return the instance of the singleton.
 *//*from  w w w .jav a 2  s  .  co m*/
private static JDBCConnectionFactory instanceOf() {
    if (instance == null) {
        instance = new JDBCConnectionFactory();
        for (DatabaseConfiguration db : configuration.getDatabases()) {
            l.debug("Registering Connection Pool for " + db.getDatabase());
            GenericObjectPool pool = new GenericObjectPool(null, db.getMaxConnections());
            ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(db.toJdbcURL(),
                    db.getUsername(), db.getPassword());
            PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(
                    connectionFactory, pool, null, null, db.isReadOnly(), true);
            poolableConnectionFactory.hashCode();
            instance.connectionPool.put(db.getDatabase(), pool);
        }
    }
    return instance;
}

From source file:org.kitodo.data.database.persistence.apache.ConnectionManager.java

/**
 *
 * @param connectURI//  w  w w  .jav a  2s.  c o m
 *            - JDBC Connection URI.
 * @param username
 *            - JDBC Connection username.
 * @param password
 *            - JDBC Connection password.
 * @param minIdle
 *            - Minimum number of idel connection in the connection pool.
 * @param maxActive
 *            - Connection Pool Maximum Capacity (Size).
 */
public static DataSource setupDataSource(String connectURI, String username, String password, int minIdle,
        int maxActive) {
    //
    // First, we'll need a ObjectPool that serves as the actual pool of
    // connections.
    //
    // We'll use a GenericObjectPool instance, although any ObjectPool
    // implementation will suffice.
    //
    GenericObjectPool connectionPool = new GenericObjectPool(null);

    connectionPool.setMinIdle(minIdle);
    connectionPool.setMaxActive(maxActive);

    ConnectionManager._pool = connectionPool;
    // we keep it for two reasons
    // #1 We need it for statistics/debugging
    // #2 PoolingDataSource does not have getPool() method, for some
    // obscure, weird reason.

    //
    // Next, we'll create a ConnectionFactory that the pool will use to
    // create Connections.
    // We'll use the DriverManagerConnectionFactory, using the connect
    // string from configuration
    //
    ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(connectURI, username, password);

    //
    // Now we'll create the PoolableConnectionFactory, which wraps the
    // "real" Connections created by
    // the ConnectionFactory with the classes that implement the pooling
    // functionality.
    //
    new PoolableConnectionFactory(connectionFactory, connectionPool, null, null, false, true);

    PoolingDataSource dataSource = new PoolingDataSource(connectionPool);

    return dataSource;
}

From source file:org.methodize.nntprss.feed.db.JdbcChannelDAO.java

private void initializeDatabasePool(Document config) throws Exception {
    Element rootElm = config.getDocumentElement();
    Element dbConfig = (Element) rootElm.getElementsByTagName("db").item(0);
    String connectString = dbConfig.getAttribute("connect");

    if (log.isInfoEnabled()) {
        log.info("Initializing JDBC, connection string = " + connectString);
    }//from   w  w  w.j a  va 2s  .  com

    ObjectPool connectionPool = new GenericObjectPool(null);

    String dbDriver = dbConfig.getAttribute("driverClass");
    if (dbDriver != null && dbDriver.length() > 0) {
        Class.forName(dbDriver);
    } else {
        // Default to HSSQLDB
        Class.forName("org.hsqldb.jdbcDriver");
    }

    String user = dbConfig.getAttribute("user");
    String password = dbConfig.getAttribute("password");
    if (user == null) {
        user = "sa";
    }
    if (password == null) {
        password = "";
    }

    ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(connectString, user, password);

    //
    // Now we'll create the PoolableConnectionFactory, which wraps
    // the "real" Connections created by the ConnectionFactory with
    // the classes that implement the pooling functionality.
    //
    PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory,
            connectionPool, null, null, false, true);

    //
    // Finally, we create the PoolingDriver itself...
    //
    PoolingDriver driver = new PoolingDriver();

    //
    // ...and register our pool with it.
    //
    driver.registerPool("nntprss", connectionPool);
}