Java tutorial
/* * Copyright (c) 2008-2011 Simon Ritchie. * All rights reserved. * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published * by the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * See the GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this program. If not, see http://www.gnu.org/licenses/>. */ package org.rimudb.pool; import java.sql.*; import java.util.*; import org.apache.commons.dbcp.*; import org.apache.commons.logging.*; import org.apache.commons.pool.*; import org.apache.commons.pool.impl.*; import org.rimudb.*; import org.rimudb.configuration.*; import org.rimudb.exception.*; public class DBCPConnectionManager implements IConnectionManager { private static Log log = LogFactory.getLog(DBCPConnectionManager.class); private final DatabaseConfiguration dbConfig; private GenericObjectPool connectionPool; private DBCPPoolConfiguration poolConfiguration; public DBCPConnectionManager(DatabaseConfiguration dbConfig) { super(); this.dbConfig = dbConfig; poolConfiguration = (DBCPPoolConfiguration) dbConfig.getPoolConfiguration(); } public void connect(String user, String password) throws Exception { // Load the jdbc driver Class.forName(poolConfiguration.getJdbcDriver()); // Create an ObjectPool that serves as the actual pool of connections. connectionPool = new GenericObjectPool(null); connectionPool.setMaxActive(99999); connectionPool.setTimeBetweenEvictionRunsMillis(poolConfiguration.getTimeBetweenEvictionRunsSecs() * 1000L); connectionPool.setMaxActive(poolConfiguration.getMaxActive()); connectionPool.setMinEvictableIdleTimeMillis(poolConfiguration.getMinEvictableIdleTimeSecs() * 1000L); connectionPool.setMinIdle(poolConfiguration.getMinIdle()); connectionPool.setMaxIdle(poolConfiguration.getMaxIdle()); connectionPool.setMaxWait(poolConfiguration.getMaxWaitSecs() * 1000L); // Create a Properties object for the connection factory Properties props = new Properties(); // Pass all the properties from the configuration into the connection factory properties if (poolConfiguration.getProperties() != null) { props.putAll(poolConfiguration.getProperties()); } // Set user and password if (user != null && password != null) { props.setProperty("user", user); props.setProperty("pasword", password); } props.setProperty("poolPreparedStatements", "" + poolConfiguration.getStatementCaching()); // Create a ConnectionFactory that the pool will use to create Connections. ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(poolConfiguration.getJdbcURL(), props); KeyedObjectPoolFactory stmtPoolFactory = null; if (poolConfiguration.getStatementCaching()) { stmtPoolFactory = new GenericKeyedObjectPoolFactory(null); } // 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, stmtPoolFactory, null, false, true); // Create the PoolingDriver itself Class.forName("org.apache.commons.dbcp.PoolingDriver"); PoolingDriver driver = (PoolingDriver) DriverManager.getDriver("jdbc:apache:commons:dbcp:"); // Register the pool driver.registerPool(dbConfig.getDatabaseID(), connectionPool); } public void disconnect() throws RimuDBException { try { PoolingDriver driver = (PoolingDriver) DriverManager.getDriver("jdbc:apache:commons:dbcp:"); driver.closePool(dbConfig.getDatabaseID()); } catch (SQLException e) { throw new RimuDBException(e); } } public Connection getDatabaseConnection() throws SQLException { Connection connection = DriverManager.getConnection("jdbc:apache:commons:dbcp:" + dbConfig.getDatabaseID()); // Set the default transaction isolation level if (poolConfiguration.getDefaultTransactionIsolation() != TransactionIsolation.TRANSACTION_NOT_SPECIFIED) { connection.setTransactionIsolation(poolConfiguration.getDefaultTransactionIsolation().getJdbcValue()); } // Set the default auto-commit value if (poolConfiguration.getDefaultAutoCommit() != AutoCommit.AUTO_COMMIT_NONE) { connection.setAutoCommit(poolConfiguration.getDefaultAutoCommit().getJdbcValue()); } return connection; } public PoolStatistic getPoolStatistic() throws RimuDBException { DBCPPoolConfiguration poolConfiguration = (DBCPPoolConfiguration) dbConfig.getPoolConfiguration(); PoolStatistic poolStatistic = new PoolStatistic(); poolStatistic.setPoolName(dbConfig.getDatabaseID()); poolStatistic.setUrl(poolConfiguration.getJdbcURL()); if (connectionPool != null) { poolStatistic.setConnectionsInPool(connectionPool.getNumActive() + connectionPool.getNumIdle()); poolStatistic.setMaximumConnections(connectionPool.getMaxActive()); poolStatistic.setActiveConnections(connectionPool.getNumActive()); poolStatistic.setIdleConnections(connectionPool.getNumIdle()); poolStatistic.setTimeBetweenEvictionRunsMillis(connectionPool.getTimeBetweenEvictionRunsMillis()); } return poolStatistic; } }