001: /*
002: Copyright (C) 2002-2006 MySQL AB
003:
004: This program is free software; you can redistribute it and/or modify
005: it under the terms of version 2 of the GNU General Public License as
006: published by the Free Software Foundation.
007:
008: There are special exceptions to the terms and conditions of the GPL
009: as it is applied to this software. View the full text of the
010: exception in file EXCEPTIONS-CONNECTOR-J in the directory of this
011: software distribution.
012:
013: This program is distributed in the hope that it will be useful,
014: but WITHOUT ANY WARRANTY; without even the implied warranty of
015: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016: GNU General Public License for more details.
017:
018: You should have received a copy of the GNU General Public License
019: along with this program; if not, write to the Free Software
020: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
021: */
022:
023: package com.mysql.jdbc.integration.jboss;
024:
025: import java.io.Serializable;
026: import java.lang.reflect.Method;
027: import java.sql.Connection;
028: import java.sql.SQLException;
029: import java.sql.Statement;
030:
031: import org.jboss.resource.adapter.jdbc.ValidConnectionChecker;
032:
033: import com.mysql.jdbc.SQLError;
034:
035: /**
036: * A more efficient connection checker for JBoss.
037: *
038: * @version $Id: MysqlValidConnectionChecker.java,v 1.1.2.1 2005/05/13 18:58:42
039: * mmatthews Exp $
040: */
041: public final class MysqlValidConnectionChecker implements
042: ValidConnectionChecker, Serializable {
043:
044: private static final long serialVersionUID = 3258689922776119348L;
045:
046: private Method pingMethod;
047:
048: private Method pingMethodWrapped;
049:
050: private final static Object[] NO_ARGS_OBJECT_ARRAY = new Object[0];
051:
052: public MysqlValidConnectionChecker() {
053: try {
054: // Avoid classloader goofiness
055: Class mysqlConnection = Thread.currentThread()
056: .getContextClassLoader().loadClass(
057: "com.mysql.jdbc.Connection");
058:
059: pingMethod = mysqlConnection.getMethod("ping", null);
060:
061: Class mysqlConnectionWrapper = Thread
062: .currentThread()
063: .getContextClassLoader()
064: .loadClass(
065: "com.mysql.jdbc.jdbc2.optional.ConnectionWrapper");
066:
067: pingMethodWrapped = mysqlConnectionWrapper.getMethod(
068: "ping", null);
069: } catch (Exception ex) {
070: // Punt, we'll use 'SELECT 1' to do the check
071: }
072: }
073:
074: /*
075: * (non-Javadoc)
076: *
077: * @see org.jboss.resource.adapter.jdbc.ValidConnectionChecker#isValidConnection(java.sql.Connection)
078: */
079: public SQLException isValidConnection(Connection conn) {
080: if (conn instanceof com.mysql.jdbc.Connection) {
081: if (pingMethod != null) {
082: try {
083: this .pingMethod.invoke(conn, null);
084:
085: return null;
086: } catch (Exception ex) {
087: if (ex instanceof SQLException) {
088: return (SQLException) ex;
089: }
090:
091: return SQLError.createSQLException("Ping failed: "
092: + ex.toString());
093: }
094: }
095: } else if (conn instanceof com.mysql.jdbc.jdbc2.optional.ConnectionWrapper) {
096: if (pingMethodWrapped != null) {
097: try {
098: this .pingMethodWrapped.invoke(conn, null);
099:
100: return null;
101: } catch (Exception ex) {
102: if (ex instanceof SQLException) {
103: return (SQLException) ex;
104: }
105:
106: return SQLError.createSQLException("Ping failed: "
107: + ex.toString());
108: }
109: }
110: }
111:
112: // Punt and use 'SELECT 1'
113:
114: Statement pingStatement = null;
115:
116: try {
117: pingStatement = conn.createStatement();
118:
119: pingStatement.executeQuery("SELECT 1").close();
120:
121: return null;
122: } catch (SQLException sqlEx) {
123: return sqlEx;
124: } finally {
125: if (pingStatement != null) {
126: try {
127: pingStatement.close();
128: } catch (SQLException sqlEx) {
129: // can't do anything about it here
130: }
131: }
132: }
133: }
134: }
|