FileDocCategorySizeDatePackage
C3P0ConnectionProvider.javaAPI DocHibernate 3.2.58070Fri Jan 19 08:14:44 GMT 2007org.hibernate.connection

C3P0ConnectionProvider

public class C3P0ConnectionProvider extends Object implements ConnectionProvider
A connection provider that uses a C3P0 connection pool. Hibernate will use this by default if the hibernate.c3p0.* properties are set.
author
various people
see
ConnectionProvider

Fields Summary
private static final Log
log
private static final String
C3P0_STYLE_MIN_POOL_SIZE
private static final String
C3P0_STYLE_MAX_POOL_SIZE
private static final String
C3P0_STYLE_MAX_IDLE_TIME
private static final String
C3P0_STYLE_MAX_STATEMENTS
private static final String
C3P0_STYLE_ACQUIRE_INCREMENT
private static final String
C3P0_STYLE_IDLE_CONNECTION_TEST_PERIOD
private static final String
C3P0_STYLE_TEST_CONNECTION_ON_CHECKOUT
private static final String
C3P0_STYLE_INITIAL_POOL_SIZE
private DataSource
ds
private Integer
isolation
private boolean
autocommit
Constructors Summary
Methods Summary
public voidclose()
{@inheritDoc}

		try {
			DataSources.destroy( ds );
		}
		catch ( SQLException sqle ) {
			log.warn( "could not destroy C3P0 connection pool", sqle );
		}
	
public voidcloseConnection(java.sql.Connection conn)
{@inheritDoc}

		conn.close();
	
public voidconfigure(java.util.Properties props)
{@inheritDoc}

		String jdbcDriverClass = props.getProperty( Environment.DRIVER );
		String jdbcUrl = props.getProperty( Environment.URL );
		Properties connectionProps = ConnectionProviderFactory.getConnectionProperties( props );

		log.info( "C3P0 using driver: " + jdbcDriverClass + " at URL: " + jdbcUrl );
		log.info( "Connection properties: " + PropertiesHelper.maskOut( connectionProps, "password" ) );

		autocommit = PropertiesHelper.getBoolean( Environment.AUTOCOMMIT, props );
		log.info( "autocommit mode: " + autocommit );

		if ( jdbcDriverClass == null ) {
			log.warn( "No JDBC Driver class was specified by property " + Environment.DRIVER );
		}
		else {
			try {
				Class.forName( jdbcDriverClass );
			}
			catch ( ClassNotFoundException cnfe ) {
				try {
					ReflectHelper.classForName( jdbcDriverClass );
				}
				catch ( ClassNotFoundException e ) {
					String msg = "JDBC Driver class not found: " + jdbcDriverClass;
					log.fatal( msg, e );
					throw new HibernateException( msg, e );
				}
			}
		}

		try {

			//swaldman 2004-02-07: modify to allow null values to signify fall through to c3p0 PoolConfig defaults
			Integer minPoolSize = PropertiesHelper.getInteger( Environment.C3P0_MIN_SIZE, props );
			Integer maxPoolSize = PropertiesHelper.getInteger( Environment.C3P0_MAX_SIZE, props );
			Integer maxIdleTime = PropertiesHelper.getInteger( Environment.C3P0_TIMEOUT, props );
			Integer maxStatements = PropertiesHelper.getInteger( Environment.C3P0_MAX_STATEMENTS, props );
			Integer acquireIncrement = PropertiesHelper.getInteger( Environment.C3P0_ACQUIRE_INCREMENT, props );
			Integer idleTestPeriod = PropertiesHelper.getInteger( Environment.C3P0_IDLE_TEST_PERIOD, props );

			Properties c3props = new Properties();

			// turn hibernate.c3p0.* into c3p0.*, so c3p0
			// gets a chance to see all hibernate.c3p0.*
			for ( Iterator ii = props.keySet().iterator(); ii.hasNext(); ) {
				String key = ( String ) ii.next();
				if ( key.startsWith( "hibernate.c3p0." ) ) {
					String newKey = key.substring( 10 );
					if ( props.containsKey( newKey ) ) {
						warnPropertyConflict( key, newKey );
					}
					c3props.put( newKey, props.get( key ) );
				}
			}

			setOverwriteProperty( Environment.C3P0_MIN_SIZE, C3P0_STYLE_MIN_POOL_SIZE, props, c3props, minPoolSize );
			setOverwriteProperty( Environment.C3P0_MAX_SIZE, C3P0_STYLE_MAX_POOL_SIZE, props, c3props, maxPoolSize );
			setOverwriteProperty( Environment.C3P0_TIMEOUT, C3P0_STYLE_MAX_IDLE_TIME, props, c3props, maxIdleTime );
			setOverwriteProperty(
					Environment.C3P0_MAX_STATEMENTS, C3P0_STYLE_MAX_STATEMENTS, props, c3props, maxStatements
			);
			setOverwriteProperty(
					Environment.C3P0_ACQUIRE_INCREMENT, C3P0_STYLE_ACQUIRE_INCREMENT, props, c3props, acquireIncrement
			);
			setOverwriteProperty(
					Environment.C3P0_IDLE_TEST_PERIOD, C3P0_STYLE_IDLE_CONNECTION_TEST_PERIOD, props, c3props, idleTestPeriod
			);

			// revert to traditional hibernate behavior of setting initialPoolSize to minPoolSize
			// unless otherwise specified with a c3p0.*-style parameter.
			Integer initialPoolSize = PropertiesHelper.getInteger( C3P0_STYLE_INITIAL_POOL_SIZE, props );
			if ( initialPoolSize == null && minPoolSize != null ) {
				c3props.put( C3P0_STYLE_INITIAL_POOL_SIZE, String.valueOf( minPoolSize ).trim() );
			}

			/*DataSource unpooled = DataSources.unpooledDataSource(
				jdbcUrl, props.getProperty(Environment.USER), props.getProperty(Environment.PASS)
			);*/
			DataSource unpooled = DataSources.unpooledDataSource( jdbcUrl, connectionProps );

			Properties allProps = ( Properties ) props.clone();
			allProps.putAll( c3props );

			ds = DataSources.pooledDataSource( unpooled, allProps );
		}
		catch ( Exception e ) {
			log.fatal( "could not instantiate C3P0 connection pool", e );
			throw new HibernateException( "Could not instantiate C3P0 connection pool", e );
		}

		String i = props.getProperty( Environment.ISOLATION );
		if ( i == null ) {
			isolation = null;
		}
		else {
			isolation = new Integer( i );
			log.info( "JDBC isolation level: " + Environment.isolationLevelToString( isolation.intValue() ) );
		}

	
public java.sql.ConnectiongetConnection()
{@inheritDoc}


	 	 
	     
		final Connection c = ds.getConnection();
		if ( isolation != null ) {
			c.setTransactionIsolation( isolation.intValue() );
		}
		if ( c.getAutoCommit() != autocommit ) {
			c.setAutoCommit( autocommit );
		}
		return c;
	
private voidsetOverwriteProperty(java.lang.String hibernateStyleKey, java.lang.String c3p0StyleKey, java.util.Properties hibp, java.util.Properties c3p, java.lang.Integer value)

		if ( value != null ) {
			c3p.put( c3p0StyleKey, String.valueOf( value ).trim() );
			if ( hibp.getProperty( c3p0StyleKey ) != null ) {
				warnPropertyConflict( hibernateStyleKey, c3p0StyleKey );
			}
			String longC3p0StyleKey = "hibernate." + c3p0StyleKey;
			if ( hibp.getProperty( longC3p0StyleKey ) != null ) {
				warnPropertyConflict( hibernateStyleKey, longC3p0StyleKey );
			}
		}
	
public booleansupportsAggressiveRelease()
{@inheritDoc}

		return false;
	
private voidwarnPropertyConflict(java.lang.String hibernateStyle, java.lang.String c3p0Style)

		log.warn(
				"Both hibernate-style property '" + hibernateStyle +
						"' and c3p0-style property '" + c3p0Style +
						"' have been set in hibernate.properties. " +
						"Hibernate-style property '" + hibernateStyle + "' will be used " +
						"and c3p0-style property '" + c3p0Style + "' will be ignored!"
		);