EMMA Coverage Report (generated Mon Jul 24 20:22:52 CDT 2006)
[all classes][com.mysql.jdbc.integration.jboss]

COVERAGE SUMMARY FOR SOURCE FILE [MysqlValidConnectionChecker.java]

nameclass, %method, %block, %line, %
MysqlValidConnectionChecker.java100% (1/1)100% (3/3)38%  (47/124)38%  (13/34)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class MysqlValidConnectionChecker100% (1/1)100% (3/3)38%  (47/124)38%  (13/34)
isValidConnection (Connection): SQLException 100% (1/1)18%  (17/93)20%  (5/25)
MysqlValidConnectionChecker (): void 100% (1/1)96%  (26/27)88%  (7/8)
<static initializer> 100% (1/1)100% (4/4)100% (1/1)

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 
23package com.mysql.jdbc.integration.jboss;
24 
25import java.io.Serializable;
26import java.lang.reflect.Method;
27import java.sql.Connection;
28import java.sql.SQLException;
29import java.sql.Statement;
30 
31import org.jboss.resource.adapter.jdbc.ValidConnectionChecker;
32 
33import 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 */
41public 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.executeQuery("SELECT 1").close();
113 
114                        return null;
115                } catch (SQLException sqlEx) {
116                        return sqlEx;
117                } finally {
118                        if (pingStatement != null) {
119                                try {
120                                        pingStatement.close();
121                                } catch (SQLException sqlEx) {
122                                        // can't do anything about it here
123                                }
124                        }
125                }
126        }
127}

[all classes][com.mysql.jdbc.integration.jboss]
EMMA 2.0.4217 (C) Vladimir Roubtsov