KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > resource > adapter > jdbc > vendor > MySQLValidConnectionChecker


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.resource.adapter.jdbc.vendor;
23
24 import java.io.Serializable JavaDoc;
25 import java.lang.reflect.Method JavaDoc;
26 import java.sql.Connection JavaDoc;
27 import java.sql.SQLException JavaDoc;
28 import java.sql.Statement JavaDoc;
29 import java.sql.ResultSet JavaDoc;
30
31 import org.jboss.logging.Logger;
32 import org.jboss.resource.adapter.jdbc.ValidConnectionChecker;
33
34 /**
35  * Implements check valid connection sql Requires MySQL driver 3.1.8 or later.
36  * This should work on just about any version of the database itself but will
37  * only be "fast" on version 3.22.1 and later. Prior to that version it just
38  * does "SELECT 1" anyhow.
39  *
40  * @author <a HREF="mailto:adrian@jboss.org">Adrian Brock</a>
41  * @author <a HREF="mailto:acoliver ot jbosss dat org">Andrew C. Oliver</a>
42  * @author <a HREF="mailto:jim.moran@jboss.org">Jim Moran</a>
43  * @version $Revision: 57094 $
44  */

45 public class MySQLValidConnectionChecker implements ValidConnectionChecker, Serializable JavaDoc {
46
47
48     private static final Logger log = Logger.getLogger(MySQLValidConnectionChecker.class);
49     
50     private static final long serialVersionUID = -2227528634302168878L;
51
52     private Method JavaDoc ping;
53     
54     private boolean driverHasPingMethod = false;
55
56     // The timeout (apparently the timeout is ignored?)
57
private static Object JavaDoc[] params = new Object JavaDoc[] {};
58
59     public MySQLValidConnectionChecker()
60    {
61       try
62       {
63          Class JavaDoc mysqlConnection = Thread.currentThread().getContextClassLoader().loadClass("com.mysql.jdbc.Connection");
64          ping = mysqlConnection.getMethod("ping", new Class JavaDoc[]
65          {});
66          if (ping != null)
67          {
68             driverHasPingMethod = true;
69          }
70       }
71       catch (Exception JavaDoc e)
72       {
73          log.warn("Cannot resolve com.mysq.jdbc.Connection.ping method. Will use 'SELECT 1' instead.", e);
74       }
75    }
76
77     public SQLException JavaDoc isValidConnection(Connection JavaDoc c)
78    {
79       //if there is a ping method then use it, otherwise just use a 'SELECT 1' statement
80
if (driverHasPingMethod)
81       {
82          try
83          {
84             ping.invoke(c, params);
85          }
86          catch (Exception JavaDoc e)
87          {
88             if (e instanceof SQLException JavaDoc)
89             {
90                return (SQLException JavaDoc) e;
91             }
92             else
93             {
94                log.warn("Unexpected error in ping", e);
95                return new SQLException JavaDoc("ping failed: " + e.toString());
96             }
97          }
98
99       }
100       else
101       {
102
103          Statement JavaDoc stmt = null;
104          ResultSet JavaDoc rs = null;
105          try
106          {
107             stmt = c.createStatement();
108             rs = stmt.executeQuery("SELECT 1");
109          }
110          catch (Exception JavaDoc e)
111          {
112             if (e instanceof SQLException JavaDoc)
113             {
114                return (SQLException JavaDoc) e;
115             }
116             else
117             {
118                log.warn("Unexpected error in ping (SELECT 1)", e);
119                return new SQLException JavaDoc("ping (SELECT 1) failed: " + e.toString());
120             }
121          }
122          finally
123          {
124             //cleanup the Statment
125
try
126             {
127                if (rs != null)
128                   rs.close();
129                
130                if (stmt != null)
131                   stmt.close();
132             }
133             catch (SQLException JavaDoc ignore)
134             {
135             
136             }
137          }
138
139       }
140       return null;
141    }
142 }
143
Popular Tags