I am trying to make a JDBC
connection pool component, but I have met a problem. That is: How to detect the error state of java.sql.SQLException
.
It's said that the state should follow SQL2003
conventions and XOPEN SQL
conventions in JDK
documentations. But I can't find any documents about these two conventions.Could someone provide it for me?
I want to know what every state stands for, so I can decide when to close the connection fully or reconnect.
I have refered to source code of BoneCP
. Here is a part which will be actived when a SQLException occurs :
ImmutableSet<String> sqlStateDBFailureCodes = ImmutableSet.of("08001", "08007", "08S01", "57P01", "HY000");
String state = e.getSQLState();
ConnectionState connectionState = this.getConnectionHook() != null ? this.getConnectionHook().onMarkPossiblyBroken(this, state, e) : ConnectionState.NOP;
if (state == null){ // safety;
state = "08999";
}
if (((sqlStateDBFailureCodes.contains(state) || connectionState.equals(ConnectionState.TERMINATE_ALL_CONNECTIONS)) && this.pool != null) && this.pool.getDbIsDown().compareAndSet(false, true) ){
logger.error("Database access problem. Killing off all remaining connections in the connection pool. SQL State = " + state);
this.pool.connectionStrategy.terminateAllConnections();
this.pool.poisonAndRepopulatePartitions();
}
char firstChar = state.charAt(0);
if (connectionState.equals(ConnectionState.CONNECTION_POSSIBLY_BROKEN) || state.equals("40001") ||
state.startsWith("08") || (firstChar >= '5' && firstChar <='9') /*|| (firstChar >='I' && firstChar <= 'Z')*/){
this.possiblyBroken = true;
}
// Notify anyone who's interested
if (this.possiblyBroken && (this.getConnectionHook() != null)){
this.possiblyBroken = this.getConnectionHook().onConnectionException(this, state, e);
}
According to these code, boneCP
considers it as a database server crash when the state of SQLException equals one of "08001", "08007", "08S01", "57P01", "HY000".
But why, what do these states stand for ?