KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > cofax > connectionpool > ConnectionWrapper


1 /*
2  * ConnectionWrapper is part of the Cofax content management system library.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * Please see http://www.cofax.org for contact information and other related informaion.
19  *
20  * $Header: /cvsroot/cofax/cofax/src/org/cofax/connectionpool/ConnectionWrapper.java,v 1.4.2.1 2006/12/11 16:33:22 fxrobin Exp $
21  */

22
23 package org.cofax.connectionpool;
24
25 import java.sql.*;
26 import java.util.*;
27
28 /**
29  * This class is a wrapper around a Connection, overriding the close method to
30  * just inform the pool that it's available for reuse again, and the isClosed
31  * method to return the state of the wrapper instead of the Connection.
32  *
33  * @author kmartino
34  * @created August 11, 2002
35  */

36 public class ConnectionWrapper implements Connection {
37     private static long counter = 0;
38
39     // realConn should be private but we use package scope to
40
// be able to test removal of bad connections
41
Connection realConn;
42
43     private ConnectionPool pool;
44
45     private boolean isClosed = false;
46
47     // number of connection created
48
private long id;
49
50     // unique id of this connection
51
private int timesUsed;
52
53     private long timeCreated;
54
55     // time stamp of time connection was created
56
private long lastTimeUsed;
57
58     // time stamp of last time connection was used or created
59
private String JavaDoc currentActivity;
60
61     // the current or last query this connection ran
62

63     /**
64      * Constructor for the ConnectionWrapper object
65      *
66      * @param realConn
67      * Description of Parameter
68      * @param pool
69      * Description of Parameter
70      */

71     public ConnectionWrapper(Connection realConn, ConnectionPool pool) {
72
73         this.realConn = realConn;
74         this.pool = pool;
75         timesUsed = 0;
76         counter++;
77         id = counter;
78         java.util.Date JavaDoc currDate = new java.util.Date JavaDoc();
79         timeCreated = currDate.getTime();
80         lastTimeUsed = currDate.getTime();
81
82     }
83
84     /**
85      * Sets the lastTimeUsed attribute of the ConnectionWrapper object
86      */

87     public void setLastTimeUsed() {
88
89         java.util.Date JavaDoc currDate = new java.util.Date JavaDoc();
90         lastTimeUsed = currDate.getTime();
91
92     }
93
94     /**
95      * Function: setCurrentActivity Purpose: to set the query that this
96      * connection is 'about' to run Args: activity - the string representing
97      * what this connection is doing Author: Sam Cohen Created: 7/23/2001
98      * Revision History: 8/29/2001 - made it only take up to 50 chars to keep
99      * run-aways out of the log file
100      *
101      * @param activity
102      * The new currentActivity value
103      */

104     public void setCurrentActivity(String JavaDoc activity) {
105
106         int junkStart = activity.indexOf(" ");
107         if (junkStart != -1) {
108             this.currentActivity = activity.substring(0, junkStart);
109         } else if (activity.length() > 250) {
110             this.currentActivity = activity.substring(0, 250);
111         } else {
112             this.currentActivity = activity;
113         }
114
115     }
116
117     /**
118      * Sets the holdability attribute of the ConnectionWrapper object
119      *
120      * @param holdability
121      * The new holdability value
122      * @exception SQLException
123      * Description of Exception
124      */

125     public void setHoldability(int holdability) throws SQLException {
126         if (isClosed) {
127             throw new SQLException("Pooled connection is closed");
128         }
129         realConn.setHoldability(holdability);
130     }
131
132     /**
133      * Gets the holdability attribute of the ConnectionWrapper object
134      *
135      */

136     public int getHoldability() throws SQLException {
137         if (isClosed) {
138             throw new SQLException("Pooled connection is closed");
139         }
140         return realConn.getHoldability();
141     }
142
143     /**
144      * Sets the savepoint attribute of the ConnectionWrapper object
145      *
146      * @exception SQLException
147      * Description of Exception
148      */

149     public Savepoint setSavepoint() throws SQLException {
150         if (isClosed) {
151             throw new SQLException("Pooled connection is closed");
152         }
153         return realConn.setSavepoint();
154     }
155
156     /**
157      * Sets the savepoint attribute of the ConnectionWrapper object
158      *
159      * @param holdability
160      * The new holdability value
161      * @exception SQLException
162      * Description of Exception
163      */

164     public Savepoint setSavepoint(String JavaDoc savepoint) throws SQLException {
165         if (isClosed) {
166             throw new SQLException("Pooled connection is closed");
167         }
168         return realConn.setSavepoint(savepoint);
169     }
170
171     /**
172      * Executes the rollback method of the ConnectionWrapper object
173      *
174      * @param holdability
175      * The new holdability value
176      * @exception SQLException
177      * Description of Exception
178      */

179     public void rollback(Savepoint savepoint) throws SQLException {
180         if (isClosed) {
181             throw new SQLException("Pooled connection is closed");
182         }
183         realConn.rollback(savepoint);
184     }
185
186     /**
187      * Executes the createStatement method of the ConnectionWrapper object
188      *
189      * @param resultSetType
190      * one of the following ResultSet constants:
191      * ResultSet.TYPE_FORWARD_ONLY,
192      * ResultSet.TYPE_SCROLL_INSENSITIVE, or
193      * ResultSet.TYPE_SCROLL_SENSITIVE
194      * @param resultSetConcurrency
195      * one of the following ResultSet constants:
196      * ResultSet.CONCUR_READ_ONLY or ResultSet.CONCUR_UPDATABLE
197      * @param resultSetHoldability
198      * one of the following ResultSet constants:
199      * ResultSet.HOLD_CURSORS_OVER_COMMIT or
200      * ResultSet.CLOSE_CURSORS_AT_COMMIT
201      * @exception SQLException
202      * if a database access error occurs or the given parameters
203      * are not ResultSet constants indicating type, concurrency,
204      * and holdability
205      * @return a new Statement object that will generate ResultSet objects with
206      * the given type, concurrency, and holdability
207      */

208     public Statement createStatement(int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException {
209         if (isClosed) {
210             throw new SQLException("Pooled connection is closed");
211         }
212         return realConn.createStatement(resultSetType, resultSetConcurrency, resultSetHoldability);
213     }
214
215     /**
216      * Executes the prepareStatement method of the ConnectionWrapper object
217      *
218      * @param sql
219      * a String object that is the SQL statement to be sent to the
220      * database; may contain one or more ? IN parameters
221      * @param resultSetType
222      * one of the following ResultSet constants:
223      * ResultSet.TYPE_FORWARD_ONLY,
224      * ResultSet.TYPE_SCROLL_INSENSITIVE, or
225      * ResultSet.TYPE_SCROLL_SENSITIVE
226      * @param resultSetConcurrency
227      * one of the following ResultSet constants:
228      * ResultSet.CONCUR_READ_ONLY or ResultSet.CONCUR_UPDATABLE
229      * @param resultSetHoldability
230      * one of the following ResultSet constants:
231      * ResultSet.HOLD_CURSORS_OVER_COMMIT or
232      * ResultSet.CLOSE_CURSORS_AT_COMMIT
233      * @exception SQLException
234      * if a database access error occurs or the given parameters
235      * are not ResultSet constants indicating type, concurrency,
236      * and holdability
237      * @return a new Statement object that will generate ResultSet objects with
238      * the given type, concurrency, and holdability
239      */

240     public PreparedStatement prepareStatement(String JavaDoc sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException {
241         if (isClosed) {
242             throw new SQLException("Pooled connection is closed");
243         }
244         return realConn.prepareStatement(sql, resultSetType, resultSetConcurrency, resultSetHoldability);
245     }
246
247     /**
248      * Executes the prepareStatement method of the ConnectionWrapper object
249      *
250      * @param sql
251      * a String object that is the SQL statement to be sent to the
252      * database; may contain one or more ? IN parameters
253      * @param autoGeneratedKeys
254      * a flag indicating whether auto-generated keys should be
255      * returned; one of the following Statement constants:
256      * @exception SQLException
257      * if a database access error occurs or the given parameters
258      * are not ResultSet constants indicating type, concurrency,
259      * and holdability
260      * @return a new Statement object that will generate ResultSet objects with
261      * the given type, concurrency, and holdability
262      */

263     public PreparedStatement prepareStatement(String JavaDoc sql, int autoGeneratedKeys) throws SQLException {
264         if (isClosed) {
265             throw new SQLException("Pooled connection is closed");
266         }
267         return realConn.prepareStatement(sql, autoGeneratedKeys);
268     }
269
270     /**
271      * Executes the prepareStatement method of the ConnectionWrapper object
272      *
273      * @param sql
274      * a String object that is the SQL statement to be sent to the
275      * database; may contain one or more ? IN parameters
276      * @param columnIndexes
277      * an array of column indexes indicating the columns that should
278      * be returned from the inserted row or rows
279      * @exception SQLException
280      * if a database access error occurs or the given parameters
281      * are not ResultSet constants indicating type, concurrency,
282      * and holdability
283      * @return a new Statement object that will generate ResultSet objects with
284      * the given type, concurrency, and holdability
285      */

286     public PreparedStatement prepareStatement(String JavaDoc sql, int[] columnIndexes) throws SQLException {
287         if (isClosed) {
288             throw new SQLException("Pooled connection is closed");
289         }
290         return realConn.prepareStatement(sql, columnIndexes);
291     }
292
293     /**
294      * Executes the prepareStatement method of the ConnectionWrapper object
295      *
296      * @param sql
297      * a String object that is the SQL statement to be sent to the
298      * database; may contain one or more ? IN parameters
299      * @param columnNames -
300      * an array of column names indicating the columns that should be
301      * returned from the inserted row or rows
302      * @exception SQLException
303      * if a database access error occurs or the given parameters
304      * are not ResultSet constants indicating type, concurrency,
305      * and holdability
306      * @return a new Statement object that will generate ResultSet objects with
307      * the given type, concurrency, and holdability
308      */

309     public PreparedStatement prepareStatement(String JavaDoc sql, String JavaDoc[] columnNames) throws SQLException {
310         if (isClosed) {
311             throw new SQLException("Pooled connection is closed");
312         }
313         return realConn.prepareStatement(sql, columnNames);
314     }
315
316     /**
317      * Executes the prepareCall method of the ConnectionWrapper object
318      *
319      * @param sql
320      * a String object that is the SQL statement to be sent to the
321      * database; may contain one or more ? IN parameters
322      * @param resultSetType
323      * one of the following ResultSet constants:
324      * ResultSet.TYPE_FORWARD_ONLY,
325      * ResultSet.TYPE_SCROLL_INSENSITIVE, or
326      * ResultSet.TYPE_SCROLL_SENSITIVE
327      * @param resultSetConcurrency
328      * one of the following ResultSet constants:
329      * ResultSet.CONCUR_READ_ONLY or ResultSet.CONCUR_UPDATABLE
330      * @param resultSetHoldability
331      * one of the following ResultSet constants:
332      * ResultSet.HOLD_CURSORS_OVER_COMMIT or
333      * ResultSet.CLOSE_CURSORS_AT_COMMIT
334      * @exception SQLException
335      * if a database access error occurs or the given parameters
336      * are not ResultSet constants indicating type, concurrency,
337      * and holdability
338      * @return a new Statement object that will generate ResultSet objects with
339      * the given type, concurrency, and holdability
340      */

341     public CallableStatement prepareCall(String JavaDoc sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException {
342         if (isClosed) {
343             throw new SQLException("Pooled connection is closed");
344         }
345         return realConn.prepareCall(sql, resultSetType, resultSetConcurrency, resultSetHoldability);
346     }
347
348     /**
349      * Executes the releaseSavepoint method of the ConnectionWrapper object
350      *
351      * @param holdability
352      * The new holdability value
353      * @exception SQLException
354      * Description of Exception
355      */

356     public void releaseSavepoint(Savepoint savepoint) throws SQLException {
357         if (isClosed) {
358             throw new SQLException("Pooled connection is closed");
359         }
360         realConn.releaseSavepoint(savepoint);
361     }
362
363     /**
364      * Sets the typeMap attribute of the ConnectionWrapper object
365      *
366      * @param map
367      * The new typeMap value
368      * @exception SQLException
369      * Description of Exception
370      */

371     public void setTypeMap(Map map) throws SQLException {
372         if (isClosed) {
373             throw new SQLException("Pooled connection is closed");
374         }
375         realConn.setTypeMap(map);
376     }
377
378     /**
379      * Sets the autoCommit attribute of the ConnectionWrapper object
380      *
381      * @param autoCommit
382      * The new autoCommit value
383      * @exception SQLException
384      * Description of Exception
385      */

386     public void setAutoCommit(boolean autoCommit) throws SQLException {
387         if (isClosed) {
388             throw new SQLException("Pooled connection is closed");
389         }
390         realConn.setAutoCommit(autoCommit);
391     }
392
393     /**
394      * Sets the catalog attribute of the ConnectionWrapper object
395      *
396      * @param catalog
397      * The new catalog value
398      * @exception SQLException
399      * Description of Exception
400      */

401     public void setCatalog(String JavaDoc catalog) throws SQLException {
402         if (isClosed) {
403             throw new SQLException("Pooled connection is closed");
404         }
405         realConn.setCatalog(catalog);
406     }
407
408     /**
409      * Sets the readOnly attribute of the ConnectionWrapper object
410      *
411      * @param readOnly
412      * The new readOnly value
413      * @exception SQLException
414      * Description of Exception
415      */

416     public void setReadOnly(boolean readOnly) throws SQLException {
417         if (isClosed) {
418             throw new SQLException("Pooled connection is closed");
419         }
420         realConn.setReadOnly(readOnly);
421     }
422
423     /**
424      * Sets the transactionIsolation attribute of the ConnectionWrapper object
425      *
426      * @param level
427      * The new transactionIsolation value
428      * @exception SQLException
429      * Description of Exception
430      */

431     public void setTransactionIsolation(int level) throws SQLException {
432         if (isClosed) {
433             throw new SQLException("Pooled connection is closed");
434         }
435         realConn.setTransactionIsolation(level);
436     }
437
438     /**
439      * Gets the timesUsed attribute of the ConnectionWrapper object
440      *
441      * @return The timesUsed value
442      */

443     public int getTimesUsed() {
444
445         return timesUsed;
446     }
447
448     /**
449      * Gets the timeCreated attribute of the ConnectionWrapper object
450      *
451      * @return The timeCreated value
452      */

453     public long getTimeCreated() {
454
455         return timeCreated;
456     }
457
458     /**
459      * Gets the lastTimeUsed attribute of the ConnectionWrapper object
460      *
461      * @return The lastTimeUsed value
462      */

463     public long getLastTimeUsed() {
464
465         return lastTimeUsed;
466     }
467
468     /**
469      * Gets the connectionId attribute of the ConnectionWrapper object
470      *
471      * @return The connectionId value
472      */

473     public long getConnectionId() {
474
475         return id;
476     }
477
478     /**
479      * Gets the currentActivity attribute of the ConnectionWrapper object
480      *
481      * @return The currentActivity value
482      */

483     public String JavaDoc getCurrentActivity() {
484
485         return this.currentActivity;
486     }
487
488     /**
489      * Returns true if the ConnectionWrapper is closed, false otherwise.
490      *
491      * @return The closed value
492      * @exception SQLException
493      * Description of Exception
494      */

495     public boolean isClosed() throws SQLException {
496
497         return isClosed;
498     }
499
500     /**
501      * Gets the autoCommit attribute of the ConnectionWrapper object
502      *
503      * @return The autoCommit value
504      * @exception SQLException
505      * Description of Exception
506      */

507     public boolean getAutoCommit() throws SQLException {
508         if (isClosed) {
509             throw new SQLException("Pooled connection is closed");
510         }
511         return realConn.getAutoCommit();
512     }
513
514     /**
515      * Gets the catalog attribute of the ConnectionWrapper object
516      *
517      * @return The catalog value
518      * @exception SQLException
519      * Description of Exception
520      */

521     public String JavaDoc getCatalog() throws SQLException {
522         if (isClosed) {
523             throw new SQLException("Pooled connection is closed");
524         }
525         return realConn.getCatalog();
526     }
527
528     /**
529      * Gets the metaData attribute of the ConnectionWrapper object
530      *
531      * @return The metaData value
532      * @exception SQLException
533      * Description of Exception
534      */

535     public DatabaseMetaData getMetaData() throws SQLException {
536         if (isClosed) {
537             throw new SQLException("Pooled connection is closed");
538         }
539         return realConn.getMetaData();
540     }
541
542     /**
543      * Gets the typeMap attribute of the ConnectionWrapper object
544      *
545      * @return The typeMap value
546      * @exception SQLException
547      * Description of Exception
548      */

549     public Map getTypeMap() throws SQLException {
550         if (isClosed) {
551             throw new SQLException("Pooled connection is closed");
552         }
553         return realConn.getTypeMap();
554     }
555
556     /**
557      * Gets the transactionIsolation attribute of the ConnectionWrapper object
558      *
559      * @return The transactionIsolation value
560      * @exception SQLException
561      * Description of Exception
562      */

563     public int getTransactionIsolation() throws SQLException {
564         if (isClosed) {
565             throw new SQLException("Pooled connection is closed");
566         }
567         return realConn.getTransactionIsolation();
568     }
569
570     /**
571      * Gets the warnings attribute of the ConnectionWrapper object
572      *
573      * @return The warnings value
574      * @exception SQLException
575      * Description of Exception
576      */

577     public SQLWarning getWarnings() throws SQLException {
578         if (isClosed) {
579             throw new SQLException("Pooled connection is closed");
580         }
581         return realConn.getWarnings();
582     }
583
584     /**
585      * Gets the readOnly attribute of the ConnectionWrapper object
586      *
587      * @return The readOnly value
588      * @exception SQLException
589      * Description of Exception
590      */

591     public boolean isReadOnly() throws SQLException {
592         if (isClosed) {
593             throw new SQLException("Pooled connection is closed");
594         }
595         return realConn.isReadOnly();
596     }
597
598     /**
599      * Description of the Method
600      */

601     public void incrementTimesUsed() {
602
603         timesUsed++;
604         setLastTimeUsed();
605
606     }
607
608     /**
609      * Inform the ConnectionPool that the ConnectionWrapper is closed.
610      *
611      * @exception SQLException
612      * Description of Exception
613      */

614     public void close() throws SQLException {
615
616         pool.freeConnection(this);
617
618     }
619
620     /**
621      * Description of the Method
622      *
623      * @exception SQLException
624      * Description of Exception
625      */

626     public void closeConnection() throws SQLException {
627
628         try {
629             realConn.close();
630         } catch (Exception JavaDoc e) {
631         } finally {
632             realConn = null;
633             isClosed = true;
634         }
635
636     }
637
638     /*
639      * Wrapped methods.
640      */

641     /**
642      * Description of the Method
643      *
644      * @exception SQLException
645      * Description of Exception
646      */

647     public void clearWarnings() throws SQLException {
648         if (isClosed) {
649             throw new SQLException("Pooled connection is closed");
650         }
651         realConn.clearWarnings();
652     }
653
654     /**
655      * Description of the Method
656      *
657      * @exception SQLException
658      * Description of Exception
659      */

660     public void commit() throws SQLException {
661         if (isClosed) {
662             throw new SQLException("Pooled connection is closed");
663         }
664         realConn.commit();
665     }
666
667     /**
668      * Description of the Method
669      *
670      * @return Description of the Returned Value
671      * @exception SQLException
672      * Description of Exception
673      */

674     public Statement createStatement() throws SQLException {
675         if (isClosed) {
676             throw new SQLException("Pooled connection is closed");
677         }
678         return realConn.createStatement();
679     }
680
681     /**
682      * Description of the Method
683      *
684      * @param resultSetType
685      * Description of Parameter
686      * @param resultSetConcurrency
687      * Description of Parameter
688      * @return Description of the Returned Value
689      * @exception SQLException
690      * Description of Exception
691      */

692     public Statement createStatement(int resultSetType, int resultSetConcurrency) throws SQLException {
693         if (isClosed) {
694             throw new SQLException("Pooled connection is closed");
695         }
696         return realConn.createStatement(resultSetType, resultSetConcurrency);
697     }
698
699     /**
700      * Description of the Method
701      *
702      * @param sql
703      * Description of Parameter
704      * @return Description of the Returned Value
705      * @exception SQLException
706      * Description of Exception
707      */

708     public String JavaDoc nativeSQL(String JavaDoc sql) throws SQLException {
709         if (isClosed) {
710             throw new SQLException("Pooled connection is closed");
711         }
712         return realConn.nativeSQL(sql);
713     }
714
715     /**
716      * Description of the Method
717      *
718      * @param sql
719      * Description of Parameter
720      * @return Description of the Returned Value
721      * @exception SQLException
722      * Description of Exception
723      */

724     public CallableStatement prepareCall(String JavaDoc sql) throws SQLException {
725         if (isClosed) {
726             throw new SQLException("Pooled connection is closed");
727         }
728         return realConn.prepareCall(sql);
729     }
730
731     /**
732      * Description of the Method
733      *
734      * @param sql
735      * Description of Parameter
736      * @param resultSetType
737      * Description of Parameter
738      * @param resultSetConcurrency
739      * Description of Parameter
740      * @return Description of the Returned Value
741      * @exception SQLException
742      * Description of Exception
743      */

744     public CallableStatement prepareCall(String JavaDoc sql, int resultSetType, int resultSetConcurrency) throws SQLException {
745         if (isClosed) {
746             throw new SQLException("Pooled connection is closed");
747         }
748         return realConn.prepareCall(sql, resultSetType, resultSetConcurrency);
749     }
750
751     /**
752      * Description of the Method
753      *
754      * @param sql
755      * Description of Parameter
756      * @return Description of the Returned Value
757      * @exception SQLException
758      * Description of Exception
759      */

760     public PreparedStatement prepareStatement(String JavaDoc sql) throws SQLException {
761         if (isClosed) {
762             throw new SQLException("Pooled connection is closed");
763         }
764         return realConn.prepareStatement(sql);
765     }
766
767     /**
768      * Description of the Method
769      *
770      * @param sql
771      * Description of Parameter
772      * @param resultSetType
773      * Description of Parameter
774      * @param resultSetConcurrency
775      * Description of Parameter
776      * @return Description of the Returned Value
777      * @exception SQLException
778      * Description of Exception
779      */

780     public PreparedStatement prepareStatement(String JavaDoc sql, int resultSetType, int resultSetConcurrency) throws SQLException {
781         if (isClosed) {
782             throw new SQLException("Pooled connection is closed");
783         }
784         return realConn.prepareStatement(sql, resultSetType, resultSetConcurrency);
785     }
786
787     /**
788      * Description of the Method
789      *
790      * @exception SQLException
791      * Description of Exception
792      */

793     public void rollback() throws SQLException {
794         if (isClosed) {
795             throw new SQLException("Pooled connection is closed");
796         }
797         realConn.rollback();
798     }
799 }
800
Popular Tags