add double check for mysql testWhileIdle.

This commit is contained in:
wenshao
2017-11-01 12:24:22 +08:00
parent d1189cefe6
commit 616ec0d431
5 changed files with 173 additions and 35 deletions
@@ -15,6 +15,7 @@
*/
package com.alibaba.druid.pool;
import com.alibaba.druid.VERSION;
import com.alibaba.druid.support.logging.Log;
import com.alibaba.druid.support.logging.LogFactory;
import com.alibaba.druid.util.JdbcConstants;
@@ -225,6 +226,8 @@ public class DruidPooledStatement extends PoolableWrapper implements Statement {
return poolableResultSet;
} catch (Throwable t) {
errorCheck(t);
throw checkException(t);
} finally {
conn.afterExecute();
@@ -242,12 +245,33 @@ public class DruidPooledStatement extends PoolableWrapper implements Statement {
try {
return stmt.executeUpdate(sql);
} catch (Throwable t) {
errorCheck(t);
throw checkException(t);
} finally {
conn.afterExecute();
}
}
protected final void errorCheck(Throwable t) {
String errorClassName = t.getClass().getName();
if (errorClassName.endsWith(".CommunicationsException")
&& conn.holder != null
&& conn.holder.dataSource.testWhileIdle)
{
DruidConnectionHolder holder = conn.holder;
DruidAbstractDataSource dataSource = holder.dataSource;
long lastActiveTimeMillis = holder.lastActiveTimeMillis;
long idleMillis = System.currentTimeMillis() - lastActiveTimeMillis;
LOG.error("CommunicationsException, druid version " + VERSION.getVersionNumber()
+ ", jdbcUrl : " + dataSource.jdbcUrl
+ ", testWhileIdle " + dataSource.testWhileIdle
+ ", idle millis " + idleMillis);
}
}
@Override
public final int executeUpdate(String sql, int autoGeneratedKeys) throws SQLException {
checkOpen();
@@ -259,6 +283,8 @@ public class DruidPooledStatement extends PoolableWrapper implements Statement {
try {
return stmt.executeUpdate(sql, autoGeneratedKeys);
} catch (Throwable t) {
errorCheck(t);
throw checkException(t);
} finally {
conn.afterExecute();
@@ -276,6 +302,8 @@ public class DruidPooledStatement extends PoolableWrapper implements Statement {
try {
return stmt.executeUpdate(sql, columnIndexes);
} catch (Throwable t) {
errorCheck(t);
throw checkException(t);
} finally {
conn.afterExecute();
@@ -293,6 +321,8 @@ public class DruidPooledStatement extends PoolableWrapper implements Statement {
try {
return stmt.executeUpdate(sql, columnNames);
} catch (Throwable t) {
errorCheck(t);
throw checkException(t);
} finally {
conn.afterExecute();
@@ -310,6 +340,8 @@ public class DruidPooledStatement extends PoolableWrapper implements Statement {
try {
return stmt.execute(sql, autoGeneratedKeys);
} catch (Throwable t) {
errorCheck(t);
throw checkException(t);
} finally {
conn.afterExecute();
@@ -327,6 +359,8 @@ public class DruidPooledStatement extends PoolableWrapper implements Statement {
try {
return stmt.execute(sql, columnIndexes);
} catch (Throwable t) {
errorCheck(t);
throw checkException(t);
} finally {
conn.afterExecute();
@@ -344,6 +378,8 @@ public class DruidPooledStatement extends PoolableWrapper implements Statement {
try {
return stmt.execute(sql, columnNames);
} catch (Throwable t) {
errorCheck(t);
throw checkException(t);
} finally {
conn.afterExecute();
@@ -496,6 +532,8 @@ public class DruidPooledStatement extends PoolableWrapper implements Statement {
try {
return stmt.execute(sql);
} catch (Throwable t) {
errorCheck(t);
throw checkException(t);
}
}
@@ -653,6 +691,8 @@ public class DruidPooledStatement extends PoolableWrapper implements Statement {
try {
return stmt.executeBatch();
} catch (Throwable t) {
errorCheck(t);
throw checkException(t);
} finally {
conn.afterExecute();