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:
@@ -24,12 +24,32 @@ public class MySqlExceptionSorter implements ExceptionSorter {
|
||||
|
||||
@Override
|
||||
public boolean isExceptionFatal(SQLException e) {
|
||||
int loopCount = 20;
|
||||
|
||||
Throwable cause = e;
|
||||
while (cause != null) {
|
||||
if (cause instanceof SQLException) {
|
||||
SQLException sqlException = (SQLException) cause;
|
||||
|
||||
if (isExceptionFatal0(sqlException)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
cause = cause.getCause();
|
||||
if (--loopCount < 0) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private boolean isExceptionFatal0(SQLException e) {
|
||||
String sqlState = e.getSQLState();
|
||||
if (sqlState != null && sqlState.startsWith("08")) { // per Mark Matthews at MySQL
|
||||
final int errorCode = Math.abs(e.getErrorCode());
|
||||
|
||||
if (sqlState != null && sqlState.startsWith("08")) {
|
||||
return true;
|
||||
}
|
||||
|
||||
final int errorCode = e.getErrorCode();
|
||||
switch (errorCode) {
|
||||
// Communications Errors
|
||||
case 1040: // ER_CON_COUNT_ERROR
|
||||
@@ -39,35 +59,32 @@ public class MySqlExceptionSorter implements ExceptionSorter {
|
||||
case 1081: // ER_IPSOCK_ERROR
|
||||
case 1129: // ER_HOST_IS_BLOCKED
|
||||
case 1130: // ER_HOST_NOT_PRIVILEGED
|
||||
|
||||
// Authentication Errors
|
||||
case 1045: // ER_ACCESS_DENIED_ERROR
|
||||
|
||||
// Resource errors
|
||||
case 1004: // ER_CANT_CREATE_FILE
|
||||
case 1005: // ER_CANT_CREATE_TABLE
|
||||
case 1015: // ER_CANT_LOCK
|
||||
case 1021: // ER_DISK_FULL
|
||||
case 1041: // ER_OUT_OF_RESOURCES
|
||||
|
||||
// Out-of-memory errors
|
||||
case 1037: // ER_OUTOFMEMORY
|
||||
case 1038: // ER_OUT_OF_SORTMEMORY
|
||||
return true;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
if (errorCode >= -10000 && errorCode <= -9000) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (StringUtils.isNotBlank(e.getMessage())) {
|
||||
final String errorText = e.getMessage().toUpperCase();
|
||||
|
||||
if (errorCode == 0
|
||||
&& (errorText.indexOf("COMMUNICATIONS LINK FAILURE") > -1 || errorText
|
||||
.indexOf("COULD NOT CREATE CONNECTION") > -1)
|
||||
|| errorText.indexOf("NO DATASOURCE") > -1
|
||||
|| errorText.indexOf("NO ALIVE DATASOURCE") > -1) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public void configFromProperties(Properties properties) {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -39,6 +39,9 @@ import com.alibaba.druid.sql.visitor.SchemaStatVisitor;
|
||||
import com.alibaba.druid.support.logging.Log;
|
||||
import com.alibaba.druid.support.logging.LogFactory;
|
||||
import com.alibaba.druid.util.JdbcUtils;
|
||||
import com.alibaba.druid.util.JdbcConstants;
|
||||
import com.alibaba.druid.util.StringUtils;
|
||||
|
||||
|
||||
public class SQLUtils {
|
||||
|
||||
@@ -240,4 +243,37 @@ public class SQLUtils {
|
||||
}
|
||||
return stmtList;
|
||||
}
|
||||
|
||||
/**
|
||||
* @author owenludong.lud
|
||||
* @param columnName
|
||||
* @param tableAlias
|
||||
* @param pattern if pattern is null,it will be set {%Y-%m-%d %H:%i:%s} as mysql default value and set {yyyy-mm-dd hh24:mi:ss} as oracle default value
|
||||
* @param dbType {@link JdbcConstants} if dbType is null ,it will be set the mysql as a default value
|
||||
*/
|
||||
public static String buildToDate(String columnName,String tableAlias,String pattern,String dbType){
|
||||
StringBuilder sql = new StringBuilder();
|
||||
if(StringUtils.isEmpty(columnName))
|
||||
return "";
|
||||
if(StringUtils.isEmpty(dbType)) dbType = JdbcConstants.MYSQL;
|
||||
String formatMethod = "";
|
||||
if(JdbcConstants.MYSQL.equalsIgnoreCase(dbType)){
|
||||
formatMethod = "STR_TO_DATE";
|
||||
if(StringUtils.isEmpty(pattern)) pattern = "%Y-%m-%d %H:%i:%s";
|
||||
}else if(JdbcConstants.ORACLE.equalsIgnoreCase(dbType)){
|
||||
formatMethod = "TO_DATE";
|
||||
if(StringUtils.isEmpty(pattern)) pattern = "yyyy-mm-dd hh24:mi:ss";
|
||||
}else{
|
||||
return "";
|
||||
//expand date's handle method for other database
|
||||
}
|
||||
sql.append(formatMethod).append("(");
|
||||
if(!StringUtils.isEmpty(tableAlias))
|
||||
sql.append(tableAlias).append(".");
|
||||
sql.append(columnName).append(",");
|
||||
sql.append("'");
|
||||
sql.append(pattern);
|
||||
sql.append("')");
|
||||
return sql.toString();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user