KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > resource > adapter > jdbc > WrappedConnection


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software 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 software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.resource.adapter.jdbc;
23
24 import java.sql.CallableStatement JavaDoc;
25 import java.sql.Connection JavaDoc;
26 import java.sql.DatabaseMetaData JavaDoc;
27 import java.sql.PreparedStatement JavaDoc;
28 import java.sql.ResultSet JavaDoc;
29 import java.sql.SQLException JavaDoc;
30 import java.sql.SQLWarning JavaDoc;
31 import java.sql.Savepoint JavaDoc;
32 import java.sql.Statement JavaDoc;
33 import java.util.HashMap JavaDoc;
34 import java.util.Iterator JavaDoc;
35 import java.util.Map JavaDoc;
36
37 import org.jboss.logging.Logger;
38 import org.jboss.util.NestedSQLException;
39
40 /**
41  * A wrapper for a connection.
42  *
43  * @author <a HREF="mailto:d_jencks@users.sourceforge.net">David Jencks</a>
44  * @author <a HREF="mailto:adrian@jboss.com">Adrian Brock</a>
45  * @version $Revision: 54926 $
46  */

47 public class WrappedConnection implements Connection JavaDoc
48 {
49    private static final Logger log = Logger.getLogger(WrappedConnection.class);
50
51    private BaseWrapperManagedConnection mc;
52
53    private WrapperDataSource dataSource;
54    
55    private HashMap JavaDoc statements;
56
57    private boolean closed = false;
58
59    private int trackStatements;
60    
61    public WrappedConnection(final BaseWrapperManagedConnection mc)
62    {
63       this.mc = mc;
64       if (mc != null)
65          trackStatements = mc.getTrackStatements();
66    }
67
68    void setManagedConnection(final BaseWrapperManagedConnection mc)
69    {
70       this.mc = mc;
71       if (mc != null)
72          trackStatements = mc.getTrackStatements();
73    }
74
75    public WrapperDataSource getDataSource()
76    {
77       return dataSource;
78    }
79    
80    protected void setDataSource(WrapperDataSource dataSource)
81    {
82       this.dataSource = dataSource;
83    }
84    
85    public void setReadOnly(boolean readOnly) throws SQLException JavaDoc
86    {
87       checkStatus();
88       mc.setJdbcReadOnly(readOnly);
89    }
90
91    public boolean isReadOnly() throws SQLException JavaDoc
92    {
93       checkStatus();
94       return mc.isJdbcReadOnly();
95    }
96
97    public void close() throws SQLException JavaDoc
98    {
99       closed = true;
100       if (mc != null)
101       {
102          if (trackStatements != BaseWrapperManagedConnectionFactory.TRACK_STATEMENTS_FALSE_INT)
103          {
104             synchronized (this)
105             {
106                if (statements != null)
107                {
108                   for (Iterator JavaDoc i = statements.entrySet().iterator(); i.hasNext(); )
109                   {
110                      Map.Entry JavaDoc entry = (Map.Entry JavaDoc) i.next();
111                      WrappedStatement ws = (WrappedStatement) entry.getKey();
112                      if (trackStatements == BaseWrapperManagedConnectionFactory.TRACK_STATEMENTS_TRUE_INT)
113                      {
114                         Throwable JavaDoc stackTrace = (Throwable JavaDoc) entry.getValue();
115                         log.warn("Closing a statement you left open, please do your own housekeeping", stackTrace);
116                      }
117                      try
118                      {
119                         ws.internalClose();
120                      }
121                      catch (Throwable JavaDoc t)
122                      {
123                         log.warn("Exception trying to close statement:", t);
124                      }
125                   }
126                }
127             }
128          }
129          mc.closeHandle(this);
130       }
131       mc = null;
132       dataSource = null;
133    }
134
135    public boolean isClosed() throws SQLException JavaDoc
136    {
137       return closed;
138    }
139
140    public Statement JavaDoc createStatement() throws SQLException JavaDoc
141    {
142       checkTransaction();
143       try
144       {
145          return new WrappedStatement(this, mc.getConnection().createStatement());
146       }
147       catch (Throwable JavaDoc t)
148       {
149          throw checkException(t);
150       }
151    }
152
153    public Statement JavaDoc createStatement(int resultSetType, int resultSetConcurrency) throws SQLException JavaDoc
154    {
155       checkTransaction();
156       try
157       {
158          return new WrappedStatement(this, mc.getConnection().createStatement(resultSetType, resultSetConcurrency));
159       }
160       catch (Throwable JavaDoc t)
161       {
162          throw checkException(t);
163       }
164    }
165
166    public Statement JavaDoc createStatement(int resultSetType, int resultSetConcurrency, int resultSetHoldability)
167          throws SQLException JavaDoc
168    {
169
170       checkTransaction();
171       try
172       {
173          return new WrappedStatement(this, mc.getConnection()
174                .createStatement(resultSetType, resultSetConcurrency, resultSetHoldability));
175       }
176       catch (Throwable JavaDoc t)
177       {
178          throw checkException(t);
179       }
180    }
181
182    public PreparedStatement JavaDoc prepareStatement(String JavaDoc sql) throws SQLException JavaDoc
183    {
184       checkTransaction();
185       try
186       {
187          return new WrappedPreparedStatement(this, mc.prepareStatement(sql, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY));
188       }
189       catch (Throwable JavaDoc t)
190       {
191          throw checkException(t);
192       }
193    }
194
195    public PreparedStatement JavaDoc prepareStatement(String JavaDoc sql, int resultSetType, int resultSetConcurrency)
196          throws SQLException JavaDoc
197    {
198       checkTransaction();
199       try
200       {
201          return new WrappedPreparedStatement(this, mc.prepareStatement(sql, resultSetType, resultSetConcurrency));
202       }
203       catch (Throwable JavaDoc t)
204       {
205          throw checkException(t);
206       }
207    }
208
209    public PreparedStatement JavaDoc prepareStatement(String JavaDoc sql, int resultSetType, int resultSetConcurrency,
210          int resultSetHoldability) throws SQLException JavaDoc
211    {
212       checkTransaction();
213       try
214       {
215          return new WrappedPreparedStatement(this, mc.getConnection()
216                .prepareStatement(sql, resultSetType, resultSetConcurrency, resultSetHoldability));
217       }
218       catch (Throwable JavaDoc t)
219       {
220          throw checkException(t);
221       }
222    }
223
224    public PreparedStatement JavaDoc prepareStatement(String JavaDoc sql, int autoGeneratedKeys) throws SQLException JavaDoc
225    {
226       checkTransaction();
227       try
228       {
229          return new WrappedPreparedStatement(this, mc.getConnection().prepareStatement(sql, autoGeneratedKeys));
230       }
231       catch (Throwable JavaDoc t)
232       {
233          throw checkException(t);
234       }
235    }
236
237    public PreparedStatement JavaDoc prepareStatement(String JavaDoc sql, int[] columnIndexes) throws SQLException JavaDoc
238    {
239       checkTransaction();
240       try
241       {
242          return new WrappedPreparedStatement(this, mc.getConnection().prepareStatement(sql, columnIndexes));
243       }
244       catch (Throwable JavaDoc t)
245       {
246          throw checkException(t);
247       }
248    }
249
250    public PreparedStatement JavaDoc prepareStatement(String JavaDoc sql, String JavaDoc[] columnNames) throws SQLException JavaDoc
251    {
252
253       checkTransaction();
254       try
255       {
256          return new WrappedPreparedStatement(this, mc.getConnection().prepareStatement(sql, columnNames));
257       }
258       catch (Throwable JavaDoc t)
259       {
260          throw checkException(t);
261       }
262    }
263
264    public CallableStatement JavaDoc prepareCall(String JavaDoc sql) throws SQLException JavaDoc
265    {
266       checkTransaction();
267       try
268       {
269          return new WrappedCallableStatement(this, mc.prepareCall(sql, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY));
270       }
271       catch (Throwable JavaDoc t)
272       {
273          throw checkException(t);
274       }
275    }
276
277    public CallableStatement JavaDoc prepareCall(String JavaDoc sql, int resultSetType, int resultSetConcurrency) throws SQLException JavaDoc
278    {
279       checkTransaction();
280       try
281       {
282          return new WrappedCallableStatement(this, mc.prepareCall(sql, resultSetType, resultSetConcurrency));
283       }
284       catch (Throwable JavaDoc t)
285       {
286          throw checkException(t);
287       }
288    }
289
290    public CallableStatement JavaDoc prepareCall(String JavaDoc sql, int resultSetType, int resultSetConcurrency,
291          int resultSetHoldability) throws SQLException JavaDoc
292    {
293
294       checkTransaction();
295       try
296       {
297          return new WrappedCallableStatement(this, mc.getConnection()
298                .prepareCall(sql, resultSetType, resultSetConcurrency, resultSetHoldability));
299       }
300       catch (Throwable JavaDoc t)
301       {
302          throw checkException(t);
303       }
304    }
305
306    public String JavaDoc nativeSQL(String JavaDoc sql) throws SQLException JavaDoc
307    {
308       checkTransaction();
309       try
310       {
311          return mc.getConnection().nativeSQL(sql);
312       }
313       catch (Throwable JavaDoc t)
314       {
315          throw checkException(t);
316       }
317    }
318
319    public void setAutoCommit(boolean autocommit) throws SQLException JavaDoc
320    {
321       checkStatus();
322       mc.setJdbcAutoCommit(autocommit);
323    }
324
325    public boolean getAutoCommit() throws SQLException JavaDoc
326    {
327       checkStatus();
328       return mc.isJdbcAutoCommit();
329    }
330
331    public void commit() throws SQLException JavaDoc
332    {
333       checkTransaction();
334       mc.jdbcCommit();
335    }
336
337    public void rollback() throws SQLException JavaDoc
338    {
339       checkTransaction();
340       mc.jdbcRollback();
341    }
342
343    public void rollback(Savepoint JavaDoc savepoint) throws SQLException JavaDoc
344    {
345       checkTransaction();
346       mc.jdbcRollback(savepoint);
347    }
348
349    public DatabaseMetaData JavaDoc getMetaData() throws SQLException JavaDoc
350    {
351       checkTransaction();
352       try
353       {
354          return mc.getConnection().getMetaData();
355       }
356       catch (Throwable JavaDoc t)
357       {
358          throw checkException(t);
359       }
360    }
361
362    public void setCatalog(String JavaDoc catalog) throws SQLException JavaDoc
363    {
364       checkTransaction();
365       try
366       {
367          mc.getConnection().setCatalog(catalog);
368       }
369       catch (Throwable JavaDoc t)
370       {
371          throw checkException(t);
372       }
373    }
374    public String JavaDoc getCatalog() throws SQLException JavaDoc
375    {
376       checkTransaction();
377       try
378       {
379          return mc.getConnection().getCatalog();
380       }
381       catch (Throwable JavaDoc t)
382       {
383          throw checkException(t);
384       }
385    }
386
387    public void setTransactionIsolation(int isolationLevel) throws SQLException JavaDoc
388    {
389       checkStatus();
390       mc.setJdbcTransactionIsolation(isolationLevel);
391    }
392
393    public int getTransactionIsolation() throws SQLException JavaDoc
394    {
395       checkStatus();
396       return mc.getJdbcTransactionIsolation();
397    }
398
399    public SQLWarning JavaDoc getWarnings() throws SQLException JavaDoc
400    {
401       checkTransaction();
402       try
403       {
404          return mc.getConnection().getWarnings();
405       }
406       catch (Throwable JavaDoc t)
407       {
408          throw checkException(t);
409       }
410    }
411
412    public void clearWarnings() throws SQLException JavaDoc
413    {
414       checkTransaction();
415       try
416       {
417          mc.getConnection().clearWarnings();
418       }
419       catch (Throwable JavaDoc t)
420       {
421          throw checkException(t);
422       }
423    }
424
425    public Map JavaDoc getTypeMap() throws SQLException JavaDoc
426    {
427       checkTransaction();
428       try
429       {
430          return mc.getConnection().getTypeMap();
431       }
432       catch (Throwable JavaDoc t)
433       {
434          throw checkException(t);
435       }
436    }
437
438    public void setTypeMap(Map JavaDoc typeMap) throws SQLException JavaDoc
439    {
440       checkTransaction();
441       try
442       {
443          mc.getConnection().setTypeMap(typeMap);
444       }
445       catch (Throwable JavaDoc t)
446       {
447          throw checkException(t);
448       }
449    }
450
451    public void setHoldability(int holdability) throws SQLException JavaDoc
452    {
453       checkTransaction();
454       try
455       {
456          mc.getConnection().setHoldability(holdability);
457       }
458       catch (Throwable JavaDoc t)
459       {
460          throw checkException(t);
461       }
462    }
463
464    public int getHoldability() throws SQLException JavaDoc
465    {
466       checkTransaction();
467       try
468       {
469          return mc.getConnection().getHoldability();
470       }
471       catch (Throwable JavaDoc t)
472       {
473          throw checkException(t);
474       }
475    }
476
477    public Savepoint JavaDoc setSavepoint() throws SQLException JavaDoc
478    {
479       checkTransaction();
480       try
481       {
482          return mc.getConnection().setSavepoint();
483       }
484       catch (Throwable JavaDoc t)
485       {
486          throw checkException(t);
487       }
488    }
489
490    public Savepoint JavaDoc setSavepoint(String JavaDoc name) throws SQLException JavaDoc
491    {
492       checkTransaction();
493       try
494       {
495          return mc.getConnection().setSavepoint(name);
496       }
497       catch (Throwable JavaDoc t)
498       {
499          throw checkException(t);
500       }
501    }
502
503    public void releaseSavepoint(Savepoint JavaDoc savepoint) throws SQLException JavaDoc
504    {
505       checkTransaction();
506       try
507       {
508          mc.getConnection().releaseSavepoint(savepoint);
509       }
510       catch (Throwable JavaDoc t)
511       {
512          throw checkException(t);
513       }
514    }
515
516    public Connection JavaDoc getUnderlyingConnection() throws SQLException JavaDoc
517    {
518       checkTransaction();
519       return mc.getConnection();
520    }
521
522    void checkTransaction() throws SQLException JavaDoc
523    {
524       checkStatus();
525       mc.checkTransaction();
526    }
527
528    /**
529     * The checkStatus method checks that the handle has not been closed and
530     * that it is associated with a managed connection.
531     *
532     * @exception SQLException if an error occurs
533     */

534    protected void checkStatus() throws SQLException JavaDoc
535    {
536       if (closed)
537          throw new SQLException JavaDoc("Connection handle has been closed and is unusable");
538       if (mc == null)
539          throw new SQLException JavaDoc("Connection handle is not currently associated with a ManagedConnection");
540    }
541
542    /**
543     * The base checkException method rethrows the supplied exception, informing
544     * the ManagedConnection of the error. Subclasses may override this to
545     * filter exceptions based on their severity.
546     *
547     * @param e a <code>SQLException</code> value
548     * @exception Exception if an error occurs
549     */

550    protected SQLException JavaDoc checkException(Throwable JavaDoc t) throws SQLException JavaDoc
551    {
552       Throwable JavaDoc result = null;
553
554       if (mc != null)
555          result = mc.connectionError(t);
556
557       if (result instanceof SQLException JavaDoc)
558       {
559          throw (SQLException JavaDoc) result;
560       }
561       else
562       {
563          throw new NestedSQLException("Error", result);
564       }
565       
566    }
567
568    int getTrackStatements()
569    {
570       return trackStatements;
571    }
572    
573    void registerStatement(WrappedStatement ws)
574    {
575       if (trackStatements == BaseWrapperManagedConnectionFactory.TRACK_STATEMENTS_FALSE_INT)
576          return;
577       
578       synchronized (this)
579       {
580          if (statements == null)
581             statements = new HashMap JavaDoc();
582          
583          if (trackStatements == BaseWrapperManagedConnectionFactory.TRACK_STATEMENTS_TRUE_INT)
584             statements.put(ws, new Throwable JavaDoc("STACKTRACE"));
585          else
586             statements.put(ws, null);
587       }
588    }
589
590    void unregisterStatement(WrappedStatement ws)
591    {
592       if (trackStatements == BaseWrapperManagedConnectionFactory.TRACK_STATEMENTS_FALSE_INT)
593          return;
594       synchronized (this)
595       {
596          if (statements != null)
597             statements.remove(ws);
598       }
599    }
600
601    void checkConfiguredQueryTimeout(WrappedStatement ws) throws SQLException JavaDoc
602    {
603       if (mc == null || dataSource == null)
604          return;
605
606       int timeout = 0;
607       
608       // Use the transaction timeout
609
if (mc.isTransactionQueryTimeout())
610          timeout = dataSource.getTimeLeftBeforeTransactionTimeout();
611       
612       // Look for a configured value
613
if (timeout <= 0)
614          timeout = mc.getQueryTimeout();
615       
616       if (timeout > 0)
617          ws.setQueryTimeout(timeout);
618    }
619    
620    Logger getLogger()
621    {
622       return log;
623    }
624 }
625
Popular Tags