KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > protomatter > jdbc > pool > JdbcConnectionPoolConnection


1 package com.protomatter.jdbc.pool;
2
3 /**
4  * {{{ The Protomatter Software License, Version 1.0
5  * derived from The Apache Software License, Version 1.1
6  *
7  * Copyright (c) 1998-2002 Nate Sammons. All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in
18  * the documentation and/or other materials provided with the
19  * distribution.
20  *
21  * 3. The end-user documentation included with the redistribution,
22  * if any, must include the following acknowledgment:
23  * "This product includes software developed for the
24  * Protomatter Software Project
25  * (http://protomatter.sourceforge.net/)."
26  * Alternately, this acknowledgment may appear in the software itself,
27  * if and wherever such third-party acknowledgments normally appear.
28  *
29  * 4. The names "Protomatter" and "Protomatter Software Project" must
30  * not be used to endorse or promote products derived from this
31  * software without prior written permission. For written
32  * permission, please contact support@protomatter.com.
33  *
34  * 5. Products derived from this software may not be called "Protomatter",
35  * nor may "Protomatter" appear in their name, without prior written
36  * permission of the Protomatter Software Project
37  * (support@protomatter.com).
38  *
39  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
40  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
41  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
42  * DISCLAIMED. IN NO EVENT SHALL THE PROTOMATTER SOFTWARE PROJECT OR
43  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
45  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
46  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
47  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
48  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
49  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50  * SUCH DAMAGE. }}}
51  */

52
53 import java.io.*;
54 import java.sql.*;
55 import java.util.*;
56 import java.text.MessageFormat JavaDoc;
57 import com.protomatter.util.*;
58 import com.protomatter.pool.*;
59 import com.protomatter.syslog.*;
60
61 /**
62  * A java.sql.Connection that's part of a pool of conections. When this
63  * connection is closed, it is checked back into the pool, and it's options
64  * like transaction isolation level, auto-commit, type map, etc are all reset to
65  * defaults.
66  *
67  * @see java.sql.Connection
68  * @see JdbcConnectionPoolDriver
69  * @see JdbcConnectionPool
70  */

71 public class JdbcConnectionPoolConnection
72 implements ObjectPoolObject, Connection, SyslogChannelAware
73 {
74   /**
75    * The pool that this connection is associated with.
76    */

77   protected JdbcConnectionPool pool = null;
78
79   /**
80    * The actual connection to the database.
81    */

82   protected Connection connection = null;
83
84   private boolean isDeleted = false;
85   private boolean isClosed = false;
86   private boolean isValid = true;
87
88   private String JavaDoc url = null;
89   private Properties props = null;
90
91   private String JavaDoc originalCatalog = null;
92   private Map originalTypeMap = null;
93   private int originalTransactionIsolation = -1;
94   private boolean originalAutoCommit = false;
95   private boolean originalReadOnly = false;
96   private boolean canCheckAutoCommit = true;
97
98   // when was this thing last used?
99
private long lastTimeUsed = 0;
100   private JdbcCheckoutExceptionTrace checkoutStackException = null;
101
102   // did they set any of these?
103
private boolean setCatalog;
104   private boolean setTransactionIsolation;
105   private boolean setAutoCommit;
106   private boolean setReadOnly;
107   private boolean setTypeMap;
108
109   JdbcConnectionPoolConnection(JdbcConnectionPool pool, String JavaDoc url, Properties props)
110   throws SQLException
111   {
112     this.isDeleted = false;
113     this.isClosed = false;
114     this.pool = pool;
115     this.url = url;
116     this.props = props;
117     init(false);
118   }
119
120   /**
121    * Get the pool that this connection is associated with.
122    */

123   public JdbcConnectionPool getConnectionPool()
124   {
125     return this.pool;
126   }
127
128   /**
129    * Get the connection that this object wraps. This method
130    * should only be called if you <i>really</i> know what
131    * you're doing. This method exposes the connection
132    * that is actually connected to the database, but you should
133    * not really have any need to get ahold of it. This method
134    * will return <tt>null</tt> if you have called the
135    * <tt>close()</tt> method already.
136    */

137   public Connection getConnection()
138   {
139     return this.connection;
140   }
141
142   /**
143    * Returns the channel information from the pool
144    * this connection is associated with.
145    *
146    * @see com.protomatter.syslog.SyslogChannelAware
147    */

148   public Object JavaDoc getSyslogChannel()
149   {
150     return pool.getSyslogChannel();
151   }
152
153   /**
154    * Get the last time that someone used this connection.
155    */

156   long getLastTimeUsed()
157   {
158     return this.lastTimeUsed;
159   }
160
161   /**
162    * Reset the last time that someone used this connection.
163    */

164   void resetLastTimeUsed()
165   {
166     this.lastTimeUsed = System.currentTimeMillis();
167   }
168
169   /**
170    * Set the stack trace of when this connection was checked out.
171    */

172   void setCheckoutStackTrace(JdbcCheckoutExceptionTrace x)
173   {
174     this.checkoutStackException = x;
175   }
176
177   /**
178    * Get the stack trace of when this connection was checked out.
179    */

180   JdbcCheckoutExceptionTrace getCheckoutStackTrace()
181   {
182     return this.checkoutStackException;
183   }
184
185   private void init(boolean verbose)
186   throws SQLException
187   {
188     try
189     {
190       if (verbose)
191       {
192         if (pool.useSyslog())
193         {
194           Syslog.info(this, MessageFormat.format(
195             PoolResources.getResourceString(MessageConstants.GETTING_CONNECTION_MESSAGE),
196             new Object JavaDoc[] { pool.getName(), url }));
197         }
198         else
199         {
200           PrintWriter writer = DriverManager.getLogWriter();
201           if (writer != null)
202           {
203             writer.println("JdbcConnectionPool: " +
204               MessageFormat.format(PoolResources.getResourceString(MessageConstants.GETTING_CONNECTION_MESSAGE),
205               new Object JavaDoc[] { pool.getName(), url }));
206           }
207         }
208       }
209
210       // ask the driver directly for a connection. Don't bother
211
// with the DriverManager.
212
this.connection = pool.getDriver().connect(url, props);
213
214       try
215       {
216         originalCatalog = this.connection.getCatalog();
217       }
218       catch (SQLException cx)
219       {
220         // don't care... some drivers (Sybase) will throw a SQLException
221
// if certain metdata tables aren't installed.
222
originalCatalog = null;
223       }
224
225       try
226       {
227         originalTypeMap = this.connection.getTypeMap();
228       }
229       catch (AbstractMethodError JavaDoc error) // Driver is not JDBC 2.0 compliant
230
{
231         originalTypeMap = null;
232       }
233       catch (Exception JavaDoc xx) // Driver is not JDBC 2.0 compliant
234
{
235         originalTypeMap = null;
236       }
237
238       // Some drivers freak out if you even *ask* them about
239
// this stuff. Informix? I'm looking at you...
240
try
241       {
242         originalTransactionIsolation = this.connection.getTransactionIsolation();
243       }
244       catch (SQLException x)
245       {
246         if (verbose)
247         {
248           if (pool.useSyslog())
249           {
250             Syslog.info(this, MessageFormat.format(PoolResources.getResourceString(MessageConstants.CANNOT_ASK_ISOLATION_MESSAGE),
251               new Object JavaDoc[] { pool.getName(), x.toString() } ));
252           }
253           else
254           {
255             PrintWriter writer = DriverManager.getLogWriter();
256             if (writer != null)
257             {
258               writer.println("JdbcConnectionPool: " +
259                 MessageFormat.format(PoolResources.getResourceString(MessageConstants.CANNOT_ASK_ISOLATION_MESSAGE),
260                 new Object JavaDoc[] { pool.getName(), x.toString() } ));
261             }
262           }
263         }
264       }
265
266       try
267       {
268         originalAutoCommit = this.connection.getAutoCommit();
269         this.canCheckAutoCommit = true;
270       }
271       catch (SQLException x)
272       {
273         this.canCheckAutoCommit = false;
274         if (verbose)
275         {
276           if (pool.useSyslog())
277           {
278             Syslog.info(this, MessageFormat.format(
279               PoolResources.getResourceString(MessageConstants.CANNOT_ASK_AUTOCOMMIT_MESSAGE),
280               new Object JavaDoc[] { pool.getName(), x.toString() } ));
281           }
282           else
283           {
284             PrintWriter writer = DriverManager.getLogWriter();
285             if (writer != null)
286             {
287               writer.println("JdbcConnectionPool: " +
288                 MessageFormat.format(
289                 PoolResources.getResourceString(MessageConstants.CANNOT_ASK_AUTOCOMMIT_MESSAGE),
290                 new Object JavaDoc[] { pool.getName(), x.toString() } ));
291             }
292           }
293         }
294       }
295
296       try
297       {
298         originalReadOnly = this.connection.isReadOnly();
299       }
300       catch (SQLException x)
301       {
302         if (verbose)
303         {
304           if (pool.useSyslog())
305           {
306             Syslog.info(this, MessageFormat.format(
307               PoolResources.getResourceString(MessageConstants.CANNOT_ASK_READONLY_MESSAGE),
308               new Object JavaDoc[] { pool.getName(), x.toString() } ));
309           }
310           else
311           {
312             PrintWriter writer = DriverManager.getLogWriter();
313             if (writer != null)
314             {
315               writer.println("JdbcConnectionPool: " +
316                 MessageFormat.format(PoolResources.getResourceString(
317                 MessageConstants.CANNOT_ASK_READONLY_MESSAGE),
318                 new Object JavaDoc[] { pool.getName(), x.toString() } ));
319             }
320           }
321         }
322       }
323
324       this.isValid = true;
325     }
326     catch (SQLException x)
327     {
328       if (verbose)
329       {
330         if (pool.useSyslog())
331         {
332           Syslog.info(this, MessageFormat.format(
333             PoolResources.getResourceString(MessageConstants.EXCEPTION_RECREATING_CONNECTION_MESSAGE),
334             new Object JavaDoc[] { pool.getName() } ), x);
335         }
336         else
337         {
338           PrintWriter pw = DriverManager.getLogWriter();
339           if (pw != null)
340           {
341             pw.println("JdbcConnectionPool: " +
342               MessageFormat.format(
343               PoolResources.getResourceString(MessageConstants.EXCEPTION_RECREATING_CONNECTION_MESSAGE),
344               new Object JavaDoc[] { pool.getName() } ));
345             x.printStackTrace(pw);
346           }
347         }
348       }
349       this.isValid = false;
350       throw x;
351     }
352   }
353
354   /**
355    * Used by the connection pool.
356    *
357    * @see com.protomatter.pool.ObjectPoolObject
358    */

359   public void deleteObjectPoolObject()
360   {
361     this.isDeleted = true;
362     this.isValid = false;
363     try
364     {
365       this.connection.close();
366     }
367     catch (SQLException x)
368     {
369       // we don't actuall care what happens here...
370
;
371     }
372   }
373
374   void reallyClose()
375   {
376     // this is called when we're going to close down everything anyway.
377
try
378     {
379       this.connection.close();
380       this.isValid = false;
381       this.isClosed = true;
382     }
383     catch (SQLException x)
384     {
385       ;
386     }
387   }
388
389   /**
390    * Will check that this connection is working and refresh it if not.
391    * This executes the validity check statement that was set when
392    * the connection pool was created. If that statement was not set,
393    * the connection is refreshed no matter what. If the statement was
394    * set, it is executed and if an exception is generated, the connection
395    * is refreshed. If there's a problem refreshing the connection, this
396    * connection wrapper is invalidated -- you will have to either keep
397    * calling refresh() until it doesn't throw a SQLException, or call
398    * close() and open another connection using the JdbcConnectionPoolDriver.
399    * If <tt>verbose</TT> is true, messages are written to Syslog
400    * during the refresh operation.
401    *
402    * @exception SQLException If the connection needs refreshing and there is
403    * a problem re-opening the connection.
404    */

405   public synchronized void refresh(boolean verbose)
406   throws SQLException
407   {
408     if (verbose)
409     {
410       if (pool.useSyslog())
411       {
412         Syslog.info(this,
413           MessageFormat.format(PoolResources.getResourceString(MessageConstants.REFRESHING_CONNECTION_MESSAGE),
414           new Object JavaDoc[] { pool.getName() }));
415       }
416       else
417       {
418         PrintWriter writer = DriverManager.getLogWriter();
419         if (writer != null)
420         {
421           writer.println("JdbcConnectionPool: " +
422             MessageFormat.format(PoolResources.getResourceString(MessageConstants.REFRESHING_CONNECTION_MESSAGE),
423             new Object JavaDoc[] { pool.getName() }));
424         }
425       }
426     }
427
428     boolean needsRefresh = !validate(verbose);
429
430     // try and re-nitialize the connection. If there's an exception
431
// opening it, init() sets the isValid flag to false.
432
if (needsRefresh)
433     {
434       if (verbose)
435       {
436         if (pool.useSyslog())
437         {
438           Syslog.info(this,
439             MessageFormat.format(PoolResources.getResourceString(MessageConstants.CLOSING_CONNECTION_MESSAGE),
440             new Object JavaDoc[] { pool.getName() }));
441         }
442         else
443         {
444           PrintWriter writer = DriverManager.getLogWriter();
445           if (writer != null)
446           {
447             writer.println("JdbcConnectionPool: " +
448               MessageFormat.format(PoolResources.getResourceString(MessageConstants.CLOSING_CONNECTION_MESSAGE),
449               new Object JavaDoc[] { pool.getName() }));
450           }
451         }
452       }
453
454       try { this.connection.close(); } catch (SQLException x) { ; }
455
456       init(verbose);
457
458       // now that we have a new connection, validate it again.
459
if (!validate(verbose))
460       {
461         if (verbose)
462         {
463           if (pool.useSyslog())
464           {
465             Syslog.info(this,
466               MessageFormat.format(PoolResources.getResourceString(MessageConstants.STILL_NOT_OK_MESSAGE),
467               new Object JavaDoc[] { pool.getName() }));
468           }
469           else
470           {
471             PrintWriter writer = DriverManager.getLogWriter();
472             if (writer != null)
473             {
474               writer.println("JdbcConnectionPool: " +
475                 MessageFormat.format(PoolResources.getResourceString(MessageConstants.STILL_NOT_OK_MESSAGE),
476                 new Object JavaDoc[] { pool.getName() }));
477             }
478           }
479         }
480         throw new SQLException(
481           MessageFormat.format(PoolResources.getResourceString(MessageConstants.CONNECTION_INVALID),
482           new Object JavaDoc[] { pool.getName() }));
483       }
484     }
485     else
486     {
487       if (verbose)
488       {
489         if (pool.useSyslog())
490         {
491           Syslog.info(this,
492             MessageFormat.format(PoolResources.getResourceString(MessageConstants.CONNECTION_OK),
493             new Object JavaDoc[] { pool.getName() }));
494         }
495         else
496         {
497           PrintWriter writer = DriverManager.getLogWriter();
498           if (writer != null)
499           {
500             writer.println("JdbcConnectionPool: " +
501               MessageFormat.format(PoolResources.getResourceString(MessageConstants.CONNECTION_OK),
502               new Object JavaDoc[] { pool.getName() }));
503           }
504         }
505       }
506     }
507   }
508
509   /**
510    * Performs a non-verbose refresh.
511    *
512    * @see #refresh(boolean)
513    */

514   public synchronized void refresh()
515   throws SQLException
516   {
517     refresh(false);
518   }
519
520   /**
521    * Validate the connection.
522    *
523    * @return true If the connection is working properly.
524    * @return false If the connection needs to be refreshed.
525    */

526   private boolean validate(boolean verbose)
527   throws SQLException
528   {
529     String JavaDoc statement = pool.getValidityCheckStatement();
530     if (statement != null)
531     {
532       Statement s = null;
533       ResultSet r = null;
534       try
535       {
536         s = createStatement();
537         r = s.executeQuery(statement);
538         if (!r.next()) // read just the first row.
539
return false;
540         return true;
541       }
542       catch (Exception JavaDoc x)
543       {
544         return false;
545       }
546       finally
547       {
548         if (r != null) try { r.close(); } catch (Exception JavaDoc x) { ; }
549         if (s != null) try { s.close(); } catch (Exception JavaDoc x) { ; }
550       }
551     }
552     // if there's no validity check statement, assume the worst.
553
return false;
554   }
555
556   /**
557    * Used by the connection pool.
558    *
559    * @see com.protomatter.pool.ObjectPoolObject
560    */

561   public boolean isObjectPoolObjectValid()
562   {
563     return isValid;
564   }
565
566   /**
567    * Invalidates this connection manually. When this connection
568    * is closed, the pool will discard it.
569    */

570   public void invalidate()
571   {
572     this.isValid = false;
573   }
574
575   /**
576    * Used by the connection pool.
577    *
578    * @see com.protomatter.pool.ObjectPoolObject
579    */

580   public void beforeObjectPoolObjectCheckout()
581   {
582     if (pool.getValidateOnCheckout())
583     {
584       int numTries = pool.getMaxCheckoutRefreshAttempts();
585       long sleep = (long)pool.getCheckoutRefreshWaitTime();
586
587       for (int i=0; i<numTries || numTries < 0; i++)
588       {
589         try
590         {
591           refresh(pool.getVerboseValidate());
592           return;
593         }
594         catch (SQLException x)
595         {
596           ; // safe to ignore here.
597
}
598         Thread.yield();
599         if (sleep > 0)
600         {
601             // sleep a bit... don't care about interruptions.
602
try { Thread.sleep(sleep); }
603             catch (Exception JavaDoc x) { ; }
604         }
605       }
606       // tried a few times to refresh... no luck
607
invalidate();
608     }
609   }
610
611   /**
612    * Used by the connection pool.
613    *
614    * @see com.protomatter.pool.ObjectPoolObject
615    */

616   public void afterObjectPoolObjectCheckin()
617   {
618     // clean things up and get ready for being checked out again.
619
this.isClosed = false;
620     try
621     {
622       clearWarnings();
623
624       if (setCatalog && (originalCatalog != null))
625         setCatalog(originalCatalog);
626       setCatalog = false;
627
628       if (setTypeMap)
629         setTypeMap(originalTypeMap);
630       setTypeMap = false;
631
632       if (setTransactionIsolation)
633         setTransactionIsolation(originalTransactionIsolation);
634       setTransactionIsolation = false;
635
636       if (setAutoCommit)
637         setAutoCommit(originalAutoCommit);
638       setAutoCommit = false;
639
640       if (setReadOnly)
641         setReadOnly(originalReadOnly);
642       setReadOnly = false;
643     }
644     catch (SQLException x)
645     {
646       if (pool.useSyslog())
647       {
648         Syslog.warning(this,
649           MessageFormat.format(PoolResources.getResourceString(MessageConstants.CANNOT_RESET_CONNECTION),
650           new Object JavaDoc[] { pool.getName() }), x);
651       }
652       else
653       {
654         PrintWriter pw = DriverManager.getLogWriter();
655         pw.println("JdbcConnectionPool: " +
656           MessageFormat.format(PoolResources.getResourceString(MessageConstants.CANNOT_RESET_CONNECTION),
657           new Object JavaDoc[] { pool.getName() }));
658         x.printStackTrace(pw);
659       }
660
661       this.isValid = false;
662       try
663       {
664         this.connection.close();
665       }
666       catch (SQLException sx)
667       {
668     ; // don't really care since we're closing anyway.
669
}
670     }
671   }
672
673   private final void checkValid()
674   throws SQLException
675   {
676     if (!this.isValid)
677       throw new SQLException(PoolResources.getResourceString(MessageConstants.CONNECTION_INVALID_REFRESH));
678     lastTimeUsed = System.currentTimeMillis();
679   }
680
681   /**
682    * See <tt>java.sql.Connection</tt>.
683    *
684    * @see java.sql.Connection
685    *
686    * @exception SQLException Because the underlying connection can throw one.
687    */

688   public Statement createStatement()
689   throws SQLException
690   {
691     checkValid();
692     return this.connection.createStatement();
693   }
694
695   /**
696    * See <tt>java.sql.Connection</tt>.
697    *
698    * @see java.sql.Connection
699    *
700    * @exception SQLException Because the underlying connection can throw one.
701    */

702   public PreparedStatement prepareStatement(String JavaDoc sql)
703   throws SQLException
704   {
705     checkValid();
706     return this.connection.prepareStatement(sql);
707   }
708
709   /**
710    * See <tt>java.sql.Connection</tt>.
711    *
712    * @see java.sql.Connection
713    *
714    * @exception SQLException Because the underlying connection can throw one.
715    */

716   public CallableStatement prepareCall(String JavaDoc sql)
717   throws SQLException
718   {
719     checkValid();
720     return this.connection.prepareCall(sql);
721   }
722
723   /**
724    * See <tt>java.sql.Connection</tt>.
725    *
726    * @see java.sql.Connection
727    *
728    * @exception SQLException Because the underlying connection can throw one.
729    */

730   public String JavaDoc nativeSQL(String JavaDoc sql)
731   throws SQLException
732   {
733     checkValid();
734     return this.connection.nativeSQL(sql);
735   }
736
737   /**
738    * See <tt>java.sql.Connection</tt>.
739    *
740    * @see java.sql.Connection
741    *
742    * @exception SQLException Because the underlying connection can throw one.
743    */

744   public void setAutoCommit(boolean autoCommit)
745   throws SQLException
746   {
747     checkValid();
748     this.connection.setAutoCommit(autoCommit);
749     this.setAutoCommit = true;
750   }
751
752   /**
753    * See <tt>java.sql.Connection</tt>.
754    *
755    * @see java.sql.Connection
756    *
757    * @exception SQLException Because the underlying connection can throw one.
758    */

759   public boolean getAutoCommit()
760   throws SQLException
761   {
762     checkValid();
763     return this.connection.getAutoCommit();
764   }
765
766   /**
767    * See <tt>java.sql.Connection</tt>.
768    *
769    * @see java.sql.Connection
770    *
771    * @exception SQLException Because the underlying connection can throw one.
772    */

773   public void commit()
774   throws SQLException
775   {
776     checkValid();
777     this.connection.commit();
778   }
779
780   /**
781    * See <tt>java.sql.Connection</tt>.
782    *
783    * @see java.sql.Connection
784    *
785    * @exception SQLException Because the underlying connection can throw one.
786    */

787   public void rollback()
788   throws SQLException
789   {
790     checkValid();
791     this.connection.rollback();
792   }
793
794   /**
795    * Close the connection. The underlying connection is checked
796    * back into the pool so it can be used by someone else. If this
797    * method completes without throwing a
798    * <tt>SQLException</tt>, then calling any method on this class
799    * (except this <tt>close()</tt> method) will throw a
800    * <tt>SQLException</tt> stating that the connection is closed.
801    * Repeatedly calling this method has no effect and will not
802    * throw exceptions (which some JDBC drivers do).<P>
803    *
804    * This method will call <tt>commit()</tt> on the connection
805    * if auto-commit is turned on, and will call <tt>rollback()</tt>
806    * otherwise.
807    *
808    * @see java.sql.Connection
809    *
810    * @exception SQLException Because the underlying connection can throw one.
811    */

812   public void close()
813   throws SQLException
814   {
815     // check the this.connection back in... don't close it.
816
if (isClosed())
817       return;
818
819     if (this.canCheckAutoCommit)
820     {
821       if (this.connection.getAutoCommit())
822       {
823         try
824         {
825           this.connection.commit();
826         }
827         catch (SQLException x)
828         {
829           // this is here since the mm.mysql driver freaks out if
830
// you call commit and autocommit is true. Bah!
831
; // ignore
832
}
833       }
834       else
835       {
836     // if we are not doing autocommit, when they
837
// close the connection we should rollback so
838
// that the next time this connection is taken
839
// from the pool it is completely fresh.
840
try
841     {
842       this.connection.rollback();
843     }
844     catch (SQLException x)
845     {
846       ;
847     }
848       }
849     }
850
851     this.isClosed = true;
852     try
853     {
854       pool.checkin(this);
855     }
856    catch (Exception JavaDoc x)
857     {
858       throw new PoolSQLException(MessageFormat.format(
859         PoolResources.getResourceString(MessageConstants.CANNOT_CHECKIN_CONNECTION_MESSAGE),
860         new Object JavaDoc[] { pool.getName() }), x);
861     }
862   }
863
864   /**
865    * See <tt>java.sql.Connection</tt>.
866    *
867    * @see java.sql.Connection
868    *
869    * @exception SQLException Because the underlying connection can throw one.
870    */

871   public boolean isClosed()
872   throws SQLException
873   {
874     // since we don't really close the this.connection, we need to
875
// tell the if they think the closed it ;-)
876
return this.isClosed;
877   }
878
879   /**
880    * See <tt>java.sql.Connection</tt>.
881    *
882    * @see java.sql.Connection
883    *
884    * @exception SQLException Because the underlying connection can throw one.
885    */

886   public DatabaseMetaData getMetaData()
887   throws SQLException
888   {
889     checkValid();
890     return this.connection.getMetaData();
891   }
892
893   /**
894    * See <tt>java.sql.Connection</tt>.
895    *
896    * @see java.sql.Connection
897    *
898    * @exception SQLException Because the underlying connection can throw one.
899    */

900   public void setReadOnly(boolean readOnly)
901   throws SQLException
902   {
903     checkValid();
904     this.connection.setReadOnly(readOnly);
905     this.setReadOnly = true;
906   }
907
908   /**
909    * See <tt>java.sql.Connection</tt>.
910    *
911    * @see java.sql.Connection
912    *
913    * @exception SQLException Because the underlying connection can throw one.
914    */

915   public boolean isReadOnly()
916   throws SQLException
917   {
918     checkValid();
919     return this.connection.isReadOnly();
920   }
921
922   /**
923    * See <tt>java.sql.Connection</tt>.
924    *
925    * @see java.sql.Connection
926    *
927    * @exception SQLException Because the underlying connection can throw one.
928    */

929   public void setCatalog(String JavaDoc catalog)
930   throws SQLException
931   {
932     checkValid();
933     this.connection.setCatalog(catalog);
934     this.setCatalog = true;
935   }
936
937   /**
938    * See <tt>java.sql.Connection</tt>.
939    *
940    * @see java.sql.Connection
941    *
942    * @exception SQLException Because the underlying connection can throw one.
943    */

944   public String JavaDoc getCatalog()
945   throws SQLException
946   {
947     checkValid();
948     return this.connection.getCatalog();
949   }
950
951   /**
952    * See <tt>java.sql.Connection</tt>.
953    *
954    * @see java.sql.Connection
955    *
956    * @exception SQLException Because the underlying connection can throw one.
957    */

958   public void setTransactionIsolation(int level)
959   throws SQLException
960   {
961     checkValid();
962     this.connection.setTransactionIsolation(level);
963     this.setTransactionIsolation = true;
964   }
965
966   /**
967    * See <tt>java.sql.Connection</tt>.
968    *
969    * @see java.sql.Connection
970    *
971    * @exception SQLException Because the underlying connection can throw one.
972    */

973   public int getTransactionIsolation()
974   throws SQLException
975   {
976     checkValid();
977     return this.connection.getTransactionIsolation();
978   }
979
980   /**
981    * See <tt>java.sql.Connection</tt>.
982    *
983    * @see java.sql.Connection
984    *
985    * @exception SQLException Because the underlying connection can throw one.
986    */

987   public SQLWarning getWarnings()
988   throws SQLException
989   {
990     checkValid();
991     return this.connection.getWarnings();
992   }
993
994   /**
995    * See <tt>java.sql.Connection</tt>.
996    *
997    * @see java.sql.Connection
998    *
999    * @exception SQLException Because the underlying connection can throw one.
1000   */

1001  public void clearWarnings()
1002  throws SQLException
1003  {
1004    checkValid();
1005    this.connection.clearWarnings();
1006  }
1007
1008  /**
1009   * See <tt>java.sql.Connection</tt>.
1010   *
1011   * @see java.sql.Connection
1012   *
1013   * @exception SQLException Because the underlying connection can throw one.
1014   */

1015  public Statement createStatement(int resultSetType, int resultSetConcurrency)
1016  throws SQLException
1017  {
1018    checkValid();
1019    return this.connection.createStatement(resultSetType, resultSetConcurrency);
1020  }
1021
1022  /**
1023   * See <tt>java.sql.Connection</tt>.
1024   *
1025   * @see java.sql.Connection
1026   *
1027   * @exception SQLException Because the underlying connection can throw one.
1028   */

1029  public Map getTypeMap()
1030  throws SQLException
1031  {
1032    checkValid();
1033    return this.connection.getTypeMap();
1034  }
1035
1036  /**
1037   * See <tt>java.sql.Connection</tt>.
1038   *
1039   * @see java.sql.Connection
1040   *
1041   * @exception SQLException Because the underlying connection can throw one.
1042   */

1043  public CallableStatement prepareCall(String JavaDoc sql, int resultSetType, int resultSetConcurrency)
1044  throws SQLException
1045  {
1046    checkValid();
1047    return this.connection.prepareCall(sql, resultSetType, resultSetConcurrency);
1048  }
1049
1050  /**
1051   * See <tt>java.sql.Connection</tt>.
1052   *
1053   * @see java.sql.Connection
1054   *
1055   * @exception SQLException Because the underlying connection can throw one.
1056   */

1057  public PreparedStatement prepareStatement(String JavaDoc sql, int resultSetType, int resultSetConcurrency)
1058  throws SQLException
1059  {
1060    checkValid();
1061    return this.connection.prepareStatement(sql, resultSetType, resultSetConcurrency);
1062  }
1063
1064  /**
1065   * See <tt>java.sql.Connection</tt>.
1066   *
1067   * @see java.sql.Connection
1068   *
1069   * @exception SQLException Because the underlying connection can throw one.
1070   */

1071  public void setTypeMap(Map typeMap)
1072  throws SQLException
1073  {
1074    checkValid();
1075    this.connection.setTypeMap(typeMap);
1076    this.setTypeMap = true;
1077  }
1078}
1079
Popular Tags