KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > uk > org > primrose > pool > core > PoolConnection


1 /**
2 * Library name : Primrose - A Java Database Connection Pool.
3 * Published by Ben Keeping, http://primrose.org.uk .
4 * Copyright (C) 2004 Ben Keeping, primrose.org.uk
5 * Email: Use "Contact Us Form" on website
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 (at your option) 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 USA
20 */

21
22 package uk.org.primrose.pool.core;
23 import java.sql.*;
24
25 public class PoolConnection implements Connection,java.io.Serializable JavaDoc {
26     Connection conn = null;
27     private PoolSqlMonitor psm = new PoolSqlMonitor();
28     Pool pool;
29
30     // look out for people closing a connection twice !
31
boolean connectionCalled = false;
32
33     public PoolConnection(Pool pool) {
34         super();
35         this.pool = pool;
36         this.psm.reset();
37     }
38
39     public PoolConnection getConnection(String JavaDoc driverClass, String JavaDoc driverURL, String JavaDoc user, String JavaDoc password) {
40         conn = new NewConnection().get(driverClass, driverURL, user, password);
41         try {
42             if (conn == null || conn.isClosed()) {
43                 return null;
44             }
45         } catch (SQLException sqle) { return null; }
46         return this;
47     }
48
49     public void setConnectionCalled(boolean connectionCalled) {
50         this.connectionCalled = connectionCalled;
51         // set the stack trace caller for logging
52
this.psm.setCaller(5);
53         this.psm.setSql("TRANSITIONAL_CONNECTION_OPENED");
54         pool.log("[PoolConnection] " +this + " Retrieving connection , CALLER is : " +this.psm.getCaller() , false);
55
56     }
57
58     public boolean getConnectionCalled() {
59         return this.connectionCalled;
60     }
61
62     /**
63     * Return the time in millis that this connection has been in use.
64     */

65     public long getTimeInUse() {
66         return this.psm.getTimeInUse();
67     }
68
69     /**
70     * Return the number of db calls this connection has done.
71     */

72     public int getNumberOfCalls() {
73         return this.psm.getNumberOfCalls();
74     }
75
76     /**
77     * Return the idle time associated with this connection.
78     */

79     public long getIdleTime() {
80         return this.psm.getIdleTime();
81     }
82
83     /**
84     * Return the SQL associated with this connection (if any).
85     */

86     public String JavaDoc getSql() {
87         return this.psm.getSql();
88     }
89
90     /**
91     * Return the calling.instantiating object associated with this connection (if any).
92     */

93     public String JavaDoc getCaller() {
94         String JavaDoc caller = this.psm.getCaller();
95         return caller;
96
97     }
98
99     /**
100     * Return the actual non-wrapped connection.<br>
101     * Warning ! This in effect disables all intelligent logging
102     * and dynamic closing of Connection related objects, and
103     * should not really be used.
104     * @deprecated available only for OracleObjects which have problems with wrapped objects.
105     */

106     public Connection getRealConnection() {
107         return this.conn;
108     }
109
110     public void close() {
111         close(true);
112     }
113
114     /**
115     * Close the connection and return it to the pool.<br>
116     * Before the Connection goes back in the pool, check that
117     * all Statement and ResultSet objects are closed,
118     * and close them as necssary. How cool is that ?!
119     * We won't ever have to worry unclosed sql objects again !
120     */

121     protected void close(boolean cleanAndDump) {
122 //synchronized(pool.synchack) {
123

124         pool.log("[PoolConnection] close() called on " +this, false);
125         pool.log("[PoolConnection] " +this + " SQL was : " +this.psm.getSql() +", CALLER was : " +this.psm.getCaller() , false);
126
127         // check for people closing a connection twice ...
128
// when a connection is removed from the pool (Pool.get() method)
129
// the boolean is set to 'true', and when closed, set to 'false'/
130
// we should only return a connection to the pool if the bool is 'true'
131
if (this.getConnectionCalled() == false) {
132             pool.log("[PoolConnection] " +this + " WARNING ! You have called close() on a connection twice ! This could cause problems !, CALLER (" +this.psm.getCaller() +") ", true);
133             return;
134         }
135
136         if (psm.getResultSet() != null) {
137             pool.log("[PoolConnection] " +this + " isResultSetClosed(" +psm.isResultSetClosed() +")", false);
138             if (!psm.isResultSetClosed()) {
139                 pool.log("[PoolConnection] " +this + " Trying to close unclosed ResultSet SQL(" +this.psm.getSql() +"), CALLER (" +this.psm.getCaller() +") ...", false);
140                 try {
141                     psm.getResultSet().close();
142                 } catch (SQLException sqle) {
143                     sqle.printStackTrace(pool.getLog());
144                     // something is obviously up with this connection ....
145
// mark it as closed so that pool dumps it.
146
if (cleanAndDump) {
147                         pool.dump(this, false);
148                     }
149                 }
150             }
151         }
152
153         if (psm.getStatement() != null) {
154             pool.log("[PoolConnection] " +this + " isStatementClosed(" +psm.isStatementClosed() +")", false);
155             if (!psm.isStatementClosed()) {
156                 pool.log("[PoolConnection] " +this + " Trying to close unclosed " +psm.getStatement().getClass().getName() +" SQL(" +this.psm.getSql() +"), CALLER (" +this.psm.getCaller() +") ...", false);
157                 try {
158                     psm.getStatement().close();
159                 } catch (SQLException sqle) {
160                     sqle.printStackTrace(pool.getLog());
161
162                     // something is obviously up with this connection ....
163
// mark it as closed so that pool dumps it.
164
if (cleanAndDump) {
165                         pool.dump(this, false);
166                     }
167                 }
168             }
169         }
170
171         if (cleanAndDump) {
172             this.connectionCalled = false;
173             this.psm.reset();
174             pool.put(this);
175         }
176 //}
177
}
178
179     public Statement createStatement() throws SQLException {
180         PoolStatement ps = new PoolStatement(conn.createStatement(), psm);
181         return ps;
182
183     }
184
185     public PreparedStatement prepareStatement(String JavaDoc sql) throws SQLException {
186         PoolPreparedStatement pps = new PoolPreparedStatement(conn.prepareStatement(sql), psm);
187         this.psm.setSql(sql);
188         return pps;
189     }
190
191     public CallableStatement prepareCall(String JavaDoc sql) throws SQLException {
192         PoolCallableStatement pcs = new PoolCallableStatement(conn.prepareCall(sql), psm);
193         this.psm.setSql(sql);
194         return pcs;
195     }
196
197     public String JavaDoc nativeSQL(String JavaDoc sql) throws SQLException {
198         this.psm.setSql(sql);
199         return conn.nativeSQL(sql);
200     }
201
202     public void setAutoCommit(boolean autoCommit) throws SQLException {
203         conn.setAutoCommit(autoCommit);
204     }
205
206     public boolean getAutoCommit() throws SQLException{
207         return conn.getAutoCommit();
208     }
209
210     public void commit() throws SQLException{
211         conn.commit();
212     }
213
214     public void rollback() throws SQLException{
215         conn.rollback();
216     }
217
218     public boolean isClosed() {
219         if (conn == null) return false;
220         try {
221             return conn.isClosed();
222         } catch (SQLException sqle) {
223             sqle.printStackTrace(System.err);
224             return true;
225         }
226     }
227
228
229     public DatabaseMetaData getMetaData() throws SQLException{
230         return conn.getMetaData();
231     }
232
233     public void setReadOnly(boolean readOnly) throws SQLException{
234         conn.setReadOnly(readOnly);
235     }
236
237     public boolean isReadOnly() throws SQLException{
238         return conn.isReadOnly();
239     }
240
241     public void setCatalog(String JavaDoc catalog) throws SQLException{
242         conn.setCatalog(catalog);
243     }
244
245
246     public String JavaDoc getCatalog() throws SQLException{
247         return conn.getCatalog();
248     }
249
250     public void setTransactionIsolation(int level) throws SQLException{
251         conn.setTransactionIsolation(level);
252     }
253
254     public int getTransactionIsolation() throws SQLException{
255         return conn.getTransactionIsolation();
256     }
257
258     public SQLWarning getWarnings() throws SQLException{
259         return conn.getWarnings();
260     }
261
262     public void clearWarnings() throws SQLException{
263         conn.clearWarnings();
264     }
265
266     public Statement createStatement(int resultSetType, int resultSetConcurrency) throws SQLException{
267         return conn.createStatement(resultSetType, resultSetConcurrency);
268     }
269
270     public PreparedStatement prepareStatement(String JavaDoc sql, int resultSetType, int resultSetConcurrency) throws SQLException{
271         return conn.prepareStatement(sql, resultSetType, resultSetConcurrency);
272     }
273
274     public CallableStatement prepareCall(String JavaDoc sql, int resultSetType, int resultSetConcurrency) throws SQLException{
275         return conn.prepareCall(sql, resultSetType, resultSetConcurrency);
276     }
277
278     public java.util.Map JavaDoc getTypeMap() throws SQLException{
279         return conn.getTypeMap();
280     }
281
282     public void setTypeMap(java.util.Map JavaDoc map) throws SQLException{
283         conn.setTypeMap(map);
284     }
285
286     public void setHoldability(int holdability) throws SQLException{
287         conn.setHoldability(holdability);
288     }
289
290     public int getHoldability() throws SQLException{
291         return conn.getHoldability();
292     }
293
294     public Savepoint setSavepoint() throws SQLException{
295         return conn.setSavepoint();
296     }
297
298     public Savepoint setSavepoint(String JavaDoc name) throws SQLException{
299         return conn.setSavepoint(name);
300     }
301
302     public void rollback(Savepoint savepoint) throws SQLException{
303         conn.rollback(savepoint);
304     }
305
306
307     public void releaseSavepoint(Savepoint savepoint) throws SQLException{
308         conn.releaseSavepoint(savepoint);
309     }
310
311     public Statement createStatement(int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException{
312         return conn.createStatement(resultSetType, resultSetConcurrency, resultSetHoldability);
313     }
314
315     public PreparedStatement prepareStatement(String JavaDoc sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException{
316         return conn.prepareStatement(sql, resultSetType, resultSetConcurrency,resultSetHoldability);
317     }
318
319     public CallableStatement prepareCall(String JavaDoc sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException{
320         return conn.prepareCall(sql, resultSetType, resultSetConcurrency, resultSetHoldability);
321     }
322
323     public PreparedStatement prepareStatement(String JavaDoc sql, int autoGeneratedKeys) throws SQLException{
324         return conn.prepareStatement(sql, autoGeneratedKeys);
325     }
326
327     public PreparedStatement prepareStatement(String JavaDoc sql, int columnIndexes[]) throws SQLException{
328         return conn.prepareStatement(sql, columnIndexes);
329     }
330
331     public PreparedStatement prepareStatement(String JavaDoc sql, String JavaDoc columnNames[]) throws SQLException{
332         return conn.prepareStatement(sql, columnNames);
333     }
334 }
335
Popular Tags