FileDocCategorySizeDatePackage
DriverManagerConnectionProvider.javaAPI DocHibernate 3.2.55013Fri Aug 12 16:22:38 BST 2005org.hibernate.connection

DriverManagerConnectionProvider

public class DriverManagerConnectionProvider extends Object implements ConnectionProvider
A connection provider that uses java.sql.DriverManager. This provider also implements a very rudimentary connection pool.
see
ConnectionProvider
author
Gavin King

Fields Summary
private String
url
private Properties
connectionProps
private Integer
isolation
private final ArrayList
pool
private int
poolSize
private int
checkedOut
private boolean
autocommit
private static final Log
log
Constructors Summary
Methods Summary
public voidclose()


		log.info("cleaning up connection pool: " + url);

		Iterator iter = pool.iterator();
		while ( iter.hasNext() ) {
			try {
				( (Connection) iter.next() ).close();
			}
			catch (SQLException sqle) {
				log.warn("problem closing pooled connection", sqle);
			}
		}
		pool.clear();

	
public voidcloseConnection(java.sql.Connection conn)


		if ( log.isDebugEnabled() ) checkedOut--;

		synchronized (pool) {
			int currentSize = pool.size();
			if ( currentSize < poolSize ) {
				if ( log.isTraceEnabled() ) log.trace("returning connection to pool, pool size: " + (currentSize + 1) );
				pool.add(conn);
				return;
			}
		}

		log.debug("closing JDBC connection");

		conn.close();

	
public voidconfigure(java.util.Properties props)


	      

		String driverClass = props.getProperty(Environment.DRIVER);

		poolSize = PropertiesHelper.getInt(Environment.POOL_SIZE, props, 20); //default pool size 20
		log.info("Using Hibernate built-in connection pool (not for production use!)");
		log.info("Hibernate connection pool size: " + poolSize);
		
		autocommit = PropertiesHelper.getBoolean(Environment.AUTOCOMMIT, props);
		log.info("autocommit mode: " + autocommit);

		isolation = PropertiesHelper.getInteger(Environment.ISOLATION, props);
		if (isolation!=null)
		log.info( "JDBC isolation level: " + Environment.isolationLevelToString( isolation.intValue() ) );

		if (driverClass==null) {
			log.warn("no JDBC Driver class was specified by property " + Environment.DRIVER);
		}
		else {
			try {
				// trying via forName() first to be as close to DriverManager's semantics
				Class.forName(driverClass);
			}
			catch (ClassNotFoundException cnfe) {
				try {
					ReflectHelper.classForName(driverClass);
				}
				catch (ClassNotFoundException e) {
					String msg = "JDBC Driver class not found: " + driverClass;
					log.fatal(msg, e);
					throw new HibernateException(msg, e);
				}
			}
		}

		url = props.getProperty(Environment.URL);
		if (url==null) {
			String msg = "JDBC URL was not specified by property " + Environment.URL;
			log.fatal(msg);
			throw new HibernateException(msg);
		}

		connectionProps = ConnectionProviderFactory.getConnectionProperties(props);

		log.info( "using driver: " + driverClass + " at URL: " + url );
		// if debug level is enabled, then log the password, otherwise mask it
		if ( log.isDebugEnabled() ) {
			log.info( "connection properties: " + connectionProps );
		} 
		else if ( log.isInfoEnabled() ) {
			log.info( "connection properties: " + PropertiesHelper.maskOut(connectionProps, "password") );
		}

	
protected voidfinalize()

		close();
	
public java.sql.ConnectiongetConnection()


		if ( log.isTraceEnabled() ) log.trace( "total checked-out connections: " + checkedOut );

		synchronized (pool) {
			if ( !pool.isEmpty() ) {
				int last = pool.size() - 1;
				if ( log.isTraceEnabled() ) {
					log.trace("using pooled JDBC connection, pool size: " + last);
					checkedOut++;
				}
				Connection pooled = (Connection) pool.remove(last);
				if (isolation!=null) pooled.setTransactionIsolation( isolation.intValue() );
				if ( pooled.getAutoCommit()!=autocommit ) pooled.setAutoCommit(autocommit);
				return pooled;
			}
		}

		log.debug("opening new JDBC connection");
		Connection conn = DriverManager.getConnection(url, connectionProps);
		if (isolation!=null) conn.setTransactionIsolation( isolation.intValue() );
		if ( conn.getAutoCommit()!=autocommit ) conn.setAutoCommit(autocommit);

		if ( log.isDebugEnabled() ) {
			log.debug( "created connection to: " + url + ", Isolation Level: " + conn.getTransactionIsolation() );
		}
		if ( log.isTraceEnabled() ) checkedOut++;

		return conn;
	
public booleansupportsAggressiveRelease()

see
ConnectionProvider#supportsAggressiveRelease()

		return false;