mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
Merge remote-tracking branch 'upstream/master'
This commit is contained in:
@@ -280,6 +280,8 @@ public abstract class DruidAbstractDataSource extends WrapperAdapter implements
|
||||
protected boolean initGlobalVariants = false;
|
||||
protected volatile boolean onFatalError = false;
|
||||
protected volatile int onFatalErrorMaxActive = 0;
|
||||
protected volatile int fatalErrorCount = 0;
|
||||
protected volatile int fatalErrorCountLastShrink = 0;
|
||||
protected volatile long lastFatalErrorTimeMillis = 0;
|
||||
protected volatile String lastFatalErrorSql = null;
|
||||
protected volatile Throwable lastFatalError = null;
|
||||
@@ -1382,6 +1384,17 @@ public abstract class DruidAbstractDataSource extends WrapperAdapter implements
|
||||
Exception error = null;
|
||||
try {
|
||||
result = validConnectionChecker.isValidConnection(conn, validationQuery, validationQueryTimeout);
|
||||
|
||||
if (result && onFatalError) {
|
||||
lock.lock();
|
||||
try {
|
||||
if (onFatalError) {
|
||||
onFatalError = false;
|
||||
}
|
||||
} finally {
|
||||
lock.unlock();
|
||||
}
|
||||
}
|
||||
} catch (SQLException ex) {
|
||||
throw ex;
|
||||
} catch (Exception ex) {
|
||||
@@ -1409,6 +1422,18 @@ public abstract class DruidAbstractDataSource extends WrapperAdapter implements
|
||||
if (!rs.next()) {
|
||||
throw new SQLException("validationQuery didn't return a row");
|
||||
}
|
||||
|
||||
if (onFatalError) {
|
||||
lock.lock();
|
||||
try {
|
||||
if (onFatalError) {
|
||||
onFatalError = false;
|
||||
}
|
||||
}
|
||||
finally {
|
||||
lock.unlock();
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
JdbcUtils.close(rs);
|
||||
JdbcUtils.close(stmt);
|
||||
@@ -1458,6 +1483,17 @@ public abstract class DruidAbstractDataSource extends WrapperAdapter implements
|
||||
}
|
||||
}
|
||||
|
||||
if (valid && onFatalError) {
|
||||
lock.lock();
|
||||
try {
|
||||
if (onFatalError) {
|
||||
onFatalError = false;
|
||||
}
|
||||
} finally {
|
||||
lock.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
return valid;
|
||||
}
|
||||
|
||||
@@ -1485,6 +1521,17 @@ public abstract class DruidAbstractDataSource extends WrapperAdapter implements
|
||||
JdbcUtils.close(stmt);
|
||||
}
|
||||
|
||||
if (onFatalError) {
|
||||
lock.lock();
|
||||
try {
|
||||
if (onFatalError) {
|
||||
onFatalError = false;
|
||||
}
|
||||
} finally {
|
||||
lock.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
} catch (Throwable ex) {
|
||||
// skip
|
||||
|
||||
@@ -1228,7 +1228,7 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
|
||||
}
|
||||
|
||||
if (removeAbandoned) {
|
||||
LOG.warn("removeAbandoned is true, not use in productiion.");
|
||||
LOG.warn("removeAbandoned is true, not use in production.");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1691,6 +1691,9 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
|
||||
|
||||
public void handleConnectionException(DruidPooledConnection pooledConnection, Throwable t, String sql) throws SQLException {
|
||||
final DruidConnectionHolder holder = pooledConnection.getConnectionHolder();
|
||||
if (holder == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
errorCountUpdater.incrementAndGet(this);
|
||||
lastError = t;
|
||||
@@ -1751,13 +1754,27 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
|
||||
}
|
||||
|
||||
lastFatalErrorTimeMillis = lastErrorTimeMillis;
|
||||
onFatalError = true;
|
||||
fatalErrorCount++;
|
||||
if (fatalErrorCount - fatalErrorCountLastShrink > onFatalErrorMaxActive) {
|
||||
onFatalError = true;
|
||||
}
|
||||
lastFatalError = error;
|
||||
lastFatalErrorSql = sql;
|
||||
} finally {
|
||||
lock.unlock();
|
||||
}
|
||||
|
||||
if(onFatalError && holder != null && holder.getDataSource() != null) {
|
||||
ReentrantLock dataSourceLock = holder.getDataSource().lock;
|
||||
dataSourceLock.lock();
|
||||
try {
|
||||
emptySignal();
|
||||
}
|
||||
finally {
|
||||
dataSourceLock.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
if (requireDiscard) {
|
||||
if (holder.statementTrace != null) {
|
||||
holder.lock.lock();
|
||||
@@ -2483,6 +2500,7 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
|
||||
&& (!(keepAlive && activeCount + poolingCount < minIdle)) // 在keepAlive场景不能放弃创建
|
||||
&& (!initTask) // 线程池初始化时的任务不能放弃创建
|
||||
&& !isFailContinuous() // failContinuous时不能放弃创建,否则会无法创建线程
|
||||
&& !isOnFatalError() // onFatalError时不能放弃创建,否则会无法创建线程
|
||||
) {
|
||||
clearCreateTask(taskId);
|
||||
return;
|
||||
@@ -2970,6 +2988,9 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
|
||||
boolean needFill = false;
|
||||
int evictCount = 0;
|
||||
int keepAliveCount = 0;
|
||||
int fatalErrorIncrement = fatalErrorCount - fatalErrorCountLastShrink;
|
||||
fatalErrorCountLastShrink = fatalErrorCount;
|
||||
|
||||
try {
|
||||
if (!inited) {
|
||||
return;
|
||||
@@ -2980,6 +3001,11 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
|
||||
for (int i = 0; i < poolingCount; ++i) {
|
||||
DruidConnectionHolder connection = connections[i];
|
||||
|
||||
if ((onFatalError || fatalErrorIncrement > 0) && (lastFatalErrorTimeMillis > connection.connectTimeMillis)) {
|
||||
keepAliveConnections[keepAliveCount++] = connection;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (checkTime) {
|
||||
if (phyTimeoutMillis > 0) {
|
||||
long phyConnectTimeMillis = currentTimeMillis - connection.connectTimeMillis;
|
||||
@@ -3104,6 +3130,13 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
|
||||
} finally {
|
||||
lock.unlock();
|
||||
}
|
||||
} else if (onFatalError || fatalErrorIncrement > 0) {
|
||||
lock.lock();
|
||||
try {
|
||||
emptySignal();
|
||||
} finally {
|
||||
lock.unlock();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3368,7 +3401,7 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
|
||||
map.put("ActiveCount", this.getActiveCount());
|
||||
map.put("PoolingCount", this.getPoolingCount());
|
||||
map.put("LockQueueLength", this.getLockQueueLength());
|
||||
map.put("WaitThreadCount", this.getNotEmptyWaitThreadPeak());
|
||||
map.put("WaitThreadCount", this.getNotEmptyWaitThreadCount());
|
||||
|
||||
// 10 - 14
|
||||
map.put("InitialSize", this.getInitialSize());
|
||||
|
||||
@@ -147,6 +147,7 @@ public class DruidPooledConnection extends PoolableWrapper implements javax.sql.
|
||||
public void closePoolableStatement(DruidPooledPreparedStatement stmt) throws SQLException {
|
||||
PreparedStatement rawStatement = stmt.getRawPreparedStatement();
|
||||
|
||||
final DruidConnectionHolder holder = this.holder;
|
||||
if (holder == null) {
|
||||
return;
|
||||
}
|
||||
@@ -1188,19 +1189,19 @@ public class DruidPooledConnection extends PoolableWrapper implements javax.sql.
|
||||
}
|
||||
|
||||
public String getSchema() throws SQLException {
|
||||
throw new SQLFeatureNotSupportedException();
|
||||
return conn.getSchema();
|
||||
}
|
||||
|
||||
public void abort(Executor executor) throws SQLException {
|
||||
throw new SQLFeatureNotSupportedException();
|
||||
conn.abort(executor);
|
||||
}
|
||||
|
||||
public void setNetworkTimeout(Executor executor, int milliseconds) throws SQLException {
|
||||
throw new SQLFeatureNotSupportedException();
|
||||
conn.setNetworkTimeout(executor, milliseconds);
|
||||
}
|
||||
|
||||
public int getNetworkTimeout() throws SQLException {
|
||||
throw new SQLFeatureNotSupportedException();
|
||||
return conn.getNetworkTimeout();
|
||||
}
|
||||
|
||||
final void beforeExecute() {
|
||||
|
||||
@@ -490,7 +490,7 @@ public class DruidPooledPreparedStatement extends DruidPooledStatement implement
|
||||
incrementExecuteCount();
|
||||
transactionRecord(sql);
|
||||
|
||||
// oracleSetRowPrefetch();
|
||||
oracleSetRowPrefetch();
|
||||
|
||||
conn.beforeExecute();
|
||||
try {
|
||||
|
||||
@@ -27,7 +27,6 @@ import java.sql.ResultSet;
|
||||
import java.sql.ResultSetMetaData;
|
||||
import java.sql.RowId;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.SQLFeatureNotSupportedException;
|
||||
import java.sql.SQLWarning;
|
||||
import java.sql.SQLXML;
|
||||
import java.sql.Statement;
|
||||
@@ -1763,11 +1762,21 @@ public final class DruidPooledResultSet extends PoolableWrapper implements Resul
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> T getObject(int columnIndex, Class<T> type) throws SQLException {
|
||||
return rs.getObject(columnIndex, type);
|
||||
try {
|
||||
return rs.getObject(columnIndex, type);
|
||||
} catch (Throwable t) {
|
||||
throw checkException(t);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> T getObject(String columnLabel, Class<T> type) throws SQLException {
|
||||
return rs.getObject(columnLabel, type);
|
||||
try {
|
||||
return rs.getObject(columnLabel, type);
|
||||
} catch (Throwable t) {
|
||||
throw checkException(t);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -98,8 +98,9 @@ public class DruidPooledStatement extends PoolableWrapper implements Statement {
|
||||
|
||||
DruidDataSource dataSource = null;
|
||||
|
||||
if (this.conn.holder.dataSource instanceof DruidDataSource) {
|
||||
dataSource = (DruidDataSource) this.conn.holder.dataSource;
|
||||
final DruidConnectionHolder holder = this.conn.holder;
|
||||
if (holder.dataSource instanceof DruidDataSource) {
|
||||
dataSource = (DruidDataSource) holder.dataSource;
|
||||
}
|
||||
if (dataSource == null) {
|
||||
return;
|
||||
|
||||
@@ -15,17 +15,17 @@
|
||||
*/
|
||||
package com.alibaba.druid.pool.vendor;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.sql.Connection;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.Statement;
|
||||
|
||||
import com.alibaba.druid.pool.DruidPooledConnection;
|
||||
import com.alibaba.druid.pool.ValidConnectionChecker;
|
||||
import com.alibaba.druid.pool.ValidConnectionCheckerAdapter;
|
||||
import com.alibaba.druid.proxy.jdbc.ConnectionProxy;
|
||||
import com.alibaba.druid.util.JdbcUtils;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.sql.Connection;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.Statement;
|
||||
|
||||
public class PGValidConnectionChecker extends ValidConnectionCheckerAdapter implements ValidConnectionChecker, Serializable {
|
||||
|
||||
private static final long serialVersionUID = -2227528634302168877L;
|
||||
@@ -61,7 +61,10 @@ public class PGValidConnectionChecker extends ValidConnectionCheckerAdapter impl
|
||||
ResultSet rs = null;
|
||||
try {
|
||||
stmt = conn.createStatement();
|
||||
stmt.setQueryTimeout(queryTimeout);
|
||||
if (queryTimeout >= 0) {
|
||||
//pgsql Driver 9.0以及以下版本不支持setQueryTimeout,可通过设置queryTimeout<0兼容低版本
|
||||
stmt.setQueryTimeout(queryTimeout);
|
||||
}
|
||||
rs = stmt.executeQuery(validateQuery);
|
||||
return true;
|
||||
} finally {
|
||||
|
||||
Reference in New Issue
Block a user