mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
support clearStatementCache
This commit is contained in:
@@ -621,6 +621,18 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
|
||||
}
|
||||
}
|
||||
|
||||
public void clearStatementCache() {
|
||||
lock.lock();
|
||||
try {
|
||||
for (int i = 0; i < poolingCount; ++i) {
|
||||
ConnectionHolder conn = connections[i];
|
||||
conn.getStatementPool().clear();
|
||||
}
|
||||
} finally {
|
||||
lock.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* close datasource
|
||||
*/
|
||||
|
||||
@@ -75,6 +75,8 @@ public interface DruidDataSourceMBean extends DruidAbstractDataSourceMBean {
|
||||
Date getPoolingPeakTime();
|
||||
|
||||
long getErrorCount();
|
||||
|
||||
|
||||
ObjectName getObjectName();
|
||||
|
||||
void clearStatementCache();
|
||||
}
|
||||
|
||||
@@ -59,11 +59,18 @@ public class PoolableConnection implements PooledConnection, Connection {
|
||||
private final boolean dupCloseLogEnable;
|
||||
private boolean traceEnable = false;
|
||||
private boolean diable = false;
|
||||
private boolean closed = false;
|
||||
private final Thread ownerThread;
|
||||
|
||||
public PoolableConnection(ConnectionHolder holder){
|
||||
this.conn = holder.getConnection();
|
||||
this.holder = holder;
|
||||
dupCloseLogEnable = holder.getDataSource().isDupCloseLogEnable();
|
||||
ownerThread = Thread.currentThread();
|
||||
}
|
||||
|
||||
public Thread getOwnerThread() {
|
||||
return ownerThread;
|
||||
}
|
||||
|
||||
public boolean isTraceEnable() {
|
||||
@@ -159,13 +166,14 @@ public class PoolableConnection implements PooledConnection, Connection {
|
||||
this.holder = null;
|
||||
conn = null;
|
||||
transactionInfo = null;
|
||||
closed = true;
|
||||
}
|
||||
|
||||
// ////////////////////
|
||||
|
||||
@Override
|
||||
public PreparedStatement prepareStatement(String sql) throws SQLException {
|
||||
checkOpen();
|
||||
checkState();
|
||||
|
||||
PreparedStatementHolder stmtHolder = null;
|
||||
PreparedStatementKey key = new PreparedStatementKey(sql, getCatalog(), MethodType.M1);
|
||||
@@ -200,7 +208,7 @@ public class PoolableConnection implements PooledConnection, Connection {
|
||||
@Override
|
||||
public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency)
|
||||
throws SQLException {
|
||||
checkOpen();
|
||||
checkState();
|
||||
|
||||
PreparedStatementHolder stmtHolder = null;
|
||||
PreparedStatementKey key = new PreparedStatementKey(sql, getCatalog(), MethodType.M2, resultSetType,
|
||||
@@ -237,7 +245,7 @@ public class PoolableConnection implements PooledConnection, Connection {
|
||||
@Override
|
||||
public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency,
|
||||
int resultSetHoldability) throws SQLException {
|
||||
checkOpen();
|
||||
checkState();
|
||||
|
||||
PreparedStatementHolder stmtHolder = null;
|
||||
PreparedStatementKey key = new PreparedStatementKey(sql, getCatalog(), MethodType.M3, resultSetType,
|
||||
@@ -274,7 +282,7 @@ public class PoolableConnection implements PooledConnection, Connection {
|
||||
|
||||
@Override
|
||||
public PreparedStatement prepareStatement(String sql, int[] columnIndexes) throws SQLException {
|
||||
checkOpen();
|
||||
checkState();
|
||||
|
||||
PreparedStatementKey key = new PreparedStatementKey(sql, getCatalog(), MethodType.M4, columnIndexes);
|
||||
PreparedStatementHolder stmtHolder = null;
|
||||
@@ -308,7 +316,7 @@ public class PoolableConnection implements PooledConnection, Connection {
|
||||
|
||||
@Override
|
||||
public PreparedStatement prepareStatement(String sql, String[] columnNames) throws SQLException {
|
||||
checkOpen();
|
||||
checkState();
|
||||
|
||||
PreparedStatementKey key = new PreparedStatementKey(sql, getCatalog(), MethodType.M5, columnNames);
|
||||
PreparedStatementHolder stmtHolder = null;
|
||||
@@ -342,7 +350,7 @@ public class PoolableConnection implements PooledConnection, Connection {
|
||||
|
||||
@Override
|
||||
public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys) throws SQLException {
|
||||
checkOpen();
|
||||
checkState();
|
||||
|
||||
PreparedStatementKey key = new PreparedStatementKey(sql, getCatalog(), MethodType.M6, autoGeneratedKeys);
|
||||
PreparedStatementHolder stmtHolder = null;
|
||||
@@ -378,7 +386,7 @@ public class PoolableConnection implements PooledConnection, Connection {
|
||||
|
||||
@Override
|
||||
public CallableStatement prepareCall(String sql) throws SQLException {
|
||||
checkOpen();
|
||||
checkState();
|
||||
|
||||
PreparedStatementHolder stmtHolder = null;
|
||||
PreparedStatementKey key = new PreparedStatementKey(sql, getCatalog(), MethodType.Precall_1);
|
||||
@@ -413,7 +421,7 @@ public class PoolableConnection implements PooledConnection, Connection {
|
||||
@Override
|
||||
public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency,
|
||||
int resultSetHoldability) throws SQLException {
|
||||
checkOpen();
|
||||
checkState();
|
||||
|
||||
PreparedStatementHolder stmtHolder = null;
|
||||
PreparedStatementKey key = new PreparedStatementKey(sql, getCatalog(), MethodType.Precall_2, resultSetType,
|
||||
@@ -450,7 +458,7 @@ public class PoolableConnection implements PooledConnection, Connection {
|
||||
|
||||
@Override
|
||||
public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency) throws SQLException {
|
||||
checkOpen();
|
||||
checkState();
|
||||
|
||||
PreparedStatementHolder stmtHolder = null;
|
||||
PreparedStatementKey key = new PreparedStatementKey(sql, getCatalog(), MethodType.Precall_3, resultSetType,
|
||||
@@ -487,7 +495,7 @@ public class PoolableConnection implements PooledConnection, Connection {
|
||||
|
||||
@Override
|
||||
public Statement createStatement() throws SQLException {
|
||||
checkOpen();
|
||||
checkState();
|
||||
|
||||
Statement stmt = null;
|
||||
try {
|
||||
@@ -507,7 +515,7 @@ public class PoolableConnection implements PooledConnection, Connection {
|
||||
@Override
|
||||
public Statement createStatement(int resultSetType, int resultSetConcurrency, int resultSetHoldability)
|
||||
throws SQLException {
|
||||
checkOpen();
|
||||
checkState();
|
||||
|
||||
Statement stmt = null;
|
||||
try {
|
||||
@@ -526,7 +534,7 @@ public class PoolableConnection implements PooledConnection, Connection {
|
||||
|
||||
@Override
|
||||
public Statement createStatement(int resultSetType, int resultSetConcurrency) throws SQLException {
|
||||
checkOpen();
|
||||
checkState();
|
||||
|
||||
Statement stmt = null;
|
||||
try {
|
||||
@@ -574,14 +582,14 @@ public class PoolableConnection implements PooledConnection, Connection {
|
||||
|
||||
@Override
|
||||
public String nativeSQL(String sql) throws SQLException {
|
||||
checkOpen();
|
||||
checkState();
|
||||
|
||||
return conn.nativeSQL(sql);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setAutoCommit(boolean autoCommit) throws SQLException {
|
||||
checkOpen();
|
||||
checkState();
|
||||
|
||||
try {
|
||||
conn.setAutoCommit(autoCommit);
|
||||
@@ -605,14 +613,14 @@ public class PoolableConnection implements PooledConnection, Connection {
|
||||
|
||||
@Override
|
||||
public boolean getAutoCommit() throws SQLException {
|
||||
checkOpen();
|
||||
checkState();
|
||||
|
||||
return conn.getAutoCommit();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void commit() throws SQLException {
|
||||
checkOpen();
|
||||
checkState();
|
||||
|
||||
DruidAbstractDataSource dataSource = holder.getDataSource();
|
||||
dataSource.incrementCommitCount();
|
||||
@@ -654,7 +662,7 @@ public class PoolableConnection implements PooledConnection, Connection {
|
||||
|
||||
@Override
|
||||
public Savepoint setSavepoint(String name) throws SQLException {
|
||||
checkOpen();
|
||||
checkState();
|
||||
|
||||
return conn.setSavepoint(name);
|
||||
}
|
||||
@@ -693,7 +701,7 @@ public class PoolableConnection implements PooledConnection, Connection {
|
||||
|
||||
@Override
|
||||
public void releaseSavepoint(Savepoint savepoint) throws SQLException {
|
||||
checkOpen();
|
||||
checkState();
|
||||
try {
|
||||
conn.releaseSavepoint(savepoint);
|
||||
} catch (SQLException ex) {
|
||||
@@ -703,7 +711,7 @@ public class PoolableConnection implements PooledConnection, Connection {
|
||||
|
||||
@Override
|
||||
public Clob createClob() throws SQLException {
|
||||
checkOpen();
|
||||
checkState();
|
||||
|
||||
return conn.createClob();
|
||||
}
|
||||
@@ -719,14 +727,14 @@ public class PoolableConnection implements PooledConnection, Connection {
|
||||
|
||||
@Override
|
||||
public DatabaseMetaData getMetaData() throws SQLException {
|
||||
checkOpen();
|
||||
checkState();
|
||||
|
||||
return conn.getMetaData();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setReadOnly(boolean readOnly) throws SQLException {
|
||||
checkOpen();
|
||||
checkState();
|
||||
|
||||
conn.setReadOnly(readOnly);
|
||||
holder.setUnderlyingReadOnly(readOnly);
|
||||
@@ -734,28 +742,28 @@ public class PoolableConnection implements PooledConnection, Connection {
|
||||
|
||||
@Override
|
||||
public boolean isReadOnly() throws SQLException {
|
||||
checkOpen();
|
||||
checkState();
|
||||
|
||||
return conn.isReadOnly();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setCatalog(String catalog) throws SQLException {
|
||||
checkOpen();
|
||||
checkState();
|
||||
|
||||
conn.setCatalog(catalog);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getCatalog() throws SQLException {
|
||||
checkOpen();
|
||||
checkState();
|
||||
|
||||
return conn.getCatalog();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setTransactionIsolation(int level) throws SQLException {
|
||||
checkOpen();
|
||||
checkState();
|
||||
|
||||
conn.setTransactionIsolation(level);
|
||||
holder.setUnderlyingTransactionIsolation(level);
|
||||
@@ -763,42 +771,42 @@ public class PoolableConnection implements PooledConnection, Connection {
|
||||
|
||||
@Override
|
||||
public int getTransactionIsolation() throws SQLException {
|
||||
checkOpen();
|
||||
checkState();
|
||||
|
||||
return conn.getTransactionIsolation();
|
||||
}
|
||||
|
||||
@Override
|
||||
public SQLWarning getWarnings() throws SQLException {
|
||||
checkOpen();
|
||||
checkState();
|
||||
|
||||
return conn.getWarnings();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clearWarnings() throws SQLException {
|
||||
checkOpen();
|
||||
checkState();
|
||||
|
||||
conn.clearWarnings();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, Class<?>> getTypeMap() throws SQLException {
|
||||
checkOpen();
|
||||
checkState();
|
||||
|
||||
return conn.getTypeMap();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setTypeMap(Map<String, Class<?>> map) throws SQLException {
|
||||
checkOpen();
|
||||
checkState();
|
||||
|
||||
conn.setTypeMap(map);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setHoldability(int holdability) throws SQLException {
|
||||
checkOpen();
|
||||
checkState();
|
||||
|
||||
conn.setHoldability(holdability);
|
||||
holder.setUnderlyingHoldability(holdability);
|
||||
@@ -806,42 +814,42 @@ public class PoolableConnection implements PooledConnection, Connection {
|
||||
|
||||
@Override
|
||||
public int getHoldability() throws SQLException {
|
||||
checkOpen();
|
||||
checkState();
|
||||
|
||||
return conn.getHoldability();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Savepoint setSavepoint() throws SQLException {
|
||||
checkOpen();
|
||||
checkState();
|
||||
|
||||
return conn.setSavepoint();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Blob createBlob() throws SQLException {
|
||||
checkOpen();
|
||||
checkState();
|
||||
|
||||
return conn.createBlob();
|
||||
}
|
||||
|
||||
@Override
|
||||
public NClob createNClob() throws SQLException {
|
||||
checkOpen();
|
||||
checkState();
|
||||
|
||||
return conn.createNClob();
|
||||
}
|
||||
|
||||
@Override
|
||||
public SQLXML createSQLXML() throws SQLException {
|
||||
checkOpen();
|
||||
checkState();
|
||||
|
||||
return conn.createSQLXML();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isValid(int timeout) throws SQLException {
|
||||
checkOpen();
|
||||
checkState();
|
||||
|
||||
return conn.isValid(timeout);
|
||||
}
|
||||
@@ -866,28 +874,28 @@ public class PoolableConnection implements PooledConnection, Connection {
|
||||
|
||||
@Override
|
||||
public String getClientInfo(String name) throws SQLException {
|
||||
checkOpen();
|
||||
checkState();
|
||||
|
||||
return conn.getClientInfo(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Properties getClientInfo() throws SQLException {
|
||||
checkOpen();
|
||||
checkState();
|
||||
|
||||
return conn.getClientInfo();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Array createArrayOf(String typeName, Object[] elements) throws SQLException {
|
||||
checkOpen();
|
||||
checkState();
|
||||
|
||||
return conn.createArrayOf(typeName, elements);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Struct createStruct(String typeName, Object[] attributes) throws SQLException {
|
||||
checkOpen();
|
||||
checkState();
|
||||
|
||||
return conn.createStruct(typeName, attributes);
|
||||
}
|
||||
@@ -928,8 +936,8 @@ public class PoolableConnection implements PooledConnection, Connection {
|
||||
holder.getStatementEventListeners().remove(listener);
|
||||
}
|
||||
|
||||
public void checkOpen() throws SQLException {
|
||||
if (holder == null) {
|
||||
public void checkState() throws SQLException {
|
||||
if (holder == null || closed || diable) {
|
||||
throw new SQLException("connection is closed");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,6 +17,7 @@ package com.alibaba.druid.pool;
|
||||
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.SQLException;
|
||||
import java.util.Iterator;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
@@ -74,6 +75,19 @@ public class PreparedStatementPool {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
Iterator<Entry<PreparedStatementKey, PreparedStatementHolder>> iter = map.entrySet().iterator();
|
||||
while (iter.hasNext()) {
|
||||
Entry<PreparedStatementKey, PreparedStatementHolder> entry = iter.next();
|
||||
|
||||
dataSource.closePreapredStatement(entry.getValue());
|
||||
dataSource.decrementCachedPreparedStatementCount();
|
||||
dataSource.incrementCachedPreparedStatementDeleteCount();
|
||||
|
||||
iter.remove();
|
||||
}
|
||||
}
|
||||
|
||||
public Map<PreparedStatementKey, PreparedStatementHolder> getMap() {
|
||||
return map;
|
||||
|
||||
Reference in New Issue
Block a user