在C3p0构建时,有驱动相关信息及数据库连接池相关的属性设置,及连接的获取,今天我们先来看一下,驱动相关信息及数据库连接池相关的属性设置,在连接的获取。
从下面几句开始,
Java代码
- cpDSource = new ComboPooledDataSource();
- //设置DriverManagerDataSource驱动相关信息
- cpDSource.setDriverClass(props.getProperty("driver"));
- cpDSource.setJdbcUrl(props.getProperty("url"));
- cpDSource.setUser(props.getProperty("user"));
- cpDSource.setPassword(props.getProperty("password"));
//AbstractComboPooledDataSource的两个关键内部成员DriverManagerDataSource,WrapperConnectionPoolDataSource
Java代码 下载
- public AbstractComboPooledDataSource(boolean autoregister)
- {
- //
- super(autoregister);
- //新建驱动数据源管理器
- dmds = new DriverManagerDataSource();
- //新建数据库连接池
- wcpds = new WrapperConnectionPoolDataSource();
- //设置数据连接池的数据源驱动管理器
- wcpds.setNestedDataSource(dmds);
- try
- {
- setConnectionPoolDataSource(wcpds);
- }
- catch(PropertyVetoException e)
- {
- logger.log(MLevel.WARNING, "Hunh??? This can't happen. We haven't set up any listeners to veto the property change yet!", e);
- throw new RuntimeException((new StringBuilder()).append("Hunh??? This can't happen. We haven't set up any listeners to veto the property change yet! ").append(e).toString());
- }
- setUpPropertyEvents();
- }
- }
- //设置driverClass
- public void setDriverClass(String driverClass)
- throws PropertyVetoException
- {
- dmds.setDriverClass(driverClass);
- }
- //设置jdbcUrl
- public void setJdbcUrl(String jdbcUrl)
- {
- if(diff(dmds.getJdbcUrl(), jdbcUrl))
- {
- dmds.setJdbcUrl(jdbcUrl);
- resetPoolManager(false);
- }
- }
- //设置user
- public void setUser(String user)
- {
- if(diff(dmds.getUser(), user))
- {
- dmds.setUser(user);
- resetPoolManager(false);
- }
- }
- //设置password
- public void setPassword(String password)
- {
- if(diff(dmds.getPassword(), password))
- {
- dmds.setPassword(password);
- resetPoolManager(false);
- }
- }
//设置WrapperConnectionPoolDataSource相关属性
Java代码 下载
- cpDSource.setInitialPoolSize(5);
- cpDSource.setMaxPoolSize(30);
- cpDSource.setMinPoolSize(5);
- cpDSource.setMaxStatements(100);
- cpDSource.setIdleConnectionTestPeriod(60);
- cpDSource.setBreakAfterAcquireFailure(false);
- cpDSource.setAcquireRetryAttempts(30);
- cpDSource.setTestConnectionOnCheckout(false);
//设置连接失败尝试连接数
Java代码
- public void setAcquireRetryAttempts(int acquireRetryAttempts)
- {
- if(diff(wcpds.getAcquireRetryAttempts(), acquireRetryAttempts))
- {
- wcpds.setAcquireRetryAttempts(acquireRetryAttempts);
- resetPoolManager(false);
- }
- }
- public int getAcquireRetryDelay()
- {
- return wcpds.getAcquireRetryDelay();
- }
- public void setAcquireRetryDelay(int acquireRetryDelay)
- {
- if(diff(wcpds.getAcquireRetryDelay(), acquireRetryDelay))
- {
- wcpds.setAcquireRetryDelay(acquireRetryDelay);
- resetPoolManager(false);
- }
- }
- public boolean isAutoCommitOnClose()
- {
- return wcpds.isAutoCommitOnClose();
- }
- //设置是否自动提交
- public void setAutoCommitOnClose(boolean autoCommitOnClose)
- {
- if(diff(wcpds.isAutoCommitOnClose(), autoCommitOnClose))
- {
- wcpds.setAutoCommitOnClose(autoCommitOnClose);
- resetPoolManager(false);
- }
- }
- public int getInitialPoolSize()
- {
- return wcpds.getInitialPoolSize();
- }
- //连接池初始化大小
- public void setInitialPoolSize(int initialPoolSize)
- {
- if(diff(wcpds.getInitialPoolSize(), initialPoolSize))
- {
- wcpds.setInitialPoolSize(initialPoolSize);
- resetPoolManager(false);
- }
- }
- public int getMaxIdleTime()
- {
- return wcpds.getMaxIdleTime();
- }
- //maxIdleTime
- public void setMaxIdleTime(int maxIdleTime)
- {
- if(diff(wcpds.getMaxIdleTime(), maxIdleTime))
- {
- wcpds.setMaxIdleTime(maxIdleTime);
- resetPoolManager(false);
- }
- }
- //maxPoolSize
- public void setMaxPoolSize(int maxPoolSize)
- {
- if(diff(wcpds.getMaxPoolSize(), maxPoolSize))
- {
- wcpds.setMaxPoolSize(maxPoolSize);
- resetPoolManager(false);
- }
- }
- //maxStatements
- public void setMaxStatements(int maxStatements)
- {
- if(diff(wcpds.getMaxStatements(), maxStatements))
- {
- wcpds.setMaxStatements(maxStatements);
- resetPoolManager(false);
- }
- }
从上面可以看出cpDSource初始化driver相关属性,是初始化数据源驱动管理器DriverManagerDataSource的属性;初始化poolConnection相关属性,是初始化数据库连接池包装类WrapperConnectionPoolDataSource的属性。
再看连接的获取,从下面一句开始,
Java代码 下载
- con = cpDSource.getConnection();
此方法在ComboPooledDataSource和其父类中都没,追溯到AbstractComboPooledDataSource的
父类AbstractPoolBackedDataSource
//AbstractPoolBackedDataSource
Java代码
- public abstract class AbstractPoolBackedDataSource extends PoolBackedDataSourceBase
- implements PooledDataSource
- {
- public Connection getConnection()
- throws SQLException
- {
- //获取数据库连接池管理器
- PooledConnection pc = getPoolManager().getPool().checkoutPooledConnection();
- //从数据库连接池,返回数据库连接
- return pc.getConnection();
- }
- }
先看获取数据库连接池管理器
//获取数据库连接池管理器
PooledConnection pc = getPoolManager().getPool().checkoutPooledConnection();
//获取数据库连接池管理器
Java代码
- private synchronized C3P0PooledConnectionPoolManager getPoolManager()
- throws SQLException
- {
- if(poolManager == null)
- {
- //获取数据源数据库连接池
- ConnectionPoolDataSource cpds = assertCpds();
- //构建数据库连接池管理器
- poolManager = new C3P0PooledConnectionPoolManager(cpds, null, null, getNumHelperThreads(), getIdentityToken(), getDataSourceName());
- if(logger.isLoggable(MLevel.INFO))
- logger.info((new StringBuilder()).append("Initializing c3p0 pool... ").append(toString(true)).toString());
- }
- return poolManager;
- }
- //确定数据源数据库连接池
- private synchronized ConnectionPoolDataSource assertCpds()
- throws SQLException
- {
- if(is_closed)
- throw new SQLException((new StringBuilder()).append(this).append(" has been closed() -- you can no longer use it.").toString());
- //获取数据源数据库连接池
- ConnectionPoolDataSource out = getConnectionPoolDataSource();
- if(out == null)
- throw new SQLException("Attempted to use an uninitialized PoolBackedDataSource. Please call setConnectionPoolDataSource( ... ) to initialize.");
- else
- return out;
- } 下载
//PoolBackedDataSourceBase
Java代码
- public class PoolBackedDataSourceBase extends IdentityTokenResolvable
- implements Referenceable, Serializable
- {
- //获取数据源数据库连接池
- public synchronized ConnectionPoolDataSource getConnectionPoolDataSource()
- {
- return connectionPoolDataSource;
- }
- public synchronized void setConnectionPoolDataSource(ConnectionPoolDataSource connectionPoolDataSource)
- throws PropertyVetoException
- {
- ConnectionPoolDataSource oldVal = this.connectionPoolDataSource;
- if(!eqOrBothNull(oldVal, connectionPoolDataSource))
- vcs.fireVetoableChange("connectionPoolDataSource", oldVal, connectionPoolDataSource);
- this.connectionPoolDataSource = connectionPoolDataSource;
- if(!eqOrBothNull(oldVal, connectionPoolDataSource))
- pcs.firePropertyChange("connectionPoolDataSource", oldVal, connectionPoolDataSource);
- }
- }
这个数据源数据连接池是什么呢?还记得我们前面,有讲过AbstractComboPooledDataSource的构造有这么一段
Java代码
- public AbstractComboPooledDataSource(boolean autoregister)
- {
- super(autoregister);
- dmds = new DriverManagerDataSource();
- wcpds = new WrapperConnectionPoolDataSource();
- wcpds.setNestedDataSource(dmds);
- try
- {
- //设置数据源数据库连接池为WrapperConnectionPoolDataSource
- setConnectionPoolDataSource(wcpds);
- }
- }
现在回到getPoolManager的构建数据库连接池管理器这一句
Java代码
- poolManager = new C3P0PooledConnectionPoolManager(cpds, null, null, getNumHelperThreads(), getIdentityToken(), getDataSourceName());
//C3P0PooledConnectionPoolManager
Java代码 下载
- public final class C3P0PooledConnectionPoolManager
- {
- private static final boolean POOL_EVENT_SUPPORT = false;
- private static final CoalesceChecker COALESCE_CHECKER;
- static final Coalescer COALESCER;
- static final int DFLT_NUM_TASK_THREADS_PER_DATA_SOURCE = 3;
- ThreadPoolAsynchronousRunner taskRunner;//
- ThreadPoolAsynchronousRunner deferredStatementDestroyer;
- Timer timer;
- ResourcePoolFactory rpfact;
- Map authsToPools;
- final ConnectionPoolDataSource cpds;
- final Map propNamesToReadMethods;
- final Map flatPropertyOverrides;
- final Map userOverrides;
- final DbAuth defaultAuth;
- final String parentDataSourceIdentityToken;
- final String parentDataSourceName;
- int num_task_threads;
- static
- {
- COALESCE_CHECKER = IdentityTokenizedCoalesceChecker.INSTANCE;
- COALESCER = CoalescerFactory.createCoalescer(COALESCE_CHECKER, true, false);
- }
- //初始化C3P0PooledConnectionPoolManager,cpds为WrapperConnectionPoolDataSource
- public C3P0PooledConnectionPoolManager(ConnectionPoolDataSource cpds, Map flatPropertyOverrides, Map forceUserOverrides, int num_task_threads, String parentDataSourceIdentityToken, String parentDataSourceName)
- throws SQLException
- {
- //任务线程数
- this.num_task_threads = 3;
- try
- {
- this.cpds = cpds;//初始化数据库连接池
- this.flatPropertyOverrides = flatPropertyOverrides;
- this.num_task_threads = num_task_threads;
- this.parentDataSourceIdentityToken = parentDataSourceIdentityToken;
- this.parentDataSourceName = parentDataSourceName;
- DbAuth auth = null;
- if(flatPropertyOverrides != null)
- {
- String overrideUser = (String)flatPropertyOverrides.get("overrideDefaultUser");
- String overridePassword = (String)flatPropertyOverrides.get("overrideDefaultPassword");
- if(overrideUser == null)
- {
- overrideUser = (String)flatPropertyOverrides.get("user");
- overridePassword = (String)flatPropertyOverrides.get("password");
- }
- if(overrideUser != null)
- auth = new DbAuth(overrideUser, overridePassword);
- }
- if(auth == null)
- //初始化数据库验证
- auth = C3P0ImplUtils.findAuth(cpds);
- defaultAuth = auth;
- Map tmp = new HashMap();
- BeanInfo bi = Introspector.getBeanInfo(cpds.getClass());
- PropertyDescriptor pds[] = bi.getPropertyDescriptors();
- PropertyDescriptor pd = null;
- int i = 0;
- for(int len = pds.length; i < len; i++)
- {
- pd = pds[i];
- String name = pd.getName();
- Method m = pd.getReadMethod();
- if(m != null)
- tmp.put(name, m);
- }
- propNamesToReadMethods = tmp;
- if(forceUserOverrides == null)
- {
- Method uom = (Method)propNamesToReadMethods.get("userOverridesAsString");
- if(uom != null)
- {
- String uoas = (String)uom.invoke(cpds, (Object[])null);
- Map uo = C3P0ImplUtils.parseUserOverridesAsString(uoas);
- userOverrides = uo;
- } else
- {
- userOverrides = Collections.EMPTY_MAP;
- }
- } else
- {
- userOverrides = forceUserOverrides;
- }
- poolsInit();
- }
- catch(Exception e)
- {
- logger.log(MLevel.FINE, null, e);
- throw SqlUtils.toSQLException(e);
- }
- }
- } 下载
- //连接池初始化
- private void poolsInit()
- {
- boolean privilege_spawned_threads = getPrivilegeSpawnedThreads();
- String contextClassLoaderSource = getContextClassLoaderSource();
- class _cls1ContextClassLoaderPoolsInitThread extends Thread
- {
- public void run()
- {
- //
- maybePrivilegedPoolsInit(privilege_spawned_threads);
- }
- final boolean val$privilege_spawned_threads;
- final C3P0PooledConnectionPoolManager this$0;
- _cls1ContextClassLoaderPoolsInitThread(boolean flag)
- {
- this.this$0 = C3P0PooledConnectionPoolManager.this;
- privilege_spawned_threads = flag;
- super();
- setContextClassLoader(ccl);
- }
- }
- try
- {
- if("library".equalsIgnoreCase(contextClassLoaderSource))
- {
- //
- Thread t = new _cls1ContextClassLoaderPoolsInitThread(privilege_spawned_threads);
- t.start();
- t.join();
- } else
- if("none".equalsIgnoreCase(contextClassLoaderSource))
- {
- Thread t = new _cls1ContextClassLoaderPoolsInitThread(privilege_spawned_threads);
- t.start();
- t.join();
- } else
- {
- if(logger.isLoggable(MLevel.WARNING) && !"caller".equalsIgnoreCase(contextClassLoaderSource))
- logger.log(MLevel.WARNING, (new StringBuilder()).append("Unknown contextClassLoaderSource: ").append(contextClassLoaderSource).append(" -- should be 'caller', 'library', or 'none'. Using default value 'caller'.").toString());
- maybePrivilegedPoolsInit(privilege_spawned_threads);
- }
- }
- }
- private void maybePrivilegedPoolsInit(boolean privilege_spawned_threads)
- {
- if(privilege_spawned_threads)
- {
- PrivilegedAction privilegedPoolsInit = new PrivilegedAction() {
- public Void run()
- {
- //委托给_poolsInit
- _poolsInit();
- return null;
- }
- public volatile Object run()
- {
- return run();
- }
- final C3P0PooledConnectionPoolManager this$0;
- {
- this.this$0 = C3P0PooledConnectionPoolManager.this;
- super();
- }
- };
- AccessController.doPrivileged(privilegedPoolsInit);
- } else
- {
- _poolsInit();
- }
- }
- //终于找个了poolsInit的关键,初始化定时任务调度器,及死锁检测线程,及延时死锁检测线程
- private synchronized void _poolsInit()
- {
- String idStr = idString();
- timer = new Timer((new StringBuilder()).append(idStr).append("-AdminTaskTimer").toString(), true);
- int matt = getMaxAdministrativeTaskTime();
- //创建任务线程调度器
- taskRunner = createTaskRunner(num_task_threads, matt, timer, (new StringBuilder()).append(idStr).append("-HelperThread").toString());
- int num_deferred_close_threads = getStatementCacheNumDeferredCloseThreads();
- if(num_deferred_close_threads > 0)
- deferredStatementDestroyer = createTaskRunner(num_deferred_close_threads, matt, timer, (new StringBuilder()).append(idStr).append("-DeferredStatementDestroyerThread").toString());
- else
- deferredStatementDestroyer = null;
- rpfact = BasicResourcePoolFactory.createNoEventSupportInstance(taskRunner, timer);
- authsToPools = new HashMap();
- }
从上面可以看出getPoolManager()实际上,是初始化数据库连接池管理器C3P0PooledConnectionPoolManager,初始化C3P0PooledConnectionPoolManager的