1 23 package com.mysql.jdbc.integration.c3p0; 24 25 import java.lang.reflect.Method ; 26 import java.sql.Connection ; 27 import java.sql.SQLException ; 28 import java.sql.Statement ; 29 30 import com.mchange.v2.c3p0.C3P0ProxyConnection; 31 import com.mchange.v2.c3p0.QueryConnectionTester; 32 import com.mysql.jdbc.CommunicationsException; 33 34 43 public final class MysqlConnectionTester implements QueryConnectionTester { 44 45 private static final long serialVersionUID = 3256444690067896368L; 46 47 private static final Object [] NO_ARGS_ARRAY = new Object [0]; 48 49 private Method pingMethod; 50 51 public MysqlConnectionTester() { 52 try { 53 pingMethod = com.mysql.jdbc.Connection.class 54 .getMethod("ping", null); 55 } catch (Exception ex) { 56 } 60 } 61 62 67 public int activeCheckConnection(Connection con) { 68 C3P0ProxyConnection castCon = (C3P0ProxyConnection) con; 69 70 try { 71 if (pingMethod != null) { 72 castCon.rawConnectionOperation(pingMethod, 73 C3P0ProxyConnection.RAW_CONNECTION, NO_ARGS_ARRAY); 74 } else { 75 Statement pingStatement = null; 76 77 try { 78 pingStatement = con.createStatement(); 79 pingStatement.executeQuery("SELECT 1").close(); 80 } finally { 81 if (pingStatement != null) { 82 pingStatement.close(); 83 } 84 } 85 } 86 87 return CONNECTION_IS_OKAY; 88 } catch (Exception ex) { 89 return CONNECTION_IS_INVALID; 90 } 91 } 92 93 99 public int statusOnException(Connection arg0, Throwable throwable) { 100 if (throwable instanceof CommunicationsException) { 101 return CONNECTION_IS_INVALID; 102 } 103 104 if (throwable instanceof SQLException ) { 105 String sqlState = ((SQLException ) throwable).getSQLState(); 106 107 if (sqlState != null && sqlState.startsWith("08")) { 108 return CONNECTION_IS_INVALID; 109 } 110 111 return CONNECTION_IS_OKAY; 112 } 113 114 116 return CONNECTION_IS_INVALID; 117 } 118 119 125 public int activeCheckConnection(Connection arg0, String arg1) { 126 return CONNECTION_IS_OKAY; 127 } 128 } 129 | Popular Tags |