mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
Merge branch 'master' into master
This commit is contained in:
@@ -19,7 +19,7 @@ public final class VERSION {
|
||||
|
||||
public final static int MajorVersion = 1;
|
||||
public final static int MinorVersion = 1;
|
||||
public final static int RevisionVersion = 17;
|
||||
public final static int RevisionVersion = 20;
|
||||
|
||||
public static String getVersionNumber() {
|
||||
return VERSION.MajorVersion + "." + VERSION.MinorVersion + "." + VERSION.RevisionVersion;
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -29,7 +29,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;
|
||||
@@ -1548,15 +1547,20 @@ public class ResultSetProxyImpl extends WrapperProxyImpl implements ResultSetPro
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> T getObject(int columnIndex, Class<T> type) throws SQLException {
|
||||
FilterChainImpl chain = createChain();
|
||||
Object value = chain.resultSet_getObject(this, columnIndex, type);
|
||||
T value = chain.resultSet_getObject(this, columnIndex, type);
|
||||
recycleFilterChain(chain);
|
||||
return (T) value;
|
||||
return value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> T getObject(String columnLabel, Class<T> type) throws SQLException {
|
||||
throw new SQLFeatureNotSupportedException();
|
||||
FilterChainImpl chain = createChain();
|
||||
T value = chain.resultSet_getObject(this, columnLabel, type);
|
||||
recycleFilterChain(chain);
|
||||
return value;
|
||||
}
|
||||
|
||||
public int getCloseCount() {
|
||||
@@ -1604,6 +1608,7 @@ public class ResultSetProxyImpl extends WrapperProxyImpl implements ResultSetPro
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public <T> T unwrap(Class<T> iface) throws SQLException {
|
||||
if (iface == ResultSetProxy.class || iface == ResultSetProxyImpl.class) {
|
||||
return (T) this;
|
||||
@@ -1612,6 +1617,7 @@ public class ResultSetProxyImpl extends WrapperProxyImpl implements ResultSetPro
|
||||
return super.unwrap(iface);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isWrapperFor(Class<?> iface) throws SQLException {
|
||||
if (iface == ResultSetProxy.class || iface == ResultSetProxyImpl.class) {
|
||||
return true;
|
||||
|
||||
@@ -402,6 +402,10 @@ public class PagerUtils {
|
||||
if (limit != null) {
|
||||
if (offset > 0) {
|
||||
limit.setOffset(new SQLIntegerExpr(offset));
|
||||
} else if (offset == 0) {
|
||||
if (limit.getOffset() != null) {
|
||||
limit.setOffset(new SQLIntegerExpr(offset));
|
||||
}
|
||||
}
|
||||
|
||||
if (check && limit.getRowCount() instanceof SQLNumericLiteralExpr) {
|
||||
|
||||
@@ -28,6 +28,7 @@ public interface SQLObject {
|
||||
void setParent(SQLObject parent);
|
||||
|
||||
Map<String, Object> getAttributes();
|
||||
boolean containsAttribute(String name);
|
||||
Object getAttribute(String name);
|
||||
void putAttribute(String name, Object value);
|
||||
Map<String, Object> getAttributesDirect();
|
||||
|
||||
@@ -88,6 +88,14 @@ public abstract class SQLObjectImpl implements SQLObject {
|
||||
return attributes;
|
||||
}
|
||||
|
||||
public boolean containsAttribute(String name) {
|
||||
if (attributes == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return attributes.containsKey(name);
|
||||
}
|
||||
|
||||
public Object getAttribute(String name) {
|
||||
if (attributes == null) {
|
||||
return null;
|
||||
|
||||
@@ -87,6 +87,7 @@ import com.alibaba.druid.sql.ast.statement.SQLCreateProcedureStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLCreateTableStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLExprStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLExprTableSource;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLFetchStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLIfStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLInsertStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLLoopStatement;
|
||||
@@ -5542,4 +5543,33 @@ public class MySqlStatementParser extends SQLStatementParser {
|
||||
|
||||
return stmt;
|
||||
}
|
||||
|
||||
/**
|
||||
* FETCH [[NEXT] FROM] cursor_name INTO var_name [, var_name] ...
|
||||
*/
|
||||
public SQLFetchStatement parseFetch() {
|
||||
accept(Token.FETCH);
|
||||
|
||||
if (lexer.identifierEquals("NEXT")) {
|
||||
lexer.nextToken();
|
||||
accept(Token.FROM);
|
||||
} else if (Token.FROM == lexer.token()) {
|
||||
lexer.nextToken();
|
||||
}
|
||||
|
||||
SQLFetchStatement stmt = new SQLFetchStatement();
|
||||
stmt.setCursorName(this.exprParser.name());
|
||||
|
||||
accept(Token.INTO);
|
||||
for (;;) {
|
||||
stmt.getInto().add(this.exprParser.name());
|
||||
if (lexer.token() == Token.COMMA) {
|
||||
lexer.nextToken();
|
||||
continue;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
return stmt;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4412,7 +4412,7 @@ public class MySqlOutputVisitor extends SQLASTOutputVisitor implements MySqlASTV
|
||||
@Override
|
||||
public boolean visit(MySqlChecksumTableStatement x) {
|
||||
print0(ucase ? "CHECKSUM TABLE " : "checksum table ");
|
||||
printAndAccept(x.getTables(), ",");
|
||||
printAndAccept(x.getTables(), ", ");
|
||||
return false;
|
||||
}
|
||||
} //
|
||||
|
||||
@@ -246,8 +246,8 @@ public class SQLEvalVisitorUtils {
|
||||
if (function != null) {
|
||||
Object result = function.eval(visitor, x);
|
||||
|
||||
if (result != SQLEvalVisitor.EVAL_ERROR) {
|
||||
x.getAttributes().put(EVAL_VALUE, result);
|
||||
if (result != SQLEvalVisitor.EVAL_ERROR && result != null) {
|
||||
x.putAttribute(EVAL_VALUE, result);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
@@ -744,7 +744,7 @@ public class SQLEvalVisitorUtils {
|
||||
public static boolean visit(SQLEvalVisitor visitor, SQLInListExpr x) {
|
||||
SQLExpr valueExpr = x.getExpr();
|
||||
valueExpr.accept(visitor);
|
||||
if (!valueExpr.getAttributes().containsKey(EVAL_VALUE)) {
|
||||
if (!valueExpr.containsAttribute(EVAL_VALUE)) {
|
||||
return false;
|
||||
}
|
||||
Object value = valueExpr.getAttribute(EVAL_VALUE);
|
||||
|
||||
@@ -31,7 +31,7 @@ public class Concat implements Function {
|
||||
for (SQLExpr item : x.getParameters()) {
|
||||
item.accept(visitor);
|
||||
|
||||
Object itemValue = item.getAttributes().get(EVAL_VALUE);
|
||||
Object itemValue = item.getAttribute(EVAL_VALUE);
|
||||
if (itemValue == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -41,7 +41,7 @@ public class Elt implements Function {
|
||||
param0IntValue = ((Number) param0Value).intValue();
|
||||
|
||||
if (param0IntValue >= x.getParameters().size()) {
|
||||
return null;
|
||||
return SQLEvalVisitor.EVAL_VALUE_NULL;
|
||||
}
|
||||
|
||||
SQLExpr item = x.getParameters().get(param0IntValue);
|
||||
|
||||
@@ -288,6 +288,20 @@ public class WallVisitorUtils {
|
||||
return;
|
||||
}
|
||||
|
||||
List<SQLCommentHint> hints = x.getHintsDirect();
|
||||
if (hints != null
|
||||
&& x.getParent() instanceof SQLUnionQuery
|
||||
&& x == ((SQLUnionQuery) x.getParent()).getRight()
|
||||
) {
|
||||
for (SQLCommentHint hint : hints) {
|
||||
String text = hint.getText();
|
||||
if (text.startsWith("!")) {
|
||||
addViolation(visitor, ErrorCode.UNION, "union select hint not allow", x);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
SQLExpr where = x.getWhere();
|
||||
if (where != null) {
|
||||
checkCondition(visitor, x.getWhere());
|
||||
@@ -1559,7 +1573,7 @@ public class WallVisitorUtils {
|
||||
}
|
||||
|
||||
public static Object getValue(WallVisitor visitor, SQLExpr x) {
|
||||
if (x != null && x.getAttributes().containsKey(EVAL_VALUE)) {
|
||||
if (x != null && x.containsAttribute(EVAL_VALUE)) {
|
||||
return getValueFromAttributes(visitor, x);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user