1 22 23 24 package com.mchange.v2.c3p0.impl; 25 26 import java.sql.*; 27 import java.util.*; 28 import com.mchange.v2.log.*; 29 import com.mchange.v2.c3p0.AbstractConnectionTester; 30 import com.mchange.v2.c3p0.FullQueryConnectionTester; 31 import com.mchange.v1.db.sql.ResultSetUtils; 32 import com.mchange.v1.db.sql.StatementUtils; 33 34 public class DefaultConnectionTester extends AbstractConnectionTester 35 { 36 final static MLogger logger = MLog.getLogger( DefaultConnectionTester.class ); 37 38 final static int HASH_CODE = DefaultConnectionTester.class.getName().hashCode(); 39 40 final static Set INVALID_DB_STATES; 41 42 static 43 { 44 Set temp = new HashSet(); 45 temp.add("08001"); temp.add("08007"); 48 53 INVALID_DB_STATES = Collections.unmodifiableSet( temp ); 54 } 55 56 public int activeCheckConnection(Connection c, String query, Throwable [] rootCauseOutParamHolder) 57 { 58 61 if (query == null) 62 return activeCheckConnectionNoQuery( c, rootCauseOutParamHolder); 63 else 64 { 65 Statement stmt = null; 66 ResultSet rs = null; 67 try 68 { 69 72 stmt = c.createStatement(); 73 rs = stmt.executeQuery( query ); 74 return CONNECTION_IS_OKAY; 76 } 77 catch (SQLException e) 78 { 79 if (Debug.DEBUG && logger.isLoggable( MLevel.FINE ) ) 80 logger.log( MLevel.FINE, "Connection " + c + " failed Connection test with an Exception! [query=" + query + "]", e ); 81 82 if (rootCauseOutParamHolder != null) 83 rootCauseOutParamHolder[0] = e; 84 85 String state = e.getSQLState(); 86 if ( INVALID_DB_STATES.contains( state ) ) 87 { 88 if (logger.isLoggable(MLevel.WARNING)) 89 logger.log(MLevel.WARNING, 90 "SQL State '" + state + 91 "' of Exception which occurred during a Connection test (test with query '" + query + 92 "') implies that the database is invalid, " + 93 "and the pool should refill itself with fresh Connections.", e); 94 return DATABASE_IS_INVALID; 95 } 96 else 97 return CONNECTION_IS_INVALID; 98 } 99 catch (Exception e) 100 { 101 if ( Debug.DEBUG && logger.isLoggable( MLevel.FINE )) 102 logger.log( MLevel.FINE, "Connection " + c + " failed Connection test with an Exception!", e ); 103 104 if (rootCauseOutParamHolder != null) 105 rootCauseOutParamHolder[0] = e; 106 107 return CONNECTION_IS_INVALID; 108 } 109 finally 110 { 111 ResultSetUtils.attemptClose( rs ); 112 StatementUtils.attemptClose( stmt ); 113 114 } 117 } 118 } 119 120 public int statusOnException(Connection c, Throwable t, String query, Throwable [] rootCauseOutParamHolder) 121 { 122 125 if (Debug.DEBUG && Debug.TRACE == Debug.TRACE_MAX && logger.isLoggable( MLevel.FINER ) ) 126 logger.log(MLevel.FINER, "Testing a Connection in response to an Exception:", t); 127 128 try 129 { 130 if (t instanceof SQLException) 131 { 132 String state = ((SQLException) t).getSQLState(); 133 if ( INVALID_DB_STATES.contains( state ) ) 134 { 135 if (logger.isLoggable(MLevel.WARNING)) 136 logger.log(MLevel.WARNING, 137 "SQL State '" + state + 138 "' of Exception tested by statusOnException() implies that the database is invalid, " + 139 "and the pool should refill itself with fresh Connections.", t); 140 return DATABASE_IS_INVALID; 141 } 142 else 143 return activeCheckConnection(c, query, rootCauseOutParamHolder); 144 } 145 else { 147 if ( logger.isLoggable( MLevel.FINE ) ) 148 logger.log( MLevel.FINE, "Connection test failed because test-provoking Throwable is an unexpected, non-SQLException.", t); 149 if (rootCauseOutParamHolder != null) 150 rootCauseOutParamHolder[0] = t; 151 return CONNECTION_IS_INVALID; 152 } 153 } 154 catch (Exception e) 155 { 156 if ( Debug.DEBUG && logger.isLoggable( MLevel.FINE )) 157 logger.log( MLevel.FINE, "Connection " + c + " failed Connection test with an Exception!", e ); 158 159 if (rootCauseOutParamHolder != null) 160 rootCauseOutParamHolder[0] = e; 161 162 return CONNECTION_IS_INVALID; 163 } 164 finally 165 { 166 } 172 } 173 174 private static String queryInfo(String query) 175 { return (query == null ? "[using default system-table query]" : "[query=" + query + "]"); } 176 177 private int activeCheckConnectionNoQuery(Connection c, Throwable [] rootCauseOutParamHolder) 178 { 179 182 ResultSet rs = null; 183 try 184 { 185 rs = c.getMetaData().getTables( null, 186 null, 187 "PROBABLYNOT", 188 new String [] {"TABLE"} ); 189 return CONNECTION_IS_OKAY; 190 } 191 catch (SQLException e) 192 { 193 if ( Debug.DEBUG && logger.isLoggable( MLevel.FINE )) 194 logger.log( MLevel.FINE, "Connection " + c + " failed default system-table Connection test with an Exception!", e ); 195 196 if (rootCauseOutParamHolder != null) 197 rootCauseOutParamHolder[0] = e; 198 199 String state = e.getSQLState(); 200 if ( INVALID_DB_STATES.contains( state ) ) 201 { 202 if (logger.isLoggable(MLevel.WARNING)) 203 logger.log(MLevel.WARNING, 204 "SQL State '" + state + 205 "' of Exception which occurred during a Connection test (fallback DatabaseMetaData test) implies that the database is invalid, " + 206 "and the pool should refill itself with fresh Connections.", e); 207 return DATABASE_IS_INVALID; 208 } 209 else 210 return CONNECTION_IS_INVALID; 211 } 212 catch (Exception e) 213 { 214 if ( Debug.DEBUG && logger.isLoggable( MLevel.FINE )) 215 logger.log( MLevel.FINE, "Connection " + c + " failed default system-table Connection test with an Exception!", e ); 216 217 if (rootCauseOutParamHolder != null) 218 rootCauseOutParamHolder[0] = e; 219 220 return CONNECTION_IS_INVALID; 221 } 222 finally 223 { 224 ResultSetUtils.attemptClose( rs ); 225 } 228 } 229 230 231 public boolean equals( Object o ) 232 { return ( o != null && o.getClass() == DefaultConnectionTester.class ); } 233 234 public int hashCode() 235 { return HASH_CODE; } 236 } 237 238 | Popular Tags |