KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > jdbc > standard > StandardPooledConnection


1 /*
2  * XAPool: Open Source XA JDBC Pool
3  * Copyright (C) 2003 Objectweb.org
4  * Initial Developer: Lutris Technologies Inc.
5  * Contact: xapool-public@lists.debian-sf.objectweb.org
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20  * USA
21  */

22 package org.enhydra.jdbc.standard;
23
24 import org.enhydra.jdbc.util.Logger;
25
26 import java.sql.Connection JavaDoc;
27 import java.sql.SQLException JavaDoc;
28 import java.util.Vector JavaDoc;
29 import javax.sql.ConnectionEvent JavaDoc;
30 import javax.sql.ConnectionEventListener JavaDoc;
31 import javax.sql.PooledConnection JavaDoc;
32
33 /**
34  * Provides an implementation of javax.sql.PooledConnection which
35  * is completely generic (i.e. it relies only on JDBC 1 functionality).
36  *
37  * This class maintains a physical database connection which is
38  * passed to each StandardXAConnectionHandle when it is created. It is the
39  * StandardXAConnectionHandle object which the application receives and which
40  * it perceives as the java.sql.Connection object.
41  *
42  * StandardXAConnectionHandle objects pass PreparedStatements back to the
43  * StandardPooledConnection so that they can be retained across
44  * StandardXAConnectionHandle instantiations.
45  */

46 public class StandardPooledConnection implements PooledConnection JavaDoc {
47
48     protected StandardConnectionPoolDataSource dataSource;
49     public Connection JavaDoc con; // the physical database connection
50
public StandardConnectionHandle connectionHandle;
51     // the last StandardConnectionHandle created
52
Vector JavaDoc listeners; // objects listening for events on this connection
53
boolean isClosed; // true if this connection has been closed
54
public Logger log;
55
56     /**
57      * Creates the physical database connection.
58      */

59     public StandardPooledConnection(
60         StandardConnectionPoolDataSource dataSource,
61         String JavaDoc user,
62         String JavaDoc password)
63         throws SQLException JavaDoc {
64         this.dataSource = dataSource;
65         con = dataSource.getConnection(user, password);
66         listeners = new Vector JavaDoc(5, 5);
67     }
68
69     /**
70      * Creates a new StandardConnectionHandle for use by an application.
71      * If there is already a StandardConnectionHandle in use then it is
72      * closed (i.e. the application has the connection withdrawn).
73      */

74     synchronized public Connection JavaDoc getConnection()
75         throws java.sql.SQLException JavaDoc {
76         if (connectionHandle != null) {
77             // if there's already a connection handle
78
if (!connectionHandle.isClosed()) { // and it hasn't been closed
79
connectionHandle.close(); // close it now
80
}
81         }
82         newConnectionHandle();
83         return connectionHandle;
84     }
85
86     protected void newConnectionHandle() {
87         log.debug("StandardPooledConnection:newConnectionHandle");
88         connectionHandle =
89             new StandardConnectionHandle(
90                 this,
91                 dataSource.getMasterPrepStmtCache(),
92                 dataSource.getPreparedStmtCacheSize());
93     }
94
95     public void close() throws java.sql.SQLException JavaDoc {
96         con.close();
97         dataSource.getMasterPrepStmtCache().remove(con.toString());
98     }
99
100     public void addConnectionEventListener(ConnectionEventListener JavaDoc listener) {
101         listeners.addElement(listener);
102     }
103
104     public void removeConnectionEventListener(ConnectionEventListener JavaDoc listener) {
105         listeners.removeElement(listener);
106     }
107
108     /**
109      * Notifies all listeners that the StandardConnectionHandle created by this
110      * PooledConnection has been closed.
111      */

112     void closeEvent() {
113         ConnectionEvent JavaDoc event = new ConnectionEvent JavaDoc(this);
114         // create the event that we'll send
115
for (int i = 0; i < listeners.size(); i++) { // for each listener
116
Object JavaDoc obj = listeners.elementAt(i); // get next listener
117
ConnectionEventListener JavaDoc cel = (ConnectionEventListener JavaDoc) obj;
118             //cast to something more useful
119
cel.connectionClosed(event); // notify this listener
120
}
121
122     }
123
124     /**
125      * Invoked when a fatal connection error occurs,
126      * just before an SQLException is thrown to the application
127      *
128      * This method is automatically called when a fatal error
129      * is detected on the base connection. The base connection
130      * is the actual connection that backs the connection
131      * handle provided by the getConnection() method
132      */

133     public void connectionErrorOccurred(ConnectionEvent JavaDoc event) {
134         for (int i = 0; i < listeners.size(); i++) { // for each listener
135
Object JavaDoc obj = listeners.elementAt(i); // get next listener
136
ConnectionEventListener JavaDoc cel = (ConnectionEventListener JavaDoc) obj;
137             //cast to something more useful
138
cel.connectionErrorOccurred(event); // notify this listener
139
//cel.connectionClosed (event); // notify this listener
140
}
141     }
142
143     /**
144      * Access method allowing access to the underlying physical connection.
145      */

146     public Connection JavaDoc getPhysicalConnection() {
147         return con;
148     }
149
150     public void setLogger(Logger alog) {
151         log = alog;
152     }
153     
154     public String JavaDoc toString() {
155         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
156         sb.append("StandardPooledConnection:\n");
157         sb.append(" is closed =<"+this.isClosed + ">\n");
158         sb.append(" connection =<"+this.con + ">\n");
159         
160         return sb.toString();
161     }
162 }
163
Popular Tags