~pbms-core/pbms/async_read

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
 Copyright (C) 2002-2006 MySQL AB

 This program is free software; you can redistribute it and/or modify
 it under the terms of version 2 of the GNU General Public License as 
 published by the Free Software Foundation.

 There are special exceptions to the terms and conditions of the GPL 
 as it is applied to this software. View the full text of the 
 exception in file EXCEPTIONS-CONNECTOR-J in the directory of this 
 software distribution.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package com.mysql.jdbc.integration.jboss;

import java.io.Serializable;
import java.lang.reflect.Method;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;

import org.jboss.resource.adapter.jdbc.ValidConnectionChecker;

import com.mysql.jdbc.SQLError;

/**
 * A more efficient connection checker for JBoss.
 * 
 * @version $Id: MysqlValidConnectionChecker.java,v 1.1.2.1 2005/05/13 18:58:42
 *          mmatthews Exp $
 */
public final class MysqlValidConnectionChecker implements
		ValidConnectionChecker, Serializable {

	private static final long serialVersionUID = 3258689922776119348L;

	private Method pingMethod;
	
	private Method pingMethodWrapped;

	private final static Object[] NO_ARGS_OBJECT_ARRAY = new Object[0];

	public MysqlValidConnectionChecker() {
		try {
			// Avoid classloader goofiness
			Class mysqlConnection = Thread.currentThread()
					.getContextClassLoader().loadClass(
							"com.mysql.jdbc.Connection");

			pingMethod = mysqlConnection.getMethod("ping", null);
			
			Class mysqlConnectionWrapper = Thread.currentThread()
			.getContextClassLoader().loadClass(
					"com.mysql.jdbc.jdbc2.optional.ConnectionWrapper");
			
			pingMethodWrapped = mysqlConnectionWrapper.getMethod("ping", null);
		} catch (Exception ex) {
			// Punt, we'll use 'SELECT 1' to do the check
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jboss.resource.adapter.jdbc.ValidConnectionChecker#isValidConnection(java.sql.Connection)
	 */
	public SQLException isValidConnection(Connection conn) {
		if (conn instanceof com.mysql.jdbc.Connection) {
			if (pingMethod != null) {
				try {
					this.pingMethod.invoke(conn, NO_ARGS_OBJECT_ARRAY);
	
					return null;
				} catch (Exception ex) {
					if (ex instanceof SQLException) {
						return (SQLException) ex;
					}
	
					return SQLError.createSQLException("Ping failed: " + ex.toString());
				}
			}
		} else if (conn instanceof com.mysql.jdbc.jdbc2.optional.ConnectionWrapper) {
			if (pingMethodWrapped != null) {
				try {
					this.pingMethodWrapped.invoke(conn, NO_ARGS_OBJECT_ARRAY);
	
					return null;
				} catch (Exception ex) {
					if (ex instanceof SQLException) {
						return (SQLException) ex;
					}
	
					return SQLError.createSQLException("Ping failed: " + ex.toString());
				}
			}
		}

		// Punt and use 'SELECT 1'

		Statement pingStatement = null;

		try {
			pingStatement = conn.createStatement();
			
			pingStatement.executeQuery("SELECT 1").close();

			return null;
		} catch (SQLException sqlEx) {
			return sqlEx;
		} finally {
			if (pingStatement != null) {
				try {
					pingStatement.close();
				} catch (SQLException sqlEx) {
					// can't do anything about it here
				}
			}
		}
	}
}