KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > logicalcobwebs > proxool > DefaultConnectionValidator


1 /*
2  * This software is released under a licence similar to the Apache Software Licence.
3  * See org.logicalcobwebs.proxool.package.html for details.
4  * The latest version is available at http://proxool.sourceforge.net
5  */

6 package org.logicalcobwebs.proxool;
7
8 import java.sql.Connection JavaDoc;
9 import java.sql.Statement JavaDoc;
10
11 import org.apache.commons.logging.Log;
12 import org.apache.commons.logging.LogFactory;
13
14 /**
15  * @author Bertrand Renuart
16  */

17 public class DefaultConnectionValidator implements ConnectionValidatorIF {
18
19     /**
20      *
21      */

22     public DefaultConnectionValidator() {
23         super();
24     }
25
26     //
27
// -- ConnectionValidatorIF interface implementation ----------------------
28
//
29

30     /* (non-Javadoc)
31      * @see org.logicalcobwebs.proxool.ConnectionValidatorIF#validate(org.logicalcobwebs.proxool.ConnectionPoolDefinition, java.sql.Connection)
32      */

33     public boolean validate(ConnectionPoolDefinitionIF cpd, Connection JavaDoc connection) {
34         // make sure a test SQL is defined
35
//
36
final String JavaDoc testSql = cpd.getHouseKeepingTestSql();
37         if (testSql == null || (testSql.length() == 0)) {
38             Log log = getPoolLog(cpd.getAlias());
39             log.warn("Connection validation requested but house-keeping-test-sql not defined");
40             return false;
41         }
42         
43         
44         // execute the test statement
45
//
46
Statement JavaDoc st = null;
47         try {
48             st = connection.createStatement();
49             st.execute(testSql);
50
51             return true;
52         }
53         catch (Throwable JavaDoc t) {
54             // got an exception while executing the test statement
55
// log the problem and return false
56
Log log = getPoolLog(cpd.getAlias());
57             if(log.isDebugEnabled())
58                 log.debug("A connection failed the validation test with error: "+t);
59             
60             return false;
61         }
62         finally {
63             if (st != null) {
64                 try {
65                     st.close();
66                 } catch (Throwable JavaDoc t) {
67                     // Ignore
68
return false;
69                 }
70             }
71         }
72     }
73
74
75     /**
76      *
77      * @param poolAlias
78      * @return
79      */

80     private Log getPoolLog(String JavaDoc poolAlias) {
81         return LogFactory.getLog("org.logicalcobwebs.proxool." + poolAlias);
82     }
83 }
84
85 /*
86  Revision history:
87  $Log: DefaultConnectionValidator.java,v $
88  Revision 1.2 2006/01/18 14:40:01 billhorsman
89  Unbundled Jakarta's Commons Logging.
90
91  Revision 1.1 2004/03/25 22:02:15 brenuart
92  First step towards pluggable ConnectionBuilderIF & ConnectionValidatorIF.
93  Include some minor refactoring that lead to deprecation of some PrototyperController methods.
94
95  */

96
Popular Tags