一、简介
c3p0是开源的数据库连接池(相关配置参数参见“c3p0数据连接池补充说明”,开发接口参见“C3p0开发API”),目前有Hibernate,Spring等项目支持它,跳转到GitHub浏览详细源代码https://github.com/xwooduser/struts2-core-2.3.8-sources。
二、项目快照
三、源码代表类
ComboPooledDataSource类
package com.mchange.v2.c3p0;@b@ @b@import java.beans.*;@b@import java.io.*;@b@import java.sql.*;@b@import java.util.*;@b@import javax.naming.*;@b@import com.mchange.v2.log.*;@b@import com.mchange.v2.naming.*;@b@import com.mchange.v2.c3p0.impl.*;@b@ @b@import javax.sql.DataSource;@b@import com.mchange.v2.beans.BeansUtils;@b@import com.mchange.v2.c3p0.cfg.C3P0Config;@b@ @b@/**@b@ * <p>For the meaning of most of these properties, please see c3p0's top-level documentation!</p>@b@ */@b@public final class ComboPooledDataSource extends AbstractPoolBackedDataSource implements PooledDataSource, Serializable, Referenceable@b@{@b@ final static MLogger logger = MLog.getLogger( ComboPooledDataSource.class );@b@ @b@ final static Set TO_STRING_IGNORE_PROPS = new HashSet( Arrays.asList( new String[] { @b@ "connection",@b@ "lastAcquisitionFailureDefaultUser",@b@ "lastCheckinFailureDefaultUser",@b@ "lastCheckoutFailureDefaultUser",@b@ "lastConnectionTestFailureDefaultUser",@b@ "lastIdleTestFailureDefaultUser",@b@ "logWriter",@b@ "loginTimeout",@b@ "numBusyConnections",@b@ "numBusyConnectionsAllUsers",@b@ "numBusyConnectionsDefaultUser",@b@ "numConnections",@b@ "numConnectionsAllUsers",@b@ "numConnectionsDefaultUser",@b@ "numFailedCheckinsDefaultUser",@b@ "numFailedCheckoutsDefaultUser",@b@ "numFailedIdleTestsDefaultUser",@b@ "numIdleConnections",@b@ "numIdleConnectionsAllUsers",@b@ "numIdleConnectionsDefaultUser",@b@ "numUnclosedOrphanedConnections",@b@ "numUnclosedOrphanedConnectionsAllUsers",@b@ "numUnclosedOrphanedConnectionsDefaultUser",@b@ "numUserPools",@b@ "effectivePropertyCycleDefaultUser",@b@ "startTimeMillisDefaultUser",@b@ "statementCacheNumCheckedOutDefaultUser",@b@ "statementCacheNumCheckedOutStatementsAllUsers",@b@ "statementCacheNumConnectionsWithCachedStatementsAllUsers",@b@ "statementCacheNumConnectionsWithCachedStatementsDefaultUser",@b@ "statementCacheNumStatementsAllUsers",@b@ "statementCacheNumStatementsDefaultUser",@b@ "threadPoolSize",@b@ "threadPoolNumActiveThreads",@b@ "threadPoolNumIdleThreads",@b@ "threadPoolNumTasksPending",@b@ "threadPoolStackTraces",@b@ "threadPoolStatus",@b@ "overrideDefaultUser",@b@ "overrideDefaultPassword",@b@ "password",@b@ "reference",@b@ "upTimeMillisDefaultUser",@b@ "user",@b@ "userOverridesAsString",@b@ "allUsers",@b@ "connectionPoolDataSource"@b@ } ) );@b@ @b@ // not reassigned post-ctor; mutable elements protected by their own locks@b@ // when (very rarely) necessery, we sync this -> wcpds -> dmds@b@ @b@ // note that serialization of these guys happens via out superclass@b@ // we just have to make sure they get properly reset on deserialization@b@ transient DriverManagerDataSource dmds;@b@ transient WrapperConnectionPoolDataSource wcpds;@b@ @b@ public ComboPooledDataSource()@b@ { this( true ); }@b@ @b@ public ComboPooledDataSource( boolean autoregister )@b@ {@b@ super( autoregister );@b@ @b@ // System.err.println("...Initializing ComboPooledDataSource.");@b@ @b@ dmds = new DriverManagerDataSource();@b@ wcpds = new WrapperConnectionPoolDataSource();@b@ @b@ wcpds.setNestedDataSource( dmds );@b@ @b@ try@b@ { this.setConnectionPoolDataSource( wcpds ); }@b@ catch (PropertyVetoException e)@b@ {@b@ logger.log(MLevel.WARNING, "Hunh??? This can't happen. We haven't set up any listeners to veto the property change yet!", e);@b@ throw new RuntimeException("Hunh??? This can't happen. We haven't set up any listeners to veto the property change yet! " + e);@b@ }@b@ @b@ // set things up in case there are future changes to our ConnectionPoolDataSource@b@ //@b@ setUpPropertyEvents();@b@ }@b@ @b@ private void setUpPropertyEvents()@b@ {@b@ VetoableChangeListener wcpdsConsistencyEnforcer = new VetoableChangeListener()@b@ {@b@ // always called within synchronized mutators of the parent class... needn't explicitly sync here@b@ public void vetoableChange( PropertyChangeEvent evt ) throws PropertyVetoException@b@ {@b@ String propName = evt.getPropertyName();@b@ Object val = evt.getNewValue();@b@ @b@ if ( "connectionPoolDataSource".equals( propName ) )@b@ {@b@ if (val instanceof WrapperConnectionPoolDataSource)@b@ {@b@ DataSource nested = (DataSource) ((WrapperConnectionPoolDataSource)val).getNestedDataSource();@b@ if (! (nested instanceof DriverManagerDataSource) )@b@ throw new PropertyVetoException("ComboPooledDataSource requires that its unpooled DataSource " +@b@ " be set at all times, and that it be a" +@b@ " com.mchange.v2.c3p0.DriverManagerDataSource. Bad: " + nested, evt);@b@ }@b@ else@b@ throw new PropertyVetoException("ComboPooledDataSource requires that its ConnectionPoolDataSource " +@b@ " be set at all times, and that it be a" +@b@ " com.mchange.v2.c3p0.WrapperConnectionPoolDataSource. Bad: " + val, evt);@b@ }@b@ }@b@ };@b@ this.addVetoableChangeListener( wcpdsConsistencyEnforcer );@b@ @b@ PropertyChangeListener wcpdsStateUpdater = new PropertyChangeListener()@b@ {@b@ public void propertyChange( PropertyChangeEvent evt )@b@ { updateLocalVarsFromCpdsProp(); }@b@ };@b@ this.addPropertyChangeListener( wcpdsStateUpdater );@b@ }@b@ @b@ private void updateLocalVarsFromCpdsProp()@b@ {@b@ this.wcpds = (WrapperConnectionPoolDataSource) this.getConnectionPoolDataSource();@b@ this.dmds = (DriverManagerDataSource) wcpds.getNestedDataSource();@b@ }@b@ @b@ public ComboPooledDataSource(String configName)@b@ { @b@ this();@b@ initializeNamedConfig( configName );@b@ }@b@ @b@// // workaround sun big id #6342411 (in which reflective@b@// // access to a public method of a non-public class fails,@b@// // even if the non-public class is accessed via a public@b@// // subclass)@b@// public String getDataSourceName()@b@// { return super.getDataSourceName(); }@b@ @b@ // DriverManagerDataSourceProperties (count: 4)@b@ public String getDescription()@b@ { return dmds.getDescription(); }@b@ @b@ public void setDescription( String description )@b@ { dmds.setDescription( description ); }@b@ @b@ public String getDriverClass()@b@ { return dmds.getDriverClass(); }@b@ @b@ public void setDriverClass( String driverClass ) throws PropertyVetoException@b@ { @b@ dmds.setDriverClass( driverClass ); @b@// System.err.println("setting driverClass: " + driverClass); @b@ }@b@ @b@ public String getJdbcUrl()@b@ { @b@// System.err.println("getting jdbcUrl: " + dmds.getJdbcUrl()); @b@ return dmds.getJdbcUrl(); @b@ }@b@ @b@ public void setJdbcUrl( String jdbcUrl )@b@ { @b@ dmds.setJdbcUrl( jdbcUrl ); @b@ this.resetPoolManager( false );@b@// System.err.println("setting jdbcUrl: " + jdbcUrl + " [dmds@" + C3P0ImplUtils.identityToken( dmds ) + "]"); @b@// if (jdbcUrl == null)@b@// new Exception("*** NULL SETTER ***").printStackTrace();@b@ }@b@ @b@ public Properties getProperties()@b@ { @b@ //System.err.println("getting properties: " + dmds.getProperties()); @b@ return dmds.getProperties(); @b@ }@b@ @b@ public void setProperties( Properties properties )@b@ { @b@ //System.err.println("setting properties: " + properties); @b@ dmds.setProperties( properties ); @b@ this.resetPoolManager(false);@b@ }@b@ @b@ // DriverManagerDataSource "virtual properties" based on properties@b@ public String getUser()@b@ { return dmds.getUser(); }@b@ @b@ public void setUser( String user )@b@ { @b@ dmds.setUser( user ); @b@ this.resetPoolManager( false );@b@ }@b@ @b@ public String getPassword()@b@ { return dmds.getPassword(); }@b@ @b@ public void setPassword( String password )@b@ { @b@ dmds.setPassword( password ); @b@ this.resetPoolManager( false );@b@ }@b@ @b@ // WrapperConnectionPoolDataSource properties@b@ public int getCheckoutTimeout()@b@ { return wcpds.getCheckoutTimeout(); }@b@ @b@ public void setCheckoutTimeout( int checkoutTimeout )@b@ { @b@ wcpds.setCheckoutTimeout( checkoutTimeout ); @b@ this.resetPoolManager( false );@b@ }@b@ @b@ public int getAcquireIncrement()@b@ { return wcpds.getAcquireIncrement(); }@b@ @b@ public void setAcquireIncrement( int acquireIncrement )@b@ { @b@ wcpds.setAcquireIncrement( acquireIncrement ); @b@ this.resetPoolManager( false );@b@ }@b@ @b@ public int getAcquireRetryAttempts()@b@ { return wcpds.getAcquireRetryAttempts(); }@b@ @b@ public void setAcquireRetryAttempts( int acquireRetryAttempts )@b@ { @b@ wcpds.setAcquireRetryAttempts( acquireRetryAttempts ); @b@ this.resetPoolManager( false );@b@ }@b@ @b@ public int getAcquireRetryDelay()@b@ { return wcpds.getAcquireRetryDelay(); }@b@ @b@ public void setAcquireRetryDelay( int acquireRetryDelay )@b@ { @b@ wcpds.setAcquireRetryDelay( acquireRetryDelay ); @b@ this.resetPoolManager( false );@b@ }@b@ @b@ public boolean isAutoCommitOnClose()@b@ { return wcpds.isAutoCommitOnClose(); }@b@ @b@ public void setAutoCommitOnClose( boolean autoCommitOnClose )@b@ { @b@ wcpds.setAutoCommitOnClose( autoCommitOnClose ); @b@ this.resetPoolManager( false );@b@ }@b@ @b@ public String getConnectionTesterClassName()@b@ { return wcpds.getConnectionTesterClassName(); }@b@ @b@ public void setConnectionTesterClassName( String connectionTesterClassName ) throws PropertyVetoException@b@ { @b@ wcpds.setConnectionTesterClassName( connectionTesterClassName ); @b@ this.resetPoolManager( false );@b@ }@b@ @b@ public String getAutomaticTestTable()@b@ { return wcpds.getAutomaticTestTable(); }@b@ @b@ public void setAutomaticTestTable( String automaticTestTable )@b@ { @b@ wcpds.setAutomaticTestTable( automaticTestTable ); @b@ this.resetPoolManager( false );@b@ }@b@ @b@ public boolean isForceIgnoreUnresolvedTransactions()@b@ { return wcpds.isForceIgnoreUnresolvedTransactions(); }@b@ @b@ public void setForceIgnoreUnresolvedTransactions( boolean forceIgnoreUnresolvedTransactions )@b@ { @b@ wcpds.setForceIgnoreUnresolvedTransactions( forceIgnoreUnresolvedTransactions ); @b@ this.resetPoolManager( false );@b@ }@b@ @b@ public int getIdleConnectionTestPeriod()@b@ { return wcpds.getIdleConnectionTestPeriod(); }@b@ @b@ public void setIdleConnectionTestPeriod( int idleConnectionTestPeriod )@b@ { @b@ wcpds.setIdleConnectionTestPeriod( idleConnectionTestPeriod ); @b@ this.resetPoolManager( false );@b@ }@b@ @b@ public int getInitialPoolSize()@b@ { return wcpds.getInitialPoolSize(); }@b@ @b@ public void setInitialPoolSize( int initialPoolSize )@b@ { @b@ wcpds.setInitialPoolSize( initialPoolSize ); @b@ this.resetPoolManager( false );@b@ }@b@ @b@ public int getMaxIdleTime()@b@ { return wcpds.getMaxIdleTime(); }@b@ @b@ public void setMaxIdleTime( int maxIdleTime )@b@ { @b@ wcpds.setMaxIdleTime( maxIdleTime ); @b@ this.resetPoolManager( false );@b@ }@b@ @b@ public int getMaxPoolSize()@b@ { return wcpds.getMaxPoolSize(); }@b@ @b@ public void setMaxPoolSize( int maxPoolSize )@b@ { @b@ wcpds.setMaxPoolSize( maxPoolSize ); @b@ this.resetPoolManager( false );@b@ }@b@ @b@ public int getMaxStatements()@b@ { return wcpds.getMaxStatements(); }@b@ @b@ public void setMaxStatements( int maxStatements )@b@ { @b@ wcpds.setMaxStatements( maxStatements ); @b@ this.resetPoolManager( false );@b@ }@b@ @b@ public int getMaxStatementsPerConnection()@b@ { return wcpds.getMaxStatementsPerConnection(); }@b@ @b@ public void setMaxStatementsPerConnection( int maxStatementsPerConnection )@b@ { @b@ wcpds.setMaxStatementsPerConnection( maxStatementsPerConnection ); @b@ this.resetPoolManager( false );@b@ }@b@ @b@ public int getMinPoolSize()@b@ { return wcpds.getMinPoolSize(); }@b@ @b@ public void setMinPoolSize( int minPoolSize )@b@ { @b@ wcpds.setMinPoolSize( minPoolSize ); @b@ this.resetPoolManager( false );@b@ }@b@ @b@ public String getOverrideDefaultUser()@b@ { return wcpds.getOverrideDefaultUser(); }@b@ @b@ public void setOverrideDefaultUser(String overrideDefaultUser)@b@ { @b@ wcpds.setOverrideDefaultUser( overrideDefaultUser ); @b@ this.resetPoolManager( false );@b@ }@b@ @b@ public String getOverrideDefaultPassword()@b@ { return wcpds.getOverrideDefaultPassword(); }@b@ @b@ public void setOverrideDefaultPassword(String overrideDefaultPassword)@b@ { @b@ wcpds.setOverrideDefaultPassword( overrideDefaultPassword ); @b@ this.resetPoolManager( false );@b@ }@b@ @b@ public int getPropertyCycle()@b@ { return wcpds.getPropertyCycle(); }@b@ @b@ public void setPropertyCycle( int propertyCycle )@b@ { @b@ wcpds.setPropertyCycle( propertyCycle ); @b@ this.resetPoolManager( false );@b@ }@b@ @b@ public boolean isBreakAfterAcquireFailure()@b@ { return wcpds.isBreakAfterAcquireFailure(); }@b@ @b@ public void setBreakAfterAcquireFailure( boolean breakAfterAcquireFailure )@b@ { @b@ wcpds.setBreakAfterAcquireFailure( breakAfterAcquireFailure ); @b@ this.resetPoolManager( false );@b@ }@b@ @b@ public boolean isTestConnectionOnCheckout()@b@ { return wcpds.isTestConnectionOnCheckout(); }@b@ @b@ public void setTestConnectionOnCheckout( boolean testConnectionOnCheckout )@b@ { @b@ wcpds.setTestConnectionOnCheckout( testConnectionOnCheckout ); @b@ this.resetPoolManager( false );@b@ }@b@ @b@ public boolean isTestConnectionOnCheckin()@b@ { return wcpds.isTestConnectionOnCheckin(); }@b@ @b@ public void setTestConnectionOnCheckin( boolean testConnectionOnCheckin )@b@ { @b@ wcpds.setTestConnectionOnCheckin( testConnectionOnCheckin ); @b@ this.resetPoolManager( false );@b@ }@b@ @b@ public boolean isUsesTraditionalReflectiveProxies()@b@ { return wcpds.isUsesTraditionalReflectiveProxies(); }@b@ @b@ public void setUsesTraditionalReflectiveProxies( boolean usesTraditionalReflectiveProxies )@b@ { @b@ wcpds.setUsesTraditionalReflectiveProxies( usesTraditionalReflectiveProxies ); @b@ this.resetPoolManager( false );@b@ }@b@ @b@ public String getPreferredTestQuery()@b@ { return wcpds.getPreferredTestQuery(); }@b@ @b@ public void setPreferredTestQuery( String preferredTestQuery )@b@ { @b@ wcpds.setPreferredTestQuery( preferredTestQuery ); @b@ this.resetPoolManager( false );@b@ }@b@ @b@ public String getUserOverridesAsString()@b@ { return wcpds.getUserOverridesAsString(); }@b@ @b@ public void setUserOverridesAsString( String userOverridesAsString ) throws PropertyVetoException@b@ { @b@ wcpds.setUserOverridesAsString( userOverridesAsString ); @b@ this.resetPoolManager( false );@b@ }@b@ @b@ public int getMaxAdministrativeTaskTime()@b@ { return wcpds.getMaxAdministrativeTaskTime(); }@b@ @b@ public void setMaxAdministrativeTaskTime( int maxAdministrativeTaskTime )@b@ { @b@ wcpds.setMaxAdministrativeTaskTime( maxAdministrativeTaskTime ); @b@ this.resetPoolManager( false );@b@ }@b@ @b@ public int getMaxIdleTimeExcessConnections()@b@ { return wcpds.getMaxIdleTimeExcessConnections(); }@b@ @b@ public void setMaxIdleTimeExcessConnections( int maxIdleTimeExcessConnections )@b@ { @b@ wcpds.setMaxIdleTimeExcessConnections( maxIdleTimeExcessConnections ); @b@ this.resetPoolManager( false );@b@ }@b@ @b@ public int getMaxConnectionAge()@b@ { return wcpds.getMaxConnectionAge(); }@b@ @b@ public void setMaxConnectionAge( int maxConnectionAge )@b@ { @b@ wcpds.setMaxConnectionAge( maxConnectionAge ); @b@ this.resetPoolManager( false );@b@ }@b@ @b@ public String getConnectionCustomizerClassName()@b@ { return wcpds.getConnectionCustomizerClassName(); }@b@ @b@ public void setConnectionCustomizerClassName( String connectionCustomizerClassName )@b@ { @b@ wcpds.setConnectionCustomizerClassName( connectionCustomizerClassName ); @b@ this.resetPoolManager( false );@b@ }@b@ @b@ public int getUnreturnedConnectionTimeout()@b@ { return wcpds.getUnreturnedConnectionTimeout(); }@b@ @b@ public void setUnreturnedConnectionTimeout(int unreturnedConnectionTimeout)@b@ {@b@ wcpds.setUnreturnedConnectionTimeout( unreturnedConnectionTimeout ); @b@ this.resetPoolManager( false );@b@ }@b@ @b@ public boolean isDebugUnreturnedConnectionStackTraces()@b@ { return wcpds.isDebugUnreturnedConnectionStackTraces(); }@b@ @b@ public void setDebugUnreturnedConnectionStackTraces(boolean debugUnreturnedConnectionStackTraces)@b@ {@b@ wcpds.setDebugUnreturnedConnectionStackTraces( debugUnreturnedConnectionStackTraces ); @b@ this.resetPoolManager( false );@b@ }@b@ @b@ // shared properties (count: 1)@b@ public String getFactoryClassLocation()@b@ { return super.getFactoryClassLocation(); }@b@ @b@ public void setFactoryClassLocation( String factoryClassLocation )@b@ {@b@ dmds.setFactoryClassLocation( factoryClassLocation );@b@ wcpds.setFactoryClassLocation( factoryClassLocation );@b@ super.setFactoryClassLocation( factoryClassLocation );@b@ }@b@ @b@ public String toString()@b@ {@b@ //System.err.println("ComboPooledDataSource.toString()");@b@ @b@ StringBuffer sb = new StringBuffer(512);@b@ sb.append( this.getClass().getName() );@b@ sb.append(" [ ");@b@ try { BeansUtils.appendPropNamesAndValues(sb, this, TO_STRING_IGNORE_PROPS); }@b@ catch (Exception e)@b@ { @b@ sb.append( e.toString() ); @b@ //e.printStackTrace();@b@ }@b@ sb.append(" ]");@b@ @b@ @b@ return sb.toString();@b@ }@b@ @b@ // serialization stuff -- set up bound/constrained property event handlers on deserialization@b@ private static final long serialVersionUID = 1;@b@ private static final short VERSION = 0x0001;@b@ @b@ private void writeObject( ObjectOutputStream oos ) throws IOException@b@ {@b@ oos.writeShort( VERSION );@b@ }@b@ @b@ private void readObject( ObjectInputStream ois ) throws IOException, ClassNotFoundException@b@ {@b@ short version = ois.readShort();@b@ switch (version)@b@ {@b@ case VERSION:@b@ updateLocalVarsFromCpdsProp();@b@ setUpPropertyEvents();@b@ break;@b@ default:@b@ throw new IOException("Unsupported Serialized Version: " + version);@b@ }@b@ }@b@}
DriverManagerDataSourceFactory类
package com.mchange.v2.c3p0;@b@@b@import java.beans.PropertyChangeEvent;@b@import java.sql.SQLException;@b@import java.util.Properties;@b@import javax.sql.DataSource; @b@ @b@public final class DriverManagerDataSourceFactory@b@{@b@ @b@ public static DataSource create(String driverClass,@b@ String jdbcUrl, @b@ String dfltUser, @b@ String dfltPassword, @b@ String refFactoryLoc)@b@ throws SQLException@b@ { @b@ DriverManagerDataSource out = new DriverManagerDataSource();@b@ out.setDriverClass( driverClass );@b@ out.setJdbcUrl( jdbcUrl );@b@ out.setUser( dfltUser );@b@ out.setPassword( dfltPassword );@b@ out.setFactoryClassLocation( refFactoryLoc );@b@ return out;@b@ }@b@ @b@ public static DataSource create(String driverClass,@b@ String jdbcUrl, @b@ Properties props,@b@ String refFactoryLoc)@b@ throws SQLException@b@ { @b@ DriverManagerDataSource out = new DriverManagerDataSource();@b@ out.setDriverClass( driverClass );@b@ out.setJdbcUrl( jdbcUrl );@b@ out.setProperties( props );@b@ out.setFactoryClassLocation( refFactoryLoc );@b@ return out;@b@ } @b@@b@ @b@ public static DataSource create(String driverClass,@b@ String jdbcUrl, @b@ String dfltUser, @b@ String dfltPassword)@b@ throws SQLException@b@ { return create( driverClass, jdbcUrl, dfltUser, dfltPassword, null ); }@b@@b@ @b@ public static DataSource create(String driverClass, String jdbcUrl)@b@ throws SQLException@b@ { return DriverManagerDataSourceFactory.create( driverClass, jdbcUrl, (String) null, null); }@b@@b@ @b@ public static DataSource create(String jdbcUrl, String dfltUser, String dfltPassword)@b@ throws SQLException@b@ { return DriverManagerDataSourceFactory.create( null, jdbcUrl, dfltUser, dfltPassword ); }@b@@b@ @b@ public static DataSource create(String jdbcUrl)@b@ throws SQLException@b@ { return DriverManagerDataSourceFactory.create( null, jdbcUrl, (String) null, null ); }@b@@b@ private DriverManagerDataSourceFactory()@b@ {}@b@}
四、相关下载
c3p0-0.9.1.1.jar,点击下载
c3p0-0.9.1.2.jar , 点击下载