mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
refactor DruidDataSource
This commit is contained in:
+2
-2
@@ -32,7 +32,7 @@ import com.alibaba.druid.util.JdbcUtils;
|
||||
/**
|
||||
* @author wenshao<szujobs@hotmail.com>
|
||||
*/
|
||||
public final class ConnectionHolder {
|
||||
public final class DruidConnectionHolder {
|
||||
|
||||
private final DruidAbstractDataSource dataSource;
|
||||
private final Connection conn;
|
||||
@@ -58,7 +58,7 @@ public final class ConnectionHolder {
|
||||
private boolean underlyingAutoCommit;
|
||||
private final int modCount;
|
||||
|
||||
public ConnectionHolder(DruidAbstractDataSource dataSource, Connection conn) throws SQLException{
|
||||
public DruidConnectionHolder(DruidAbstractDataSource dataSource, Connection conn) throws SQLException{
|
||||
this.modCount = dataSource.getModCount();
|
||||
|
||||
this.dataSource = dataSource;
|
||||
@@ -99,7 +99,7 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
|
||||
private long poolingPeakTime = 0;
|
||||
|
||||
// store
|
||||
private volatile ConnectionHolder[] connections;
|
||||
private volatile DruidConnectionHolder[] connections;
|
||||
private int poolingCount = 0;
|
||||
private int activeCount = 0;
|
||||
private long discardCount = 0;
|
||||
@@ -231,7 +231,7 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
|
||||
try {
|
||||
if (this.poolPreparedStatements && (!value)) {
|
||||
for (int i = 0; i < poolingCount; ++i) {
|
||||
ConnectionHolder connection = connections[i];
|
||||
DruidConnectionHolder connection = connections[i];
|
||||
|
||||
for (PreparedStatementHolder holder : connection.getStatementPool().getMap().values()) {
|
||||
closePreapredStatement(holder);
|
||||
@@ -390,7 +390,7 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
|
||||
|
||||
dataSourceStat = new JdbcDataSourceStat(this.name, this.jdbcUrl, this.dbType);
|
||||
|
||||
connections = new ConnectionHolder[maxActive];
|
||||
connections = new DruidConnectionHolder[maxActive];
|
||||
|
||||
SQLException connectError = null;
|
||||
|
||||
@@ -401,7 +401,7 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
|
||||
if (defaultAutoCommit != conn.getAutoCommit()) {
|
||||
conn.setAutoCommit(defaultAutoCommit);
|
||||
}
|
||||
connections[poolingCount++] = new ConnectionHolder(this, conn);
|
||||
connections[poolingCount++] = new DruidConnectionHolder(this, conn);
|
||||
}
|
||||
} catch (SQLException ex) {
|
||||
LOG.error("init datasource error", ex);
|
||||
@@ -682,7 +682,7 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
|
||||
|
||||
connectCount++;
|
||||
|
||||
ConnectionHolder holder;
|
||||
DruidConnectionHolder holder;
|
||||
|
||||
if (maxWait > 0) {
|
||||
holder = pollLast(maxWait, TimeUnit.MILLISECONDS);
|
||||
@@ -715,7 +715,7 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
|
||||
}
|
||||
|
||||
public void handleConnectionException(DruidPooledConnection pooledConnection, Throwable t) throws SQLException {
|
||||
final ConnectionHolder holder = pooledConnection.getConnectionHolder();
|
||||
final DruidConnectionHolder holder = pooledConnection.getConnectionHolder();
|
||||
|
||||
errorCount.incrementAndGet();
|
||||
lastError = t;
|
||||
@@ -750,7 +750,7 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
|
||||
*/
|
||||
protected void recycle(DruidPooledConnection pooledConnection) throws SQLException {
|
||||
final Connection conn = pooledConnection.getConnection();
|
||||
final ConnectionHolder holder = pooledConnection.getConnectionHolder();
|
||||
final DruidConnectionHolder holder = pooledConnection.getConnectionHolder();
|
||||
|
||||
assert holder != null;
|
||||
|
||||
@@ -852,7 +852,7 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
|
||||
lock.lock();
|
||||
try {
|
||||
for (int i = 0; i < poolingCount; ++i) {
|
||||
ConnectionHolder conn = connections[i];
|
||||
DruidConnectionHolder conn = connections[i];
|
||||
conn.getStatementPool().clear();
|
||||
}
|
||||
} finally {
|
||||
@@ -884,7 +884,7 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
|
||||
|
||||
for (int i = 0; i < poolingCount; ++i) {
|
||||
try {
|
||||
ConnectionHolder connHolder = connections[i];
|
||||
DruidConnectionHolder connHolder = connections[i];
|
||||
|
||||
for (PreparedStatementHolder stmtHolder : connHolder.getStatementPool().getMap().values()) {
|
||||
connHolder.getStatementPool().closeRemovedStatement(stmtHolder);
|
||||
@@ -920,7 +920,7 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
|
||||
}
|
||||
}
|
||||
|
||||
void putLast(ConnectionHolder e) throws SQLException {
|
||||
void putLast(DruidConnectionHolder e) throws SQLException {
|
||||
if (!enable) {
|
||||
discardConnection(e.getConnection());
|
||||
return;
|
||||
@@ -933,7 +933,7 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
|
||||
notEmptySignalCount++;
|
||||
}
|
||||
|
||||
ConnectionHolder takeLast() throws InterruptedException, SQLException {
|
||||
DruidConnectionHolder takeLast() throws InterruptedException, SQLException {
|
||||
try {
|
||||
while (poolingCount == 0) {
|
||||
empty.signal(); // send signal to CreateThread create connection
|
||||
@@ -960,13 +960,13 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
|
||||
}
|
||||
|
||||
poolingCount--;
|
||||
ConnectionHolder last = connections[poolingCount];
|
||||
DruidConnectionHolder last = connections[poolingCount];
|
||||
connections[poolingCount] = null;
|
||||
|
||||
return last;
|
||||
}
|
||||
|
||||
ConnectionHolder pollLast(long timeout, TimeUnit unit) throws InterruptedException, SQLException {
|
||||
DruidConnectionHolder pollLast(long timeout, TimeUnit unit) throws InterruptedException, SQLException {
|
||||
final long nanos = unit.toNanos(timeout);
|
||||
long estimate = nanos;
|
||||
|
||||
@@ -1018,7 +1018,7 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
|
||||
}
|
||||
|
||||
poolingCount--;
|
||||
ConnectionHolder last = connections[poolingCount];
|
||||
DruidConnectionHolder last = connections[poolingCount];
|
||||
connections[poolingCount] = null;
|
||||
|
||||
return last;
|
||||
@@ -1170,7 +1170,7 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
|
||||
|
||||
lock.lock();
|
||||
try {
|
||||
connections[poolingCount++] = new ConnectionHolder(DruidDataSource.this, connection);
|
||||
connections[poolingCount++] = new DruidConnectionHolder(DruidDataSource.this, connection);
|
||||
|
||||
if (poolingCount > poolingPeak) {
|
||||
poolingPeak = poolingCount;
|
||||
@@ -1361,7 +1361,7 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
|
||||
}
|
||||
|
||||
public void shrink(boolean checkTime) {
|
||||
final List<ConnectionHolder> evictList = new ArrayList<ConnectionHolder>();
|
||||
final List<DruidConnectionHolder> evictList = new ArrayList<DruidConnectionHolder>();
|
||||
try {
|
||||
lock.lockInterruptibly();
|
||||
} catch (InterruptedException e) {
|
||||
@@ -1371,7 +1371,7 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
|
||||
try {
|
||||
final int checkCount = poolingCount - minIdle;
|
||||
for (int i = 0; i < checkCount; ++i) {
|
||||
ConnectionHolder connection = connections[i];
|
||||
DruidConnectionHolder connection = connections[i];
|
||||
|
||||
if (checkTime) {
|
||||
long idleMillis = System.currentTimeMillis() - connection.getLastActiveTimeMillis();
|
||||
@@ -1395,7 +1395,7 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
|
||||
lock.unlock();
|
||||
}
|
||||
|
||||
for (ConnectionHolder item : evictList) {
|
||||
for (DruidConnectionHolder item : evictList) {
|
||||
Connection connection = item.getConnection();
|
||||
JdbcUtils.close(connection);
|
||||
destroyCount.incrementAndGet();
|
||||
@@ -1498,7 +1498,7 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
|
||||
|
||||
buf.append(",\n\tConnections:[");
|
||||
for (int i = 0; i < poolingCount; ++i) {
|
||||
ConnectionHolder conn = connections[i];
|
||||
DruidConnectionHolder conn = connections[i];
|
||||
if (conn != null) {
|
||||
if (i != 0) {
|
||||
buf.append(",");
|
||||
@@ -1514,7 +1514,7 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
|
||||
if (this.isPoolPreparedStatements()) {
|
||||
buf.append("\n\n[");
|
||||
for (int i = 0; i < poolingCount; ++i) {
|
||||
ConnectionHolder conn = connections[i];
|
||||
DruidConnectionHolder conn = connections[i];
|
||||
if (conn != null) {
|
||||
if (i != 0) {
|
||||
buf.append(",");
|
||||
@@ -1560,7 +1560,7 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
|
||||
lock.lock();
|
||||
try {
|
||||
for (int i = 0; i < poolingCount; ++i) {
|
||||
ConnectionHolder connHolder = connections[i];
|
||||
DruidConnectionHolder connHolder = connections[i];
|
||||
Connection conn = connHolder.getConnection();
|
||||
|
||||
Map<String, Object> map = new LinkedHashMap<String, Object>();
|
||||
|
||||
@@ -57,7 +57,7 @@ public class DruidPooledConnection implements javax.sql.PooledConnection, Connec
|
||||
private final static Log LOG = LogFactory.getLog(DruidPooledConnection.class);
|
||||
|
||||
protected Connection conn;
|
||||
protected ConnectionHolder holder;
|
||||
protected DruidConnectionHolder holder;
|
||||
protected TransactionInfo transactionInfo;
|
||||
private final boolean dupCloseLogEnable;
|
||||
private boolean traceEnable = false;
|
||||
@@ -68,7 +68,7 @@ public class DruidPooledConnection implements javax.sql.PooledConnection, Connec
|
||||
private long connectedTimeNano;
|
||||
private volatile boolean running = false;
|
||||
|
||||
public DruidPooledConnection(ConnectionHolder holder){
|
||||
public DruidPooledConnection(DruidConnectionHolder holder){
|
||||
this.conn = holder.getConnection();
|
||||
this.holder = holder;
|
||||
dupCloseLogEnable = holder.getDataSource().isDupCloseLogEnable();
|
||||
@@ -96,7 +96,7 @@ public class DruidPooledConnection implements javax.sql.PooledConnection, Connec
|
||||
}
|
||||
|
||||
public SQLException handleException(Throwable t) throws SQLException {
|
||||
final ConnectionHolder holder = this.holder;
|
||||
final DruidConnectionHolder holder = this.holder;
|
||||
|
||||
//
|
||||
if (holder != null) {
|
||||
@@ -148,7 +148,7 @@ public class DruidPooledConnection implements javax.sql.PooledConnection, Connec
|
||||
}
|
||||
}
|
||||
|
||||
public ConnectionHolder getConnectionHolder() {
|
||||
public DruidConnectionHolder getConnectionHolder() {
|
||||
return holder;
|
||||
}
|
||||
|
||||
@@ -174,7 +174,7 @@ public class DruidPooledConnection implements javax.sql.PooledConnection, Connec
|
||||
return;
|
||||
}
|
||||
|
||||
ConnectionHolder holder = this.holder;
|
||||
DruidConnectionHolder holder = this.holder;
|
||||
if (holder == null) {
|
||||
if (dupCloseLogEnable) {
|
||||
LOG.error("dup close");
|
||||
@@ -201,7 +201,7 @@ public class DruidPooledConnection implements javax.sql.PooledConnection, Connec
|
||||
return;
|
||||
}
|
||||
|
||||
ConnectionHolder holder = this.holder;
|
||||
DruidConnectionHolder holder = this.holder;
|
||||
if (holder == null && dupCloseLogEnable) {
|
||||
LOG.error("dup close");
|
||||
return;
|
||||
@@ -1006,14 +1006,14 @@ public class DruidPooledConnection implements javax.sql.PooledConnection, Connec
|
||||
}
|
||||
|
||||
final void beforeExecute() {
|
||||
final ConnectionHolder holder = this.holder;
|
||||
final DruidConnectionHolder holder = this.holder;
|
||||
if (holder != null && holder.getDataSource().isRemoveAbandoned()) {
|
||||
running = true;
|
||||
}
|
||||
}
|
||||
|
||||
final void afterExecute() {
|
||||
final ConnectionHolder holder = this.holder;
|
||||
final DruidConnectionHolder holder = this.holder;
|
||||
if (holder != null && holder.getDataSource().isRemoveAbandoned()) {
|
||||
running = false;
|
||||
holder.setLastActiveTimeMillis(System.currentTimeMillis());
|
||||
|
||||
@@ -39,7 +39,7 @@ public class PreparedStatementPool {
|
||||
private final LRUCache map;
|
||||
private final DruidAbstractDataSource dataSource;
|
||||
|
||||
public PreparedStatementPool(ConnectionHolder holder){
|
||||
public PreparedStatementPool(DruidConnectionHolder holder){
|
||||
this.dataSource = holder.getDataSource();
|
||||
int initCapacity = holder.getDataSource().getMaxPoolPreparedStatementPerConnectionSize();
|
||||
if (initCapacity <= 0) {
|
||||
|
||||
@@ -9,7 +9,7 @@ import javax.transaction.xa.XAException;
|
||||
import javax.transaction.xa.XAResource;
|
||||
import javax.transaction.xa.Xid;
|
||||
|
||||
import com.alibaba.druid.pool.ConnectionHolder;
|
||||
import com.alibaba.druid.pool.DruidConnectionHolder;
|
||||
import com.alibaba.druid.pool.DruidPooledConnection;
|
||||
import com.alibaba.druid.support.logging.Log;
|
||||
import com.alibaba.druid.support.logging.LogFactory;
|
||||
@@ -26,7 +26,7 @@ public class XAPoolableConnection extends DruidPooledConnection implements XARes
|
||||
|
||||
private Lock stateLock = new ReentrantLock();
|
||||
|
||||
public XAPoolableConnection(ConnectionHolder holder, XAConnection xaConnection) throws SQLException{
|
||||
public XAPoolableConnection(DruidConnectionHolder holder, XAConnection xaConnection) throws SQLException{
|
||||
super(holder);
|
||||
|
||||
this.xaConnection = xaConnection;
|
||||
|
||||
@@ -3,7 +3,7 @@ package com.alibaba.druid.bvt.pool;
|
||||
import junit.framework.Assert;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import com.alibaba.druid.pool.ConnectionHolder;
|
||||
import com.alibaba.druid.pool.DruidConnectionHolder;
|
||||
import com.alibaba.druid.pool.DruidDataSource;
|
||||
import com.alibaba.druid.pool.DruidPooledConnection;
|
||||
import com.alibaba.druid.pool.DruidPooledPreparedStatement;
|
||||
@@ -29,7 +29,7 @@ public class PSCacheTest3 extends TestCase {
|
||||
public void test_pscache() throws Exception {
|
||||
DruidPooledConnection conn = dataSource.getConnection();
|
||||
|
||||
ConnectionHolder holder = conn.getConnectionHolder();
|
||||
DruidConnectionHolder holder = conn.getConnectionHolder();
|
||||
PreparedStatementPool stmtPool = holder.getStatementPool();
|
||||
|
||||
final String sql_0 = "select 0";
|
||||
|
||||
@@ -4,7 +4,7 @@ import junit.framework.Assert;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import com.alibaba.druid.TestForZY.A;
|
||||
import com.alibaba.druid.pool.ConnectionHolder;
|
||||
import com.alibaba.druid.pool.DruidConnectionHolder;
|
||||
import com.alibaba.druid.pool.DruidDataSource;
|
||||
import com.alibaba.druid.pool.DruidPooledConnection;
|
||||
import com.alibaba.druid.pool.DruidPooledPreparedStatement;
|
||||
@@ -32,7 +32,7 @@ public class PSCacheTest4 extends TestCase {
|
||||
public void test_pscache() throws Exception {
|
||||
DruidPooledConnection conn = dataSource.getConnection();
|
||||
|
||||
ConnectionHolder holder = conn.getConnectionHolder();
|
||||
DruidConnectionHolder holder = conn.getConnectionHolder();
|
||||
PreparedStatementPool stmtPool = holder.getStatementPool();
|
||||
|
||||
final String sql_0 = "select 0";
|
||||
|
||||
@@ -6,7 +6,7 @@ import org.junit.Assert;
|
||||
|
||||
import com.alibaba.druid.mock.MockConnection;
|
||||
import com.alibaba.druid.mock.MockPreparedStatement;
|
||||
import com.alibaba.druid.pool.ConnectionHolder;
|
||||
import com.alibaba.druid.pool.DruidConnectionHolder;
|
||||
import com.alibaba.druid.pool.DruidDataSource;
|
||||
import com.alibaba.druid.pool.DruidPooledPreparedStatement.PreparedStatementKey;
|
||||
import com.alibaba.druid.pool.PreparedStatementHolder;
|
||||
@@ -147,7 +147,7 @@ public class PreparedStatementKeyTest extends TestCase {
|
||||
MockConnection conn = new MockConnection();
|
||||
PreparedStatementKey k1 = new PreparedStatementKey("x1", "c1", MethodType.M1);
|
||||
|
||||
PreparedStatementPool pool = new PreparedStatementPool(new ConnectionHolder(dataSource, conn));
|
||||
PreparedStatementPool pool = new PreparedStatementPool(new DruidConnectionHolder(dataSource, conn));
|
||||
MockPreparedStatement raw = new MockPreparedStatement(null, null);
|
||||
pool.put(new PreparedStatementHolder(k1, raw));
|
||||
|
||||
|
||||
Reference in New Issue
Block a user