2
Copyright (C) 2002-2006 MySQL AB
4
This program is free software; you can redistribute it and/or modify
5
it under the terms of version 2 of the GNU General Public License as
6
published by the Free Software Foundation.
8
There are special exceptions to the terms and conditions of the GPL
9
as it is applied to this software. View the full text of the
10
exception in file EXCEPTIONS-CONNECTOR-J in the directory of this
11
software distribution.
13
This program is distributed in the hope that it will be useful,
14
but WITHOUT ANY WARRANTY; without even the implied warranty of
15
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
GNU General Public License for more details.
18
You should have received a copy of the GNU General Public License
19
along with this program; if not, write to the Free Software
20
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23
package com.mysql.jdbc.integration.jboss;
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;
31
import org.jboss.resource.adapter.jdbc.ValidConnectionChecker;
33
import com.mysql.jdbc.SQLError;
36
* A more efficient connection checker for JBoss.
38
* @version $Id: MysqlValidConnectionChecker.java,v 1.1.2.1 2005/05/13 18:58:42
41
public final class MysqlValidConnectionChecker implements
42
ValidConnectionChecker, Serializable {
44
private static final long serialVersionUID = 3258689922776119348L;
46
private Method pingMethod;
48
private Method pingMethodWrapped;
50
private final static Object[] NO_ARGS_OBJECT_ARRAY = new Object[0];
52
public MysqlValidConnectionChecker() {
54
// Avoid classloader goofiness
55
Class mysqlConnection = Thread.currentThread()
56
.getContextClassLoader().loadClass(
57
"com.mysql.jdbc.Connection");
59
pingMethod = mysqlConnection.getMethod("ping", null);
61
Class mysqlConnectionWrapper = Thread.currentThread()
62
.getContextClassLoader().loadClass(
63
"com.mysql.jdbc.jdbc2.optional.ConnectionWrapper");
65
pingMethodWrapped = mysqlConnectionWrapper.getMethod("ping", null);
66
} catch (Exception ex) {
67
// Punt, we'll use 'SELECT 1' to do the check
74
* @see org.jboss.resource.adapter.jdbc.ValidConnectionChecker#isValidConnection(java.sql.Connection)
76
public SQLException isValidConnection(Connection conn) {
77
if (conn instanceof com.mysql.jdbc.Connection) {
78
if (pingMethod != null) {
80
this.pingMethod.invoke(conn, NO_ARGS_OBJECT_ARRAY);
83
} catch (Exception ex) {
84
if (ex instanceof SQLException) {
85
return (SQLException) ex;
88
return SQLError.createSQLException("Ping failed: " + ex.toString());
91
} else if (conn instanceof com.mysql.jdbc.jdbc2.optional.ConnectionWrapper) {
92
if (pingMethodWrapped != null) {
94
this.pingMethodWrapped.invoke(conn, NO_ARGS_OBJECT_ARRAY);
97
} catch (Exception ex) {
98
if (ex instanceof SQLException) {
99
return (SQLException) ex;
102
return SQLError.createSQLException("Ping failed: " + ex.toString());
107
// Punt and use 'SELECT 1'
109
Statement pingStatement = null;
112
pingStatement = conn.createStatement();
114
pingStatement.executeQuery("SELECT 1").close();
117
} catch (SQLException sqlEx) {
120
if (pingStatement != null) {
122
pingStatement.close();
123
} catch (SQLException sqlEx) {
124
// can't do anything about it here