KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jivesoftware > database > ConnectionWrapper


1 /**
2  * $RCSfile: ConnectionWrapper.java,v $
3  * $Revision: 1.1 $
4  * $Date: 2004/10/21 06:08:42 $
5  *
6  * Copyright (C) 2004 Jive Software. All rights reserved.
7  *
8  * This software is published under the terms of the GNU Public License (GPL),
9  * a copy of which is included in this distribution.
10  */

11
12 package org.jivesoftware.database;
13
14 import org.jivesoftware.database.AbstractConnection;
15 import org.jivesoftware.database.ConnectionPool;
16
17 import java.sql.Connection JavaDoc;
18 import java.sql.SQLException JavaDoc;
19
20 /**
21  * An implementation of the Connection interface that wraps an underlying
22  * Connection object. It releases the connection back to a connection pool
23  * when Connection.close() is called.
24  *
25  * @author Jive Software
26  */

27 public class ConnectionWrapper extends AbstractConnection {
28
29     public ConnectionPool pool;
30     public boolean checkedout = false;
31     public long createTime;
32     public long lockTime;
33     public long checkinTime;
34     public Exception JavaDoc exception;
35     public boolean hasLoggedException = false;
36
37     public ConnectionWrapper(Connection connection, ConnectionPool pool) {
38         super(connection);
39
40         this.pool = pool;
41         createTime = System.currentTimeMillis();
42         lockTime = createTime;
43         checkinTime = lockTime;
44     }
45
46     public void setConnection(Connection connection) {
47         super.connection = connection;
48     }
49
50     /**
51      * Instead of closing the underlying connection, we simply release
52      * it back into the pool.
53      */

54     public void close() throws SQLException JavaDoc {
55         synchronized (this) {
56             checkedout = false;
57             checkinTime = System.currentTimeMillis();
58         }
59
60         pool.freeConnection();
61
62         // Release object references. Any further method calls on the connection will fail.
63
// super.connection = null;
64
}
65
66     public String JavaDoc toString() {
67         if (connection != null) {
68             return connection.toString();
69         }
70         else {
71             return "Jive Software Connection Wrapper";
72         }
73     }
74
75     public synchronized boolean isCheckedOut() {
76         return checkedout;
77     }
78 }
79
Popular Tags