All Downloads are FREE. Search and download functionalities are using the official Maven repository.

pl.kernelpanic.dbmonster.connection.DBCPConnectionProvider Maven / Gradle / Ivy

/* Version 1.0 based on Apache Software License 1.1
 *
 * Copyright (c) 2003 Piotr Maj and DBMonster developers. All rights
 * reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * 3. The end-user documentation included with the redistribution, if any,
 *    must include the following acknowledgment:
 *
 *    "This product includes software developed by DBMonster developers
 *    (http://dbmonster.kernelpanic.pl/)."
 *
 *  Alternately, this acknowledgment may appear in the software itself,
 *  if and wherever such third-party acknowledgments normally appear.
 *
 * 4. The name "DBMonster" must not be used to endorse or promote products
 *    derived from this software without prior written permission. For
 *    written permission, please contact [email protected].
 *
 * 5. Products derived from this software may not be called "DBMonster",
 *    nor may "DBMonster" appear in their name, without prior written
 *    permission of Piotr Maj.
 *
 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE DBMONSTER DEVELOPERS BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

package pl.kernelpanic.dbmonster.connection;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.SQLException;
import java.util.Properties;

import javax.sql.DataSource;

import org.apache.commons.dbcp.ConnectionFactory;
import org.apache.commons.dbcp.PoolingDataSource;
import org.apache.commons.dbcp.PoolableConnectionFactory;
import org.apache.commons.dbcp.DriverManagerConnectionFactory;
import org.apache.commons.logging.Log;
import org.apache.commons.pool.ObjectPool;
import org.apache.commons.pool.impl.GenericObjectPool;

/**
 * The connection provider which uses commons-dbcp.
 * It uses the following properties to establish connection:
 *
 * 
    *
  • dbmonster.jdbc.driver (ie. org.postgresql.Driver)
  • *
  • dbmonster.jdbc.url (ie. jdbc:postgresql://127.0.0.1/dbmonster)
  • *
  • dbmonster.jdbc.username (ie. dbmonster)
  • *
  • dbmonster.jdbc.password (ie. dbmonster)
  • *
* * By default these properties are taken from System.getProperty, but you may * also provide them on your own. * * @author Piotr Maj * * @version $Id: DBCPConnectionProvider.java,v 1.2 2006/01/05 16:29:37 majek Exp $ */ public class DBCPConnectionProvider implements ConnectionProvider, LogEnabled { /** * JDBC driver name. */ private String driver = System.getProperty("dbmonster.jdbc.driver"); /** * JDBC url. */ private String url = System.getProperty("dbmonster.jdbc.url"); /** * JDBC user name. */ private String username = System.getProperty("dbmonster.jdbc.username"); /** * JDBC user's password. */ private String password = System.getProperty("dbmonster.jdbc.password"); /** * Properties used to establish connection. */ private Properties properties = null; /** * A datasource. */ private DataSource dataSource = null; /** * Logger. */ private Log logger = null; /** * Connection pool handler. */ private ObjectPool pool; private boolean autoCommit = true; /** * Creates new SimpleConnectionProvider. * * @throws Exception if driver class could not be found. */ public DBCPConnectionProvider() throws Exception { initDriver(); setupPool(); } /** * Creates new DBCPConnectionProvider with given connection info. * * @param jdbcDriver JDBC driver * @param jdbcUrl JDBC url * @param jdbcUsername JDBC user name * @param jdbcPassword JDBC password * * @throws Exception if the driver class could not be found. */ public DBCPConnectionProvider( String jdbcDriver, String jdbcUrl, String jdbcUsername, String jdbcPassword) throws Exception { driver = jdbcDriver; url = jdbcUrl; username = jdbcUsername; password = jdbcPassword; initDriver(); setupPool(); } /** * Creates new DBCPConnectionProvider with properties (usefull * for interbase). * * @param jdbcDriver JDBC driver * @param jdbcUrl JDBC url * @param props properties * * @throws Exception if driver class could not be found. */ public DBCPConnectionProvider( String jdbcDriver, String jdbcUrl, Properties props) throws Exception { driver = jdbcDriver; url = jdbcUrl; properties = props; initDriver(); setupPool(); } /** * @see ConnectionProvider#getConnection() */ public Connection getConnection() throws SQLException { if (dataSource == null) { throw new SQLException("Data source is null!"); } Connection conn = dataSource.getConnection(); conn.setAutoCommit(autoCommit); return conn; } /** * @see ConnectionProvider#testConnection() */ public void testConnection() throws SQLException { Connection conn = getConnection(); DatabaseMetaData dbmd = conn.getMetaData(); if (logger != null && logger.isInfoEnabled()) { logger.info("Today we are feeding: " + dbmd.getDatabaseProductName() + " " + dbmd.getDatabaseProductVersion()); } conn.close(); dbmd = null; conn = null; } /** * Sets the logger. * * @param log a logger. */ public void setLogger(Log log) { logger = log; } /** * Shutdown this connection provider. */ public void shutdown() { try { pool.close(); } catch (Exception e) { } finally { pool = null; } dataSource = null; driver = null; logger = null; password = null; properties = null; url = null; username = null; } /** * Initializes the JDBC driver. * * @throws ClassNotFoundException if driver class could not be found. */ private void initDriver() throws ClassNotFoundException { Class.forName(driver); } /** * Sets the pool up. * * @throws Exception if the pool cannot be set up */ private void setupPool() throws Exception { pool = new GenericObjectPool(null); ConnectionFactory connFactory = null; if (properties != null) { connFactory = new DriverManagerConnectionFactory(url, properties); } else { connFactory = new DriverManagerConnectionFactory(url, username, password); } new PoolableConnectionFactory( connFactory, pool, null, null, false, true); dataSource = new PoolingDataSource(pool); } public void setAutoCommit(boolean autoCommit) { this.autoCommit = autoCommit; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy