粗略的看了下源码,发现数据库池pool对象在应用中没有使用static关键字,我的理解是一般情况下pool对象应该只有一个(除非使用多种数据库,就创建多个pool),如果一个类属性中有pool对象,不使用static,那new一个对象不是就多了一个pool对象吗?还有在多用户访问数据库的时候,是多个Connection对象还是只有一个Connection对应多个用户,我听人说只有一个Connection对象,那pool里面 new 那么多Connection是怎么回事。
头痛啊!源码太多,手按鼠标都按疼了,哪位大牛能解答我的疑惑,跪谢了!
头痛啊!源码太多,手按鼠标都按疼了,哪位大牛能解答我的疑惑,跪谢了!
public GenericObjectPoolFactory(PoolableObjectFactory factory, int maxActive, byte whenExhaustedAction, long maxWait, int maxIdle, int minIdle, boolean testOnBorrow, boolean testOnReturn, long timeBetweenEvictionRunsMillis, int numTestsPerEvictionRun, long minEvictableIdleTimeMillis, boolean testWhileIdle) {
this(factory,maxActive,whenExhaustedAction,maxWait,maxIdle,minIdle,testOnBorrow,testOnReturn,timeBetweenEvictionRunsMillis,numTestsPerEvictionRun,minEvictableIdleTimeMillis,testWhileIdle, GenericObjectPool.DEFAULT_SOFT_MIN_EVICTABLE_IDLE_TIME_MILLIS);
}
public GenericObjectPoolFactory(PoolableObjectFactory factory, int maxActive, byte whenExhaustedAction, long maxWait, int maxIdle, int minIdle, boolean testOnBorrow, boolean testOnReturn, long timeBetweenEvictionRunsMillis, int numTestsPerEvictionRun, long minEvictableIdleTimeMillis, boolean testWhileIdle, long softMinEvictableIdleTimeMillis) {
this(factory,maxActive,whenExhaustedAction,maxWait,maxIdle,minIdle,testOnBorrow,testOnReturn,timeBetweenEvictionRunsMillis,numTestsPerEvictionRun,minEvictableIdleTimeMillis,testWhileIdle,softMinEvictableIdleTimeMillis, GenericObjectPool.DEFAULT_LIFO);
}
returnObject. public GenericObjectPoolFactory(PoolableObjectFactory factory, int maxActive, byte whenExhaustedAction, long maxWait, int maxIdle, int minIdle, boolean testOnBorrow, boolean testOnReturn, long timeBetweenEvictionRunsMillis, int numTestsPerEvictionRun, long minEvictableIdleTimeMillis, boolean testWhileIdle, long softMinEvictableIdleTimeMillis, boolean lifo) {
_maxIdle = maxIdle;
_minIdle = minIdle;
_maxActive = maxActive;
_maxWait = maxWait;
_whenExhaustedAction = whenExhaustedAction;
_testOnBorrow = testOnBorrow;
_testOnReturn = testOnReturn;
_testWhileIdle = testWhileIdle;
_timeBetweenEvictionRunsMillis = timeBetweenEvictionRunsMillis;
_numTestsPerEvictionRun = numTestsPerEvictionRun;
_minEvictableIdleTimeMillis = minEvictableIdleTimeMillis;
_softMinEvictableIdleTimeMillis = softMinEvictableIdleTimeMillis;
_lifo = lifo;
_factory = factory;
} public ObjectPool createPool() {
return new GenericObjectPool(_factory,_maxActive,_whenExhaustedAction,_maxWait,_maxIdle,_minIdle,_testOnBorrow,_testOnReturn,_timeBetweenEvictionRunsMillis,_numTestsPerEvictionRun,_minEvictableIdleTimeMillis,_testWhileIdle,_softMinEvictableIdleTimeMillis,_lifo);
} protected int _maxIdle = GenericObjectPool.DEFAULT_MAX_IDLE;
protected int _minIdle = GenericObjectPool.DEFAULT_MIN_IDLE;
protected int _maxActive = GenericObjectPool.DEFAULT_MAX_ACTIVE;
protected long _maxWait = GenericObjectPool.DEFAULT_MAX_WAIT;
protected byte _whenExhaustedAction = GenericObjectPool.DEFAULT_WHEN_EXHAUSTED_ACTION;
protected boolean _testOnBorrow = GenericObjectPool.DEFAULT_TEST_ON_BORROW;
protected boolean _testOnReturn = GenericObjectPool.DEFAULT_TEST_ON_RETURN;
protected boolean _testWhileIdle = GenericObjectPool.DEFAULT_TEST_WHILE_IDLE;
protected long _timeBetweenEvictionRunsMillis = GenericObjectPool.DEFAULT_TIME_BETWEEN_EVICTION_RUNS_MILLIS;
protected int _numTestsPerEvictionRun = GenericObjectPool.DEFAULT_NUM_TESTS_PER_EVICTION_RUN;
protected long _minEvictableIdleTimeMillis = GenericObjectPool.DEFAULT_MIN_EVICTABLE_IDLE_TIME_MILLIS;
protected long _softMinEvictableIdleTimeMillis = GenericObjectPool.DEFAULT_MIN_EVICTABLE_IDLE_TIME_MILLIS;
protected boolean _lifo = GenericObjectPool.DEFAULT_LIFO;
protected PoolableObjectFactory _factory = null;
这个是源码,还有Factory类创建pool
2:数据库服务端会主动断开已经连了很长时间的连接,连接池必须时刻检查池中连接的健康状态,如果有不健康的连接需要抛弃,或者重连
3:连接池一般有个最大连接数,当达到最大连接数时,使用者再次请求连接,这时连接池需要做些什么事情?
4:连接池可以通过配置将一些空闲了很久没有用到的连接主动关闭与数据库的通信连接
这是个人理解哈,不代表什么...