01: /*
02: * GeoTools - OpenSource mapping toolkit
03: * http://geotools.org
04: * (C) 2003-2006, GeoTools Project Managment Committee (PMC)
05: *
06: * This library is free software; you can redistribute it and/or
07: * modify it under the terms of the GNU Lesser General Public
08: * License as published by the Free Software Foundation;
09: * version 2.1 of the License.
10: *
11: * This library is distributed in the hope that it will be useful,
12: * but WITHOUT ANY WARRANTY; without even the implied warranty of
13: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14: * Lesser General Public License for more details.
15: */
16: package org.geotools.data.jdbc;
17:
18: import java.sql.Connection;
19: import java.sql.SQLException;
20:
21: import javax.sql.PooledConnection;
22:
23: /**
24: * Provides a container for a PooledConnection so we can store extra information such as when it
25: * was last used and whether it is currently in use.
26: *
27: * <p>
28: * This class should not be subclassed.
29: * </p>
30: *
31: * @author Sean Geoghegan, Defence Science and Technology Organisation
32: * @author $Author: cholmesny $
33: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/library/jdbc/src/main/java/org/geotools/data/jdbc/ManagedPooledConnection.java $
34: * @version $Id: ManagedPooledConnection.java 22347 2006-10-24 01:57:23Z jdeolive $ Last Modified: $Date: 2003/09/22 18:54:39 $
35: */
36: final class ManagedPooledConnection {
37: /** The timestamp the last use of the connection */
38: long lastUsed;
39: /** True if the connection is in use */
40: boolean inUse = false;
41: /** The actual connection we manage */
42: PooledConnection pooledConn;
43:
44: /**
45: * Creates a ManagedPooledConnection for a Pooled Connection.
46: *
47: * @param pooledConn The PooledConnection to manage.
48: */
49: ManagedPooledConnection(PooledConnection pooledConn) {
50: this .pooledConn = pooledConn;
51: }
52:
53: /**
54: * Check whether this PooledConnection is still valid.
55: *
56: * <p>
57: * A Pooled Connection is valid if it is either in use or it is still returning valid
58: * Connections. This method may trigger ConnectionEvents, so if you dont want to receive an
59: * event for this method you should remove any ConnectionEventListeners from the pooledConn
60: * prior to calling.
61: * </p>
62: *
63: * @return True if the connection is in use or is still returning valid logical Connections.
64: */
65: boolean isValid() {
66: if (inUse) {
67: return true;
68: } else {
69: try {
70: Connection conn = pooledConn.getConnection();
71:
72: conn.close();
73:
74: return true;
75: } catch (SQLException e) {
76: return false;
77: }
78: }
79: }
80: }
|