bug fixed for multi-thread use one connection.

This commit is contained in:
shaojin.wensj
2019-03-20 15:27:33 +08:00
parent ac47967a16
commit 116e7bee1a
3 changed files with 33 additions and 9 deletions
@@ -24,6 +24,7 @@ import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.locks.ReentrantLock;
import javax.sql.ConnectionEventListener;
import javax.sql.StatementEventListener;
@@ -68,6 +69,7 @@ public final class DruidConnectionHolder {
protected boolean discard = false;
protected final Map<String, Object> variables;
protected final Map<String, Object> globleVariables;
final ReentrantLock lock = new ReentrantLock();
public DruidConnectionHolder(DruidAbstractDataSource dataSource, PhysicalConnectionInfo pyConnectInfo)
throws SQLException{
@@ -191,11 +193,21 @@ public final class DruidConnectionHolder {
}
public void addTrace(DruidPooledStatement stmt) {
statementTrace.add(stmt);
lock.lock();
try {
statementTrace.add(stmt);
} finally {
lock.unlock();
}
}
public void removeTrace(DruidPooledStatement stmt) {
statementTrace.remove(stmt);
lock.lock();
try {
statementTrace.remove(stmt);
} finally {
lock.unlock();
}
}
public List<ConnectionEventListener> getConnectionEventListeners() {
@@ -285,11 +297,17 @@ public final class DruidConnectionHolder {
connectionEventListeners.clear();
statementEventListeners.clear();
for (Object item : statementTrace.toArray()) {
Statement stmt = (Statement) item;
JdbcUtils.close(stmt);
lock.lock();
try {
for (Object item : statementTrace.toArray()) {
Statement stmt = (Statement) item;
JdbcUtils.close(stmt);
}
statementTrace.clear();
} finally {
lock.unlock();
}
statementTrace.clear();
conn.clearWarnings();
}
@@ -1679,8 +1679,13 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
if (requireDiscard) {
if (holder.statementTrace != null) {
for (Statement stmt : holder.statementTrace) {
JdbcUtils.close(stmt);
holder.lock.lock();
try {
for (Statement stmt : holder.statementTrace) {
JdbcUtils.close(stmt);
}
} finally {
holder.lock.unlock();
}
}
@@ -69,13 +69,14 @@ public class DruidPooledConnection extends PoolableWrapper implements javax.sql.
private volatile boolean abandoned = false;
protected StackTraceElement[] connectStackTrace;
protected Throwable disableError = null;
public ReentrantLock lock = new ReentrantLock();
final ReentrantLock lock;
public DruidPooledConnection(DruidConnectionHolder holder){
super(holder.getConnection());
this.conn = holder.getConnection();
this.holder = holder;
this.lock = holder.lock;
dupCloseLogEnable = holder.getDataSource().isDupCloseLogEnable();
ownerThread = Thread.currentThread();
connectedTimeMillis = System.currentTimeMillis();