| 1 | /* |
| 2 | Copyright (C) 2002-2005 MySQL AB |
| 3 | |
| 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. |
| 7 | |
| 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. |
| 12 | |
| 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. |
| 17 | |
| 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 |
| 21 | */ |
| 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 | /** |
| 34 | * A more efficient connection checker for JBoss. |
| 35 | * |
| 36 | * @version $Id: MysqlValidConnectionChecker.java,v 1.1.2.1 2005/05/13 18:58:42 |
| 37 | * mmatthews Exp $ |
| 38 | */ |
| 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 | // Avoid classloader goofiness |
| 51 | Class mysqlConnection = Thread.currentThread() |
| 52 | .getContextClassLoader().loadClass( |
| 53 | "com.mysql.jdbc.Connection"); |
| 54 | |
| 55 | pingMethod = mysqlConnection.getMethod("ping", null); |
| 56 | } catch (Exception ex) { |
| 57 | // Punt, we'll use 'SELECT 1' to do the check |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | /* |
| 62 | * (non-Javadoc) |
| 63 | * |
| 64 | * @see org.jboss.resource.adapter.jdbc.ValidConnectionChecker#isValidConnection(java.sql.Connection) |
| 65 | */ |
| 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 | // Punt and use 'SELECT 1' |
| 82 | |
| 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 | // can't do anything about it here |
| 97 | } |
| 98 | } |
| 99 | } |
| 100 | } |
| 101 | } |