您好,登錄后才能下訂單哦!
在C3p0構(gòu)建時(shí),有驅(qū)動(dòng)相關(guān)信息及數(shù)據(jù)庫(kù)連接池相關(guān)的屬性設(shè)置,及連接的獲取,今天我們先來(lái)看一下,驅(qū)動(dòng)相關(guān)信息及數(shù)據(jù)庫(kù)連接池相關(guān)的屬性設(shè)置,在連接的獲取。
從下面幾句開(kāi)始,
Java代碼
cpDSource = new ComboPooledDataSource();
//設(shè)置DriverManagerDataSource驅(qū)動(dòng)相關(guān)信息
cpDSource.setDriverClass(props.getProperty("driver"));
cpDSource.setJdbcUrl(props.getProperty("url"));
cpDSource.setUser(props.getProperty("user"));
cpDSource.setPassword(props.getProperty("password"));
//AbstractComboPooledDataSource的兩個(gè)關(guān)鍵內(nèi)部成員DriverManagerDataSource,WrapperConnectionPoolDataSource
Java代碼下載
public AbstractComboPooledDataSource(boolean autoregister)
{
//
super(autoregister);
//新建驅(qū)動(dòng)數(shù)據(jù)源管理器
dmds = new DriverManagerDataSource();
//新建數(shù)據(jù)庫(kù)連接池
wcpds = new WrapperConnectionPoolDataSource();
//設(shè)置數(shù)據(jù)連接池的數(shù)據(jù)源驅(qū)動(dòng)管理器
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();
}
}
//設(shè)置driverClass
public void setDriverClass(String driverClass)
throws PropertyVetoException
{
dmds.setDriverClass(driverClass);
}
//設(shè)置jdbcUrl
public void setJdbcUrl(String jdbcUrl)
{
if(diff(dmds.getJdbcUrl(), jdbcUrl))
{
dmds.setJdbcUrl(jdbcUrl);
resetPoolManager(false);
}
}
//設(shè)置user
public void setUser(String user)
{
if(diff(dmds.getUser(), user))
{
dmds.setUser(user);
resetPoolManager(false);
}
}
//設(shè)置password
public void setPassword(String password)
{
if(diff(dmds.getPassword(), password))
{
dmds.setPassword(password);
resetPoolManager(false);
}
}
//設(shè)置WrapperConnectionPoolDataSource相關(guān)屬性
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);
//設(shè)置連接失敗嘗試連接數(shù)
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();
}
//設(shè)置是否自動(dòng)提交
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相關(guān)屬性,是初始化數(shù)據(jù)源驅(qū)動(dòng)管理器DriverManagerDataSource的屬性;初始化poolConnection相關(guān)屬性,是初始化數(shù)據(jù)庫(kù)連接池包裝類(lèi)WrapperConnectionPoolDataSource的屬性。
再看連接的獲取,從下面一句開(kāi)始,
Java代碼
con = cpDSource.getConnection();
此方法在ComboPooledDataSource和其父類(lèi)中都沒(méi),追溯到AbstractComboPooledDataSource的
父類(lèi)AbstractPoolBackedDataSource
//AbstractPoolBackedDataSource
Java代碼
public abstract class AbstractPoolBackedDataSource extends PoolBackedDataSourceBase
implements PooledDataSource
{
public Connection getConnection()
throws SQLException
{
//獲取數(shù)據(jù)庫(kù)連接池管理器
PooledConnection pc = getPoolManager().getPool().checkoutPooledConnection();
//從數(shù)據(jù)庫(kù)連接池,返回?cái)?shù)據(jù)庫(kù)連接
return pc.getConnection();
}
}
先看獲取數(shù)據(jù)庫(kù)連接池管理器
//獲取數(shù)據(jù)庫(kù)連接池管理器
PooledConnection pc = getPoolManager().getPool().checkoutPooledConnection();
//獲取數(shù)據(jù)庫(kù)連接池管理器
下載
Java代碼
private synchronized C3P0PooledConnectionPoolManager getPoolManager()
throws SQLException
{
if(poolManager == null)
{
//獲取數(shù)據(jù)源數(shù)據(jù)庫(kù)連接池
ConnectionPoolDataSource cpds = assertCpds();
//構(gòu)建數(shù)據(jù)庫(kù)連接池管理器
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;
}
//確定數(shù)據(jù)源數(shù)據(jù)庫(kù)連接池
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());
//獲取數(shù)據(jù)源數(shù)據(jù)庫(kù)連接池
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
{
//獲取數(shù)據(jù)源數(shù)據(jù)庫(kù)連接池
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);
}
}
這個(gè)數(shù)據(jù)源數(shù)據(jù)連接池是什么呢?還記得我們前面,有講過(guò)AbstractComboPooledDataSource的構(gòu)造有這么一段
Java代碼下載
public AbstractComboPooledDataSource(boolean autoregister)
{
super(autoregister);
dmds = new DriverManagerDataSource();
wcpds = new WrapperConnectionPoolDataSource();
wcpds.setNestedDataSource(dmds);
try
{
//設(shè)置數(shù)據(jù)源數(shù)據(jù)庫(kù)連接池為WrapperConnectionPoolDataSource
setConnectionPoolDataSource(wcpds);
}
}
現(xiàn)在回到getPoolManager的構(gòu)建數(shù)據(jù)庫(kù)連接池管理器這一句
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
{
//任務(wù)線程數(shù)
this.num_task_threads = 3;
try
{
this.cpds = cpds;//初始化數(shù)據(jù)庫(kù)連接池
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)
//初始化數(shù)據(jù)庫(kù)驗(yàn)證
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();
}
}
//終于找個(gè)了poolsInit的關(guān)鍵,初始化定時(shí)任務(wù)調(diào)度器,及死鎖檢測(cè)線程,及延時(shí)死鎖檢測(cè)線程
private synchronized void _poolsInit()
{
String idStr = idString();
timer = new Timer((new StringBuilder()).append(idStr).append("-AdminTaskTimer").toString(), true);
int matt = getMaxAdministrativeTaskTime();
//創(chuàng)建任務(wù)線程調(diào)度器
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()實(shí)際上,是初始化數(shù)據(jù)庫(kù)連接池管理器C3P0PooledConnectionPoolManager,初始化C3P0PooledConnectionPoolManager的
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。