KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mysql > jdbc > integration > c3p0 > MysqlConnectionTester


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.c3p0;
24
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
30 import com.mchange.v2.c3p0.C3P0ProxyConnection;
31 import com.mchange.v2.c3p0.QueryConnectionTester;
32 import com.mysql.jdbc.CommunicationsException;
33
34 /**
35  * ConnectionTester for C3P0 connection pool that uses the more efficient
36  * COM_PING method of testing connection 'liveness' for MySQL, and 'sorts'
37  * exceptions based on SQLState or class of 'CommunicationsException' for
38  * handling exceptions.
39  *
40  * @version $Id: MysqlConnectionTester.java,v 1.1.2.1 2005/05/13 18:58:39
41  * mmatthews Exp $
42  */

43 public final class MysqlConnectionTester implements QueryConnectionTester {
44
45     private static final long serialVersionUID = 3256444690067896368L;
46
47     private static final Object JavaDoc[] NO_ARGS_ARRAY = new Object JavaDoc[0];
48
49     private Method JavaDoc pingMethod;
50
51     public MysqlConnectionTester() {
52         try {
53             pingMethod = com.mysql.jdbc.Connection.class
54                     .getMethod("ping", null);
55         } catch (Exception JavaDoc ex) {
56             // punt, we have no way to recover, other than we now use 'SELECT 1'
57
// for
58
// handling the connection testing.
59
}
60     }
61
62     /*
63      * (non-Javadoc)
64      *
65      * @see com.mchange.v2.c3p0.ConnectionTester#activeCheckConnection(java.sql.Connection)
66      */

67     public int activeCheckConnection(Connection JavaDoc con) {
68         C3P0ProxyConnection castCon = (C3P0ProxyConnection) con;
69
70         try {
71             if (pingMethod != null) {
72                 castCon.rawConnectionOperation(pingMethod,
73                         C3P0ProxyConnection.RAW_CONNECTION, NO_ARGS_ARRAY);
74             } else {
75                 Statement JavaDoc pingStatement = null;
76
77                 try {
78                     pingStatement = con.createStatement();
79                     pingStatement.executeQuery("SELECT 1").close();
80                 } finally {
81                     if (pingStatement != null) {
82                         pingStatement.close();
83                     }
84                 }
85             }
86
87             return CONNECTION_IS_OKAY;
88         } catch (Exception JavaDoc ex) {
89             return CONNECTION_IS_INVALID;
90         }
91     }
92
93     /*
94      * (non-Javadoc)
95      *
96      * @see com.mchange.v2.c3p0.ConnectionTester#statusOnException(java.sql.Connection,
97      * java.lang.Throwable)
98      */

99     public int statusOnException(Connection JavaDoc arg0, Throwable JavaDoc throwable) {
100         if (throwable instanceof CommunicationsException) {
101             return CONNECTION_IS_INVALID;
102         }
103
104         if (throwable instanceof SQLException JavaDoc) {
105             String JavaDoc sqlState = ((SQLException JavaDoc) throwable).getSQLState();
106
107             if (sqlState != null && sqlState.startsWith("08")) {
108                 return CONNECTION_IS_INVALID;
109             }
110
111             return CONNECTION_IS_OKAY;
112         }
113
114         // Runtime/Unchecked?
115

116         return CONNECTION_IS_INVALID;
117     }
118
119     /*
120      * (non-Javadoc)
121      *
122      * @see com.mchange.v2.c3p0.QueryConnectionTester#activeCheckConnection(java.sql.Connection,
123      * java.lang.String)
124      */

125     public int activeCheckConnection(Connection JavaDoc arg0, String JavaDoc arg1) {
126         return CONNECTION_IS_OKAY;
127     }
128 }
129
Popular Tags