001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.resource.adapter.jdbc.vendor;
023:
024: import java.io.Serializable;
025: import java.lang.reflect.Method;
026: import java.sql.Connection;
027: import java.sql.SQLException;
028: import java.sql.Statement;
029: import java.sql.ResultSet;
030:
031: import org.jboss.logging.Logger;
032: import org.jboss.resource.adapter.jdbc.ValidConnectionChecker;
033:
034: /**
035: * Implements check valid connection sql Requires MySQL driver 3.1.8 or later.
036: * This should work on just about any version of the database itself but will
037: * only be "fast" on version 3.22.1 and later. Prior to that version it just
038: * does "SELECT 1" anyhow.
039: *
040: * @author <a href="mailto:adrian@jboss.org">Adrian Brock</a>
041: * @author <a href="mailto:acoliver ot jbosss dat org">Andrew C. Oliver</a>
042: * @author <a href="mailto:jim.moran@jboss.org">Jim Moran</a>
043: * @version $Revision: 57189 $
044: */
045: public class MySQLValidConnectionChecker implements
046: ValidConnectionChecker, Serializable {
047: private static final long serialVersionUID = -2227528634302168878L;
048:
049: private static final Logger log = Logger
050: .getLogger(MySQLValidConnectionChecker.class);
051:
052: private Method ping;
053: private boolean driverHasPingMethod = false;
054:
055: // The timeout (apparently the timeout is ignored?)
056: private static Object[] params = new Object[] {};
057:
058: public MySQLValidConnectionChecker() {
059: try {
060: Class mysqlConnection = Thread.currentThread()
061: .getContextClassLoader().loadClass(
062: "com.mysql.jdbc.Connection");
063: ping = mysqlConnection.getMethod("ping", new Class[] {});
064: if (ping != null) {
065: driverHasPingMethod = true;
066: }
067: } catch (Exception e) {
068: log
069: .warn(
070: "Cannot resolve com.mysq.jdbc.Connection.ping method. Will use 'SELECT 1' instead.",
071: e);
072: }
073: }
074:
075: public SQLException isValidConnection(Connection c) {
076: //if there is a ping method then use it, otherwise just use a 'SELECT 1' statement
077: if (driverHasPingMethod) {
078: try {
079: ping.invoke(c, params);
080: } catch (Exception e) {
081: if (e instanceof SQLException) {
082: return (SQLException) e;
083: } else {
084: log.warn("Unexpected error in ping", e);
085: return new SQLException("ping failed: "
086: + e.toString());
087: }
088: }
089:
090: } else {
091:
092: Statement stmt = null;
093: ResultSet rs = null;
094: try {
095: stmt = c.createStatement();
096: rs = stmt.executeQuery("SELECT 1");
097: } catch (Exception e) {
098: if (e instanceof SQLException) {
099: return (SQLException) e;
100: } else {
101: log.warn("Unexpected error in ping (SELECT 1)", e);
102: return new SQLException("ping (SELECT 1) failed: "
103: + e.toString());
104: }
105: } finally {
106: //cleanup the Statment
107: try {
108: if (rs != null)
109: rs.close();
110: if (stmt != null)
111: stmt.close();
112: } catch (SQLException e) {
113: }
114: }
115:
116: }
117: return null;
118: }
119: }
|