FileDocCategorySizeDatePackage
MySQLValidConnectionChecker.javaAPI DocJBoss 4.2.13774Fri Jul 13 21:01:14 BST 2007org.jboss.resource.adapter.jdbc.vendor

MySQLValidConnectionChecker

public class MySQLValidConnectionChecker extends Object implements Serializable, org.jboss.resource.adapter.jdbc.ValidConnectionChecker
Implements check valid connection sql Requires MySQL driver 3.1.8 or later. This should work on just about any version of the database itself but will only be "fast" on version 3.22.1 and later. Prior to that version it just does "SELECT 1" anyhow.
author
Adrian Brock
author
Andrew C. Oliver
author
Jim Moran
version
$Revision: 57189 $

Fields Summary
private static final long
serialVersionUID
private static final Logger
log
private Method
ping
private boolean
driverHasPingMethod
private static Object[]
params
Constructors Summary
public MySQLValidConnectionChecker()


	  
		try {
			Class mysqlConnection = Thread.currentThread().getContextClassLoader().loadClass("com.mysql.jdbc.Connection");
			ping = mysqlConnection.getMethod("ping", new Class[] {});
			if (ping != null) {
				driverHasPingMethod = true;
			}
		} catch (Exception e) {
			log.warn("Cannot resolve com.mysq.jdbc.Connection.ping method.  Will use 'SELECT 1' instead.", e);
		}
	
Methods Summary
public java.sql.SQLExceptionisValidConnection(java.sql.Connection c)

		//if there is a ping method then use it, otherwise just use a 'SELECT 1' statement
		if (driverHasPingMethod) {
			try {
				ping.invoke(c, params);
			} catch (Exception e) {
				if (e instanceof SQLException) {
					return (SQLException) e;
				} else {
					log.warn("Unexpected error in ping", e);
					return new SQLException("ping failed: " + e.toString());
				}
			}
			
		} else {
			
			Statement stmt = null;
                        ResultSet rs = null;
			try {
				stmt = c.createStatement();
				rs = stmt.executeQuery("SELECT 1");
			} catch (Exception e) {
				if (e instanceof SQLException) {
					return (SQLException) e;
				} else {
					log.warn("Unexpected error in ping (SELECT 1)", e);
					return new SQLException("ping (SELECT 1) failed: " + e.toString());
				}	
			} finally {
				//cleanup the Statment
				try {
                                        if (rs != null) rs.close();
					if (stmt != null) stmt.close();
				} catch (SQLException e) {
				}
			}
			
		}
		return null;