~pbms-core/pbms/async_read

« back to all changes in this revision

Viewing changes to mybs/java/src/com/mysql/jdbc/integration/jboss/MysqlValidConnectionChecker.java

  • Committer: paul-mccullagh
  • Date: 2008-03-26 11:35:17 UTC
  • Revision ID: paul-mccullagh-afb1610c21464a577ae428d72fc725eb986c05a5
Initial import

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 Copyright (C) 2002-2006 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
import com.mysql.jdbc.SQLError;
 
34
 
 
35
/**
 
36
 * A more efficient connection checker for JBoss.
 
37
 * 
 
38
 * @version $Id: MysqlValidConnectionChecker.java,v 1.1.2.1 2005/05/13 18:58:42
 
39
 *          mmatthews Exp $
 
40
 */
 
41
public final class MysqlValidConnectionChecker implements
 
42
                ValidConnectionChecker, Serializable {
 
43
 
 
44
        private static final long serialVersionUID = 3258689922776119348L;
 
45
 
 
46
        private Method pingMethod;
 
47
        
 
48
        private Method pingMethodWrapped;
 
49
 
 
50
        private final static Object[] NO_ARGS_OBJECT_ARRAY = new Object[0];
 
51
 
 
52
        public MysqlValidConnectionChecker() {
 
53
                try {
 
54
                        // Avoid classloader goofiness
 
55
                        Class mysqlConnection = Thread.currentThread()
 
56
                                        .getContextClassLoader().loadClass(
 
57
                                                        "com.mysql.jdbc.Connection");
 
58
 
 
59
                        pingMethod = mysqlConnection.getMethod("ping", null);
 
60
                        
 
61
                        Class mysqlConnectionWrapper = Thread.currentThread()
 
62
                        .getContextClassLoader().loadClass(
 
63
                                        "com.mysql.jdbc.jdbc2.optional.ConnectionWrapper");
 
64
                        
 
65
                        pingMethodWrapped = mysqlConnectionWrapper.getMethod("ping", null);
 
66
                } catch (Exception ex) {
 
67
                        // Punt, we'll use 'SELECT 1' to do the check
 
68
                }
 
69
        }
 
70
 
 
71
        /*
 
72
         * (non-Javadoc)
 
73
         * 
 
74
         * @see org.jboss.resource.adapter.jdbc.ValidConnectionChecker#isValidConnection(java.sql.Connection)
 
75
         */
 
76
        public SQLException isValidConnection(Connection conn) {
 
77
                if (conn instanceof com.mysql.jdbc.Connection) {
 
78
                        if (pingMethod != null) {
 
79
                                try {
 
80
                                        this.pingMethod.invoke(conn, NO_ARGS_OBJECT_ARRAY);
 
81
        
 
82
                                        return null;
 
83
                                } catch (Exception ex) {
 
84
                                        if (ex instanceof SQLException) {
 
85
                                                return (SQLException) ex;
 
86
                                        }
 
87
        
 
88
                                        return SQLError.createSQLException("Ping failed: " + ex.toString());
 
89
                                }
 
90
                        }
 
91
                } else if (conn instanceof com.mysql.jdbc.jdbc2.optional.ConnectionWrapper) {
 
92
                        if (pingMethodWrapped != null) {
 
93
                                try {
 
94
                                        this.pingMethodWrapped.invoke(conn, NO_ARGS_OBJECT_ARRAY);
 
95
        
 
96
                                        return null;
 
97
                                } catch (Exception ex) {
 
98
                                        if (ex instanceof SQLException) {
 
99
                                                return (SQLException) ex;
 
100
                                        }
 
101
        
 
102
                                        return SQLError.createSQLException("Ping failed: " + ex.toString());
 
103
                                }
 
104
                        }
 
105
                }
 
106
 
 
107
                // Punt and use 'SELECT 1'
 
108
 
 
109
                Statement pingStatement = null;
 
110
 
 
111
                try {
 
112
                        pingStatement = conn.createStatement();
 
113
                        
 
114
                        pingStatement.executeQuery("SELECT 1").close();
 
115
 
 
116
                        return null;
 
117
                } catch (SQLException sqlEx) {
 
118
                        return sqlEx;
 
119
                } finally {
 
120
                        if (pingStatement != null) {
 
121
                                try {
 
122
                                        pingStatement.close();
 
123
                                } catch (SQLException sqlEx) {
 
124
                                        // can't do anything about it here
 
125
                                }
 
126
                        }
 
127
                }
 
128
        }
 
129
}