Merge pull request #332 from wenshao/master

request for bulk integrate
This commit is contained in:
温高铁
2013-05-05 23:26:09 -07:00
18 changed files with 293 additions and 64 deletions
@@ -405,7 +405,8 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
filter.init(this);
}
if (this.dbType == JdbcConstants.MYSQL) {
if (JdbcConstants.MYSQL.equals(this.dbType) || //
JdbcConstants.MARIADB.equals(this.dbType)) {
boolean cacheServerConfigurationSet = false;
if (this.connectProperties.containsKey("cacheServerConfiguration")) {
cacheServerConfigurationSet = true;
@@ -544,7 +545,6 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
createConnectionThread.start();
}
/**
* load filters from SPI ServiceLoader
*
@@ -17,41 +17,112 @@ package com.alibaba.druid.pool.vendor;
import java.io.Serializable;
import java.sql.SQLException;
import java.util.HashSet;
import java.util.Set;
import com.alibaba.druid.pool.ExceptionSorter;
import com.alibaba.druid.support.logging.Log;
import com.alibaba.druid.support.logging.LogFactory;
/**
* Implementation of ExceptionSorter for Oracle.
*/
public class OracleExceptionSorter implements ExceptionSorter, Serializable {
private final static Log LOG = LogFactory.getLog(OracleExceptionSorter.class);
private static final long serialVersionUID = -9146226891418913174L;
private Set<Integer> fatalErrorCodes = new HashSet<Integer>();
public OracleExceptionSorter(){
String property = System.getProperty("druid.oracle.fatalErrorCodes");
if (property != null) {
String[] items = property.split("\\,");
for (String item : items) {
if (item != null && item.length() > 0) {
try {
int code = Integer.parseInt(item);
fatalErrorCodes.add(code);
} catch (NumberFormatException e) {
LOG.error("parse druid.oracle.fatalErrorCodes error", e);
}
}
}
}
}
public Set<Integer> getFatalErrorCodes() {
return fatalErrorCodes;
}
public void setFatalErrorCodes(Set<Integer> fatalErrorCodes) {
this.fatalErrorCodes = fatalErrorCodes;
}
public boolean isExceptionFatal(final SQLException e) {
final int error_code = Math.abs(e.getErrorCode()); // I can't remember if the errors are negative or positive.
if ((error_code == 28) // session has been killed
|| (error_code == 600) // Internal oracle error
|| (error_code == 1012) // not logged on
|| (error_code == 1014) // Oracle shutdown in progress
|| (error_code == 1033) // Oracle initialization or shutdown in progress
|| (error_code == 1034) // Oracle not available
|| (error_code == 1035) // ORACLE only available to users with RESTRICTED SESSION privilege
|| (error_code == 1089) // immediate shutdown in progress - no operations are permitted
|| (error_code == 1090) // shutdown in progress - connection is not permitted
|| (error_code == 1092) // ORACLE instance terminated. Disconnection forced
|| (error_code == 1094) // ALTER DATABASE CLOSE in progress. Connections not permitted
|| (error_code == 2396) // exceeded maximum idle time, please connect again
|| (error_code == 3106) // fatal two-task communication protocol error
|| (error_code == 3111) // break received on communication channel
|| (error_code == 3113) // end-of-file on communication channel
|| (error_code == 3114) // not connected to ORACLE
|| (error_code >= 12100 && error_code <= 12299) // TNS issues
) {
return true;
switch (error_code) {
case 28: // your session has been killed
case 600: // Internal oracle error
case 1012: // not logged on
case 1014: // Oracle shutdown in progress
case 1033: // Oracle initialization or shutdown in progress
case 1034: // Oracle not available
case 1035: // ORACLE only available to users with RESTRICTED SESSION privilege
case 1089: // immediate shutdown in progress - no operations are permitted
case 1090: // shutdown in progress - connection is not permitted
case 1092: // ORACLE instance terminated. Disconnection forced
case 1094: // ALTER DATABASE CLOSE in progress. Connections not permitted
case 2396: // exceeded maximum idle time, please connect again
case 3106: // fatal two-task communication protocol error
case 3111: // break received on communication channel
case 3113: // end-of-file on communication channel
case 3114: // not connected to ORACLE
case 3134: // Connections to this server version are no longer supported.
case 3135: // connection lost contact
case 3136: // inbound connection timed out
case 3138: // Connection terminated due to security policy violation
case 3142: // Connection was lost for the specified session and serial number. This is either due to session
// being killed or network problems.
case 3143: // Connection was lost for the specified process ID and thread ID. This is either due to session
// being killed or network problems.
case 3144: // Connection was lost for the specified process ID. This is either due to session being killed
// or network problems.
case 3145: // I/O streaming direction error
case 3149: // Invalid Oracle error code, Cause: An invalid Oracle error code was received by the server.
case 6801: // TLI Driver: listen for SPX server reconnect failed
case 6802: // TLI Driver: could not open the /etc/netware/yellowpages file
case 6805: // TLI Driver: could not send datagram SAP packet for SPX
case 9918: // Unable to get user privileges from SQL*Net
case 9920: // Unable to get sensitivity label from connection
case 9921: // Unable to get information label from connection
// TTC(Two-Task Common) ERROR CODE
case 17001: // Internal Error
case 17002: // Io exception
case 17008: // Closed Connection
case 17024: // No data read
case 17089: // internal error
case 17409: // invalid buffer length
case 17410: // No more data to read from socket
case 17416: // FATAl
case 25407: // connection terminated
case 25408: // can not safely replay call
case 25409: // failover happened during the network operation,cannot continue
case 25425: // connection lost during rollback
case 29276: // transfer timeout
case 30676: // socket read or write failed
return true;
default:
if (error_code >= 12100 && error_code <= 12299) { // TNS issues
return true;
}
break;
}
final String error_text = (e.getMessage()).toUpperCase();
@@ -59,12 +130,18 @@ public class OracleExceptionSorter implements ExceptionSorter, Serializable {
// Exclude oracle user defined error codes (20000 through 20999) from consideration when looking for
// certain strings.
if ((error_code < 20000 || error_code >= 21000)
&& ((error_text.indexOf("SOCKET") > -1) // for control socket error
|| (error_text.indexOf("CONNECTION HAS ALREADY BEEN CLOSED") > -1) || (error_text.indexOf("BROKEN PIPE") > -1))) {
return true;
if ((error_code < 20000 || error_code >= 21000)) {
if ((error_text.indexOf("SOCKET") > -1) // for control socket error
|| (error_text.indexOf("套接字") > -1) // for control socket error
|| (error_text.indexOf("CONNECTION HAS ALREADY BEEN CLOSED") > -1) //
|| (error_text.indexOf("BROKEN PIPE") > -1) //
|| (error_text.indexOf("管道已结束") > -1) //
) {
return true;
}
}
return false;
return fatalErrorCodes.contains(error_code);
}
}
@@ -94,7 +94,9 @@ public class PagerUtils {
private static String limitQueryBlock(SQLSelect select, String dbType, int offset, int count) {
SQLSelectQueryBlock queryBlock = (SQLSelectQueryBlock) select.getQuery();
if (JdbcConstants.MYSQL.equals(dbType)) {
if (JdbcConstants.MYSQL.equals(dbType) || //
JdbcConstants.MARIADB.equals(dbType) || //
JdbcConstants.H2.equals(dbType)) {
return limitMySqlQueryBlock((MySqlSelectQueryBlock) queryBlock, dbType, offset, count);
}
@@ -205,6 +207,14 @@ public class PagerUtils {
return new MySqlSelectQueryBlock();
}
if (JdbcConstants.MARIADB.equals(dbType)) {
return new MySqlSelectQueryBlock();
}
if (JdbcConstants.H2.equals(dbType)) {
return new MySqlSelectQueryBlock();
}
if (JdbcConstants.ORACLE.equals(dbType)) {
return new OracleSelectQueryBlock();
}
@@ -45,11 +45,9 @@ public class SQLUtils {
private final static Log LOG = LogFactory.getLog(SQLUtils.class);
public static String toSQLString(SQLObject sqlObject, String dbType) {
if (JdbcUtils.MYSQL.equals(dbType)) {
return toMySqlString(sqlObject);
}
if (JdbcUtils.H2.equals(dbType)) {
if (JdbcUtils.MYSQL.equals(dbType) || //
JdbcUtils.MARIADB.equals(dbType) || //
JdbcUtils.H2.equals(dbType)) {
return toMySqlString(sqlObject);
}
@@ -139,7 +137,7 @@ public class SQLUtils {
public static SQLExpr toSQLExpr(String sql) {
return toSQLExpr(sql, null);
}
public static String format(String sql, String dbType) {
return format(sql, dbType, null);
}
@@ -158,7 +156,7 @@ public class SQLUtils {
public static String toSQLString(List<SQLStatement> statementList, String dbType) {
return toSQLString(statementList, dbType, null);
}
public static String toSQLString(List<SQLStatement> statementList, String dbType, List<Object> parameters) {
StringBuilder out = new StringBuilder();
SQLASTOutputVisitor visitor = createFormatOutputVisitor(out, statementList, dbType);
@@ -183,7 +181,9 @@ public class SQLUtils {
}
}
if (JdbcUtils.MYSQL.equals(dbType)) {
if (JdbcUtils.MYSQL.equals(dbType) || //
JdbcUtils.MARIADB.equals(dbType) || //
JdbcUtils.H2.equals(dbType)) {
return new MySqlOutputVisitor(out);
}
@@ -194,15 +194,11 @@ public class SQLUtils {
if (JdbcUtils.SQL_SERVER.equals(dbType)) {
return new SQLServerOutputVisitor(out);
}
if (JdbcUtils.JTDS.equals(dbType)) {
return new SQLServerOutputVisitor(out);
}
if (JdbcUtils.H2.equals(dbType)) {
return new MySqlOutputVisitor(out);
}
return new SQLASTOutputVisitor(out);
}
@@ -215,7 +211,9 @@ public class SQLUtils {
}
}
if (JdbcUtils.MYSQL.equals(dbType)) {
if (JdbcUtils.MYSQL.equals(dbType) || //
JdbcUtils.MARIADB.equals(dbType) || //
JdbcUtils.H2.equals(dbType)) {
return new MySqlSchemaStatVisitor();
}
@@ -226,15 +224,11 @@ public class SQLUtils {
if (JdbcUtils.SQL_SERVER.equals(dbType)) {
return new SQLServerSchemaStatVisitor();
}
if (JdbcUtils.JTDS.equals(dbType)) {
return new SQLServerSchemaStatVisitor();
}
if (JdbcUtils.H2.equals(dbType)) {
return new MySqlSchemaStatVisitor();
}
return new SchemaStatVisitor();
}
@@ -36,6 +36,10 @@ public class SQLParserUtils {
return new MySqlStatementParser(sql);
}
if (JdbcUtils.MARIADB.equals(dbType)) {
return new MySqlStatementParser(sql);
}
if (JdbcUtils.POSTGRESQL.equals(dbType)) {
return new PGSQLStatementParser(sql);
}
@@ -43,7 +47,7 @@ public class SQLParserUtils {
if (JdbcUtils.SQL_SERVER.equals(dbType)) {
return new SQLServerStatementParser(sql);
}
if (JdbcUtils.JTDS.equals(dbType)) {
return new SQLServerStatementParser(sql);
}
@@ -60,11 +64,9 @@ public class SQLParserUtils {
return new OracleExprParser(sql);
}
if (JdbcUtils.H2.equals(dbType)) {
return new MySqlExprParser(sql);
}
if (JdbcUtils.MYSQL.equals(dbType)) {
if (JdbcUtils.MYSQL.equals(dbType) || //
JdbcUtils.MARIADB.equals(dbType) || //
JdbcUtils.H2.equals(dbType)) {
return new MySqlExprParser(sql);
}
@@ -75,7 +77,7 @@ public class SQLParserUtils {
if (JdbcUtils.SQL_SERVER.equals(dbType)) {
return new SQLServerExprParser(sql);
}
if (JdbcUtils.JTDS.equals(dbType)) {
return new SQLServerExprParser(sql);
}
@@ -56,6 +56,10 @@ public class ParameterizedOutputVisitorUtils {
if (JdbcUtils.MYSQL.equals(dbType)) {
return new MySqlParameterizedOutputVisitor(out);
}
if (JdbcUtils.MARIADB.equals(dbType)) {
return new MySqlParameterizedOutputVisitor(out);
}
if (JdbcUtils.H2.equals(dbType)) {
return new MySqlParameterizedOutputVisitor(out);
@@ -139,6 +139,10 @@ public class SQLEvalVisitorUtils {
if (JdbcUtils.MYSQL.equals(dbType)) {
return new MySqlEvalVisitorImpl();
}
if (JdbcUtils.MARIADB.equals(dbType)) {
return new MySqlEvalVisitorImpl();
}
if (JdbcUtils.H2.equals(dbType)) {
return new MySqlEvalVisitorImpl();
@@ -58,7 +58,7 @@ public class LogFactory {
logConstructor = null;
}
} catch (Throwable t) {
t.printStackTrace();
// skip
}
}
@@ -34,16 +34,17 @@ public interface JdbcConstants {
public static final String SQL_SERVER = "sqlserver";
public static final String ORACLE = "oracle";
public static final String ALI_ORACLE = "AliOracle";
public static final String ORACLE_DRIVER = "oracle.jdbc.driver.OracleDriver";
public static final String ALI_ORACLE = "AliOracle";
public static final String ALI_ORACLE_DRIVER = "com.alibaba.jdbc.AlibabaDriver";
public static final String MYSQL = "mysql";
public static final String MYSQL_DRIVER = "com.mysql.jdbc.Driver";
public static final String MARIADB = "mariadb";
public static final String MARIADB_DRIVER = "org.mariadb.jdbc.Driver";
public static final String DERBY = "derby";
public static final String HBASE = "hbase";
@@ -348,6 +348,8 @@ public final class JdbcUtils implements JdbcConstants {
return "org.apache.derby.jdbc.EmbeddedDriver";
} else if (rawUrl.startsWith("jdbc:mysql:")) {
return MYSQL_DRIVER;
} else if (rawUrl.startsWith("jdbc:mariadb:")) {
return MARIADB_DRIVER;
} else if (rawUrl.startsWith("jdbc:oracle:")) {
return ORACLE_DRIVER;
} else if (rawUrl.startsWith("jdbc:alibaba:oracle:")) {
@@ -414,6 +416,8 @@ public final class JdbcUtils implements JdbcConstants {
return DERBY;
} else if (rawUrl.startsWith("jdbc:mysql:")) {
return MYSQL;
} else if (rawUrl.startsWith("jdbc:mariadb:")) {
return MARIADB;
} else if (rawUrl.startsWith("jdbc:oracle:")) {
return ORACLE;
} else if (rawUrl.startsWith("jdbc:alibaba:oracle:")) {
@@ -31,7 +31,7 @@ public class WallConfig implements WallConfigMBean {
private boolean selectIntoAllow = true;
private boolean selectIntoOutfileAllow = false;
private boolean selectWhereAlwayTrueCheck = true;
private boolean selectHavingAlwayTrueCheck = true;
private boolean selectHavingAlwayTrueCheck = false;
private boolean selectUnionCheck = true;
private boolean createTableAllow = true;
private boolean dropTableAllow = true;
@@ -72,7 +72,9 @@ public class WallFilter extends FilterAdapter implements WallFilterMBean {
dbType = JdbcUtils.getDbType(dataSource.getUrl(), null);
}
if (JdbcUtils.MYSQL.equals(dbType) || JdbcUtils.H2.equals(dbType)) {
if (JdbcUtils.MYSQL.equals(dbType) || //
JdbcUtils.MARIADB.equals(dbType) || //
JdbcUtils.H2.equals(dbType)) {
if (config == null) {
config = new WallConfig(MySqlWallProvider.DEFAULT_CONFIG_DIR);
}
@@ -541,7 +543,7 @@ public class WallFilter extends FilterAdapter implements WallFilterMBean {
if (sqlStat == null) {
return;
}
Map<String, WallSqlTableStat> sqlTableStats = sqlStat.getTableStats();
if (sqlTableStats == null) {
return;
@@ -63,6 +63,7 @@ import com.alibaba.druid.wall.WallContext;
import com.alibaba.druid.wall.WallProvider;
import com.alibaba.druid.wall.WallSqlTableStat;
import com.alibaba.druid.wall.WallVisitor;
import com.alibaba.druid.wall.spi.WallVisitorUtils.WallConditionContext;
import com.alibaba.druid.wall.violation.ErrorCode;
import com.alibaba.druid.wall.violation.IllegalSQLObjectViolation;
@@ -255,8 +256,8 @@ public class MySqlWallVisitor extends MySqlASTVisitorAdapter implements WallVisi
}
if (varName.startsWith("@@") && !checkVar(x.getParent(), x.getName())) {
boolean isTop = WallVisitorUtils.isTopNoneFromSelect(this, x);
if (!isTop) {
boolean noneCondition = WallVisitorUtils.getWallConditionContext() == null;
if (!noneCondition) {
violations.add(new IllegalSQLObjectViolation(ErrorCode.VARIANT_DENY, "variable not allow : "
+ x.getName(), toSQL(x)));
}
@@ -678,7 +678,9 @@ public class WallVisitorUtils {
WallContext context = WallContext.current();
if (context != null) {
if (current.hasPartAlwayTrue() || Boolean.TRUE == value) {
context.incrementWarnnings();
if (!isFirst(x)) {
context.incrementWarnnings();
}
}
}