您好,登錄后才能下訂單哦!
在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的
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。