1 22 23 package com.mysql.jdbc.integration.jboss; 24 25 import java.io.Serializable ; 26 import java.lang.reflect.Method ; 27 import java.sql.Connection ; 28 import java.sql.SQLException ; 29 import java.sql.Statement ; 30 31 import org.jboss.resource.adapter.jdbc.ValidConnectionChecker; 32 33 39 public final class MysqlValidConnectionChecker implements 40 ValidConnectionChecker, Serializable { 41 42 private static final long serialVersionUID = 3258689922776119348L; 43 44 private Method pingMethod; 45 46 private final static Object [] NO_ARGS_OBJECT_ARRAY = new Object [0]; 47 48 public MysqlValidConnectionChecker() { 49 try { 50 Class mysqlConnection = Thread.currentThread() 52 .getContextClassLoader().loadClass( 53 "com.mysql.jdbc.Connection"); 54 55 pingMethod = mysqlConnection.getMethod("ping", null); 56 } catch (Exception ex) { 57 } 59 } 60 61 66 public SQLException isValidConnection(Connection conn) { 67 if (pingMethod != null) { 68 try { 69 this.pingMethod.invoke(conn, NO_ARGS_OBJECT_ARRAY); 70 71 return null; 72 } catch (Exception ex) { 73 if (ex instanceof SQLException ) { 74 return (SQLException ) ex; 75 } 76 77 return new SQLException ("Ping failed: " + ex.toString()); 78 } 79 } 80 81 83 Statement pingStatement = null; 84 85 try { 86 pingStatement.executeQuery("SELECT 1").close(); 87 88 return null; 89 } catch (SQLException sqlEx) { 90 return sqlEx; 91 } finally { 92 if (pingStatement != null) { 93 try { 94 pingStatement.close(); 95 } catch (SQLException sqlEx) { 96 } 98 } 99 } 100 } 101 } 102 | Popular Tags |