Save This Page
Home » hibernate-distribution-3.3.1.GA-dist » org.hibernate » connection » [javadoc | source]
    1   /*
    2    * Hibernate, Relational Persistence for Idiomatic Java
    3    *
    4    * Copyright (c) 2008, Red Hat Middleware LLC or third-party contributors as
    5    * indicated by the @author tags or express copyright attribution
    6    * statements applied by the authors.  All third-party contributions are
    7    * distributed under license by Red Hat Middleware LLC.
    8    *
    9    * This copyrighted material is made available to anyone wishing to use, modify,
   10    * copy, or redistribute it subject to the terms and conditions of the GNU
   11    * Lesser General Public License, as published by the Free Software Foundation.
   12    *
   13    * This program is distributed in the hope that it will be useful,
   14    * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
   15    * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
   16    * for more details.
   17    *
   18    * You should have received a copy of the GNU Lesser General Public License
   19    * along with this distribution; if not, write to:
   20    * Free Software Foundation, Inc.
   21    * 51 Franklin Street, Fifth Floor
   22    * Boston, MA  02110-1301  USA
   23    *
   24    */
   25   package org.hibernate.connection;
   26   
   27   import java.sql.Connection;
   28   import java.sql.DriverManager;
   29   import java.sql.SQLException;
   30   import java.util.ArrayList;
   31   import java.util.Iterator;
   32   import java.util.Properties;
   33   
   34   import org.slf4j.Logger;
   35   import org.slf4j.LoggerFactory;
   36   import org.hibernate.HibernateException;
   37   import org.hibernate.cfg.Environment;
   38   import org.hibernate.util.PropertiesHelper;
   39   import org.hibernate.util.ReflectHelper;
   40   
   41   /**
   42    * A connection provider that uses <tt>java.sql.DriverManager</tt>. This provider
   43    * also implements a very rudimentary connection pool.
   44    * @see ConnectionProvider
   45    * @author Gavin King
   46    */
   47   public class DriverManagerConnectionProvider implements ConnectionProvider {
   48   
   49   	private String url;
   50   	private Properties connectionProps;
   51   	private Integer isolation;
   52   	private final ArrayList pool = new ArrayList();
   53   	private int poolSize;
   54   	private int checkedOut = 0;
   55   	private boolean autocommit;
   56   
   57   	private static final Logger log = LoggerFactory.getLogger(DriverManagerConnectionProvider.class);
   58   
   59   	public void configure(Properties props) throws HibernateException {
   60   
   61   		String driverClass = props.getProperty(Environment.DRIVER);
   62   
   63   		poolSize = PropertiesHelper.getInt(Environment.POOL_SIZE, props, 20); //default pool size 20
   64   		log.info("Using Hibernate built-in connection pool (not for production use!)");
   65   		log.info("Hibernate connection pool size: " + poolSize);
   66   		
   67   		autocommit = PropertiesHelper.getBoolean(Environment.AUTOCOMMIT, props);
   68   		log.info("autocommit mode: " + autocommit);
   69   
   70   		isolation = PropertiesHelper.getInteger(Environment.ISOLATION, props);
   71   		if (isolation!=null)
   72   		log.info( "JDBC isolation level: " + Environment.isolationLevelToString( isolation.intValue() ) );
   73   
   74   		if (driverClass==null) {
   75   			log.warn("no JDBC Driver class was specified by property " + Environment.DRIVER);
   76   		}
   77   		else {
   78   			try {
   79   				// trying via forName() first to be as close to DriverManager's semantics
   80   				Class.forName(driverClass);
   81   			}
   82   			catch (ClassNotFoundException cnfe) {
   83   				try {
   84   					ReflectHelper.classForName(driverClass);
   85   				}
   86   				catch (ClassNotFoundException e) {
   87   					String msg = "JDBC Driver class not found: " + driverClass;
   88   					log.error( msg, e );
   89   					throw new HibernateException(msg, e);
   90   				}
   91   			}
   92   		}
   93   
   94   		url = props.getProperty( Environment.URL );
   95   		if ( url == null ) {
   96   			String msg = "JDBC URL was not specified by property " + Environment.URL;
   97   			log.error( msg );
   98   			throw new HibernateException( msg );
   99   		}
  100   
  101   		connectionProps = ConnectionProviderFactory.getConnectionProperties( props );
  102   
  103   		log.info( "using driver: " + driverClass + " at URL: " + url );
  104   		// if debug level is enabled, then log the password, otherwise mask it
  105   		if ( log.isDebugEnabled() ) {
  106   			log.info( "connection properties: " + connectionProps );
  107   		} 
  108   		else if ( log.isInfoEnabled() ) {
  109   			log.info( "connection properties: " + PropertiesHelper.maskOut(connectionProps, "password") );
  110   		}
  111   
  112   	}
  113   
  114   	public Connection getConnection() throws SQLException {
  115   
  116   		if ( log.isTraceEnabled() ) log.trace( "total checked-out connections: " + checkedOut );
  117   
  118   		synchronized (pool) {
  119   			if ( !pool.isEmpty() ) {
  120   				int last = pool.size() - 1;
  121   				if ( log.isTraceEnabled() ) {
  122   					log.trace("using pooled JDBC connection, pool size: " + last);
  123   					checkedOut++;
  124   				}
  125   				Connection pooled = (Connection) pool.remove(last);
  126   				if (isolation!=null) pooled.setTransactionIsolation( isolation.intValue() );
  127   				if ( pooled.getAutoCommit()!=autocommit ) pooled.setAutoCommit(autocommit);
  128   				return pooled;
  129   			}
  130   		}
  131   
  132   		log.debug("opening new JDBC connection");
  133   		Connection conn = DriverManager.getConnection(url, connectionProps);
  134   		if (isolation!=null) conn.setTransactionIsolation( isolation.intValue() );
  135   		if ( conn.getAutoCommit()!=autocommit ) conn.setAutoCommit(autocommit);
  136   
  137   		if ( log.isDebugEnabled() ) {
  138   			log.debug( "created connection to: " + url + ", Isolation Level: " + conn.getTransactionIsolation() );
  139   		}
  140   		if ( log.isTraceEnabled() ) checkedOut++;
  141   
  142   		return conn;
  143   	}
  144   
  145   	public void closeConnection(Connection conn) throws SQLException {
  146   
  147   		if ( log.isDebugEnabled() ) checkedOut--;
  148   
  149   		synchronized (pool) {
  150   			int currentSize = pool.size();
  151   			if ( currentSize < poolSize ) {
  152   				if ( log.isTraceEnabled() ) log.trace("returning connection to pool, pool size: " + (currentSize + 1) );
  153   				pool.add(conn);
  154   				return;
  155   			}
  156   		}
  157   
  158   		log.debug("closing JDBC connection");
  159   
  160   		conn.close();
  161   
  162   	}
  163   
  164   	protected void finalize() {
  165   		close();
  166   	}
  167   
  168   	public void close() {
  169   
  170   		log.info("cleaning up connection pool: " + url);
  171   
  172   		Iterator iter = pool.iterator();
  173   		while ( iter.hasNext() ) {
  174   			try {
  175   				( (Connection) iter.next() ).close();
  176   			}
  177   			catch (SQLException sqle) {
  178   				log.warn("problem closing pooled connection", sqle);
  179   			}
  180   		}
  181   		pool.clear();
  182   
  183   	}
  184   
  185   	/**
  186   	 * @see ConnectionProvider#supportsAggressiveRelease()
  187   	 */
  188   	public boolean supportsAggressiveRelease() {
  189   		return false;
  190   	}
  191   
  192   }
  193   
  194   
  195   
  196   
  197   
  198   
  199   

Save This Page
Home » hibernate-distribution-3.3.1.GA-dist » org.hibernate » connection » [javadoc | source]