01: /*
02: * This software is released under a licence similar to the Apache Software Licence.
03: * See org.logicalcobwebs.proxool.package.html for details.
04: * The latest version is available at http://proxool.sourceforge.net
05: */
06: package org.logicalcobwebs.proxool;
07:
08: import java.sql.Connection;
09: import java.sql.Statement;
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
18: ConnectionValidatorIF {
19:
20: /**
21: *
22: */
23: public DefaultConnectionValidator() {
24: super ();
25: }
26:
27: //
28: // -- ConnectionValidatorIF interface implementation ----------------------
29: //
30:
31: /* (non-Javadoc)
32: * @see org.logicalcobwebs.proxool.ConnectionValidatorIF#validate(org.logicalcobwebs.proxool.ConnectionPoolDefinition, java.sql.Connection)
33: */
34: public boolean validate(ConnectionPoolDefinitionIF cpd,
35: Connection connection) {
36: // make sure a test SQL is defined
37: //
38: final String testSql = cpd.getHouseKeepingTestSql();
39: if (testSql == null || (testSql.length() == 0)) {
40: Log log = getPoolLog(cpd.getAlias());
41: log
42: .warn("Connection validation requested but house-keeping-test-sql not defined");
43: return false;
44: }
45:
46: // execute the test statement
47: //
48: Statement st = null;
49: try {
50: st = connection.createStatement();
51: st.execute(testSql);
52:
53: return true;
54: } catch (Throwable t) {
55: // got an exception while executing the test statement
56: // log the problem and return false
57: Log log = getPoolLog(cpd.getAlias());
58: if (log.isDebugEnabled())
59: log
60: .debug("A connection failed the validation test with error: "
61: + t);
62:
63: return false;
64: } finally {
65: if (st != null) {
66: try {
67: st.close();
68: } catch (Throwable t) {
69: // Ignore
70: return false;
71: }
72: }
73: }
74: }
75:
76: /**
77: *
78: * @param poolAlias
79: * @return
80: */
81: private Log getPoolLog(String poolAlias) {
82: return LogFactory.getLog("org.logicalcobwebs.proxool."
83: + poolAlias);
84: }
85: }
86:
87: /*
88: Revision history:
89: $Log: DefaultConnectionValidator.java,v $
90: Revision 1.2 2006/01/18 14:40:01 billhorsman
91: Unbundled Jakarta's Commons Logging.
92:
93: Revision 1.1 2004/03/25 22:02:15 brenuart
94: First step towards pluggable ConnectionBuilderIF & ConnectionValidatorIF.
95: Include some minor refactoring that lead to deprecation of some PrototyperController methods.
96:
97: */
|