KickJava   Java API By Example, From Geeks To Geeks.

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


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.Connection JavaDoc;
25 import java.sql.ResultSet JavaDoc;
26 import java.sql.SQLException JavaDoc;
27 import java.sql.SQLWarning JavaDoc;
28 import java.sql.Statement JavaDoc;
29 import java.util.HashMap JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import java.util.Map JavaDoc;
32
33 /**
34  * A wrapper for a statement.
35  *
36  * @todo remove the org.jboss.ejb.plugins.cmp.jdbc.WrappedStatement dependency
37  *
38  * @author <a HREF="mailto:d_jencks@users.sourceforge.net">David Jencks</a>
39  * @author <a HREF="mailto:adrian@jboss.com">Adrian Brock</a>
40  * @version $Revision: 54959 $
41  */

42 public class WrappedStatement implements Statement JavaDoc, StatementAccess,
43    org.jboss.ejb.plugins.cmp.jdbc.WrappedStatement
44 {
45    private final WrappedConnection lc;
46    private final Statement JavaDoc s;
47
48    /** The result sets */
49    private HashMap JavaDoc resultSets;
50
51    /** Whether we are closed */
52    private boolean closed = false;
53
54    /** The state lock */
55    private Object JavaDoc lock = new Object JavaDoc();
56
57    public WrappedStatement(final WrappedConnection lc, Statement JavaDoc s)
58    {
59       this.lc = lc;
60       this.s = s;
61       lc.registerStatement(this);
62    }
63
64    public void close() throws SQLException JavaDoc
65    {
66       synchronized (lock)
67       {
68          if (closed)
69            return;
70          
71          closed = true;
72       }
73       lc.unregisterStatement(this);
74       internalClose();
75    }
76
77    public boolean execute(String JavaDoc sql) throws SQLException JavaDoc
78    {
79       checkTransaction();
80       try
81       {
82          checkConfiguredQueryTimeout();
83          return s.execute(sql);
84       }
85       catch (Throwable JavaDoc t)
86       {
87          throw checkException(t);
88       }
89    }
90
91    public boolean execute(String JavaDoc sql, int autoGeneratedKeys) throws SQLException JavaDoc
92    {
93       checkTransaction();
94       try
95       {
96          checkConfiguredQueryTimeout();
97          return s.execute(sql, autoGeneratedKeys);
98       }
99       catch (Throwable JavaDoc t)
100       {
101          throw checkException(t);
102       }
103    }
104
105    public boolean execute(String JavaDoc sql, int[] columnIndexes) throws SQLException JavaDoc
106    {
107       checkTransaction();
108       try
109       {
110          checkConfiguredQueryTimeout();
111          return s.execute(sql, columnIndexes);
112       }
113       catch (Throwable JavaDoc t)
114       {
115          throw checkException(t);
116       }
117    }
118
119    public boolean execute(String JavaDoc sql, String JavaDoc[]columnNames ) throws SQLException JavaDoc
120    {
121       checkTransaction();
122       try
123       {
124          checkConfiguredQueryTimeout();
125          return s.execute(sql, columnNames);
126       }
127       catch (Throwable JavaDoc t)
128       {
129          throw checkException(t);
130       }
131    }
132
133    public Connection JavaDoc getConnection() throws SQLException JavaDoc
134    {
135       return lc;
136    }
137
138    public SQLWarning JavaDoc getWarnings() throws SQLException JavaDoc
139    {
140       checkState();
141       try
142       {
143          return s.getWarnings();
144       }
145       catch (Throwable JavaDoc t)
146       {
147          throw checkException(t);
148       }
149    }
150
151    public void clearWarnings() throws SQLException JavaDoc
152    {
153       checkState();
154       try
155       {
156          s.clearWarnings();
157       }
158       catch (Throwable JavaDoc t)
159       {
160          throw checkException(t);
161       }
162    }
163
164    public ResultSet JavaDoc executeQuery(String JavaDoc sql) throws SQLException JavaDoc
165    {
166       checkTransaction();
167       try
168       {
169          checkConfiguredQueryTimeout();
170          ResultSet JavaDoc result = s.executeQuery(sql);
171          return registerResultSet(result);
172       }
173       catch (Throwable JavaDoc t)
174       {
175          throw checkException(t);
176       }
177    }
178
179    public int executeUpdate(String JavaDoc sql) throws SQLException JavaDoc
180    {
181       checkTransaction();
182       try
183       {
184          checkConfiguredQueryTimeout();
185          return s.executeUpdate(sql);
186       }
187       catch (Throwable JavaDoc t)
188       {
189          throw checkException(t);
190       }
191    }
192
193    public int executeUpdate(String JavaDoc sql, int autoGeneratedKeys) throws SQLException JavaDoc
194    {
195       checkTransaction();
196       try
197       {
198          checkConfiguredQueryTimeout();
199          return s.executeUpdate(sql, autoGeneratedKeys);
200       }
201       catch (Throwable JavaDoc t)
202       {
203          throw checkException(t);
204       }
205    }
206
207    public int executeUpdate(String JavaDoc sql, int[] columnIndexes) throws SQLException JavaDoc
208    {
209       checkTransaction();
210       try
211       {
212          checkConfiguredQueryTimeout();
213          return s.executeUpdate(sql, columnIndexes);
214       }
215       catch (Throwable JavaDoc t)
216       {
217          throw checkException(t);
218       }
219    }
220
221    public int executeUpdate(String JavaDoc sql, String JavaDoc[] columnNames) throws SQLException JavaDoc
222    {
223       checkTransaction();
224       try
225       {
226          checkConfiguredQueryTimeout();
227          return s.executeUpdate(sql, columnNames);
228       }
229       catch (Throwable JavaDoc t)
230       {
231          throw checkException(t);
232       }
233    }
234
235    public int getMaxFieldSize() throws SQLException JavaDoc
236    {
237       checkState();
238       try
239       {
240          return s.getMaxFieldSize();
241       }
242       catch (Throwable JavaDoc t)
243       {
244          throw checkException(t);
245       }
246    }
247
248    public void setMaxFieldSize(int max) throws SQLException JavaDoc
249    {
250       checkState();
251       try
252       {
253          s.setMaxFieldSize(max);
254       }
255       catch (Throwable JavaDoc t)
256       {
257          throw checkException(t);
258       }
259    }
260
261    public int getMaxRows() throws SQLException JavaDoc
262    {
263       checkState();
264       try
265       {
266          return s.getMaxRows();
267       }
268       catch (Throwable JavaDoc t)
269       {
270          throw checkException(t);
271       }
272    }
273
274    public void setMaxRows(int max) throws SQLException JavaDoc
275    {
276       checkState();
277       try
278       {
279          s.setMaxRows(max);
280       }
281       catch (Throwable JavaDoc t)
282       {
283          throw checkException(t);
284       }
285    }
286
287    public void setEscapeProcessing(boolean enable) throws SQLException JavaDoc
288    {
289       checkState();
290       try
291       {
292          s.setEscapeProcessing(enable);
293       }
294       catch (Throwable JavaDoc t)
295       {
296          throw checkException(t);
297       }
298    }
299
300    public int getQueryTimeout() throws SQLException JavaDoc
301    {
302       checkState();
303       try
304       {
305          return s.getQueryTimeout();
306       }
307       catch (Throwable JavaDoc t)
308       {
309          throw checkException(t);
310       }
311    }
312
313    public void setQueryTimeout(int timeout) throws SQLException JavaDoc
314    {
315       checkState();
316       try
317       {
318          s.setQueryTimeout(timeout);
319       }
320       catch (Throwable JavaDoc t)
321       {
322          throw checkException(t);
323       }
324    }
325
326    public void cancel() throws SQLException JavaDoc
327    {
328       checkState();
329       try
330       {
331          s.cancel();
332       }
333       catch (Throwable JavaDoc t)
334       {
335          throw checkException(t);
336       }
337    }
338
339    public void setCursorName(String JavaDoc name) throws SQLException JavaDoc
340    {
341       checkState();
342       try
343       {
344          s.setCursorName(name);
345       }
346       catch (Throwable JavaDoc t)
347       {
348          throw checkException(t);
349       }
350    }
351
352    public ResultSet JavaDoc getResultSet() throws SQLException JavaDoc
353    {
354       checkState();
355       try
356       {
357          ResultSet JavaDoc result = s.getResultSet();
358          if (result == null)
359             return null;
360          else
361             return registerResultSet(result);
362       }
363       catch (Throwable JavaDoc t)
364       {
365          throw checkException(t);
366       }
367    }
368
369    public int getUpdateCount() throws SQLException JavaDoc
370    {
371       checkState();
372       try
373       {
374          return s.getUpdateCount();
375       }
376       catch (Throwable JavaDoc t)
377       {
378          throw checkException(t);
379       }
380    }
381
382    public boolean getMoreResults() throws SQLException JavaDoc
383    {
384       checkState();
385       try
386       {
387          return s.getMoreResults();
388       }
389       catch (Throwable JavaDoc t)
390       {
391          throw checkException(t);
392       }
393    }
394
395    public boolean getMoreResults(int current) throws SQLException JavaDoc
396    {
397       checkState();
398       try
399       {
400          return s.getMoreResults(current);
401       }
402       catch (Throwable JavaDoc t)
403       {
404          throw checkException(t);
405       }
406    }
407
408    public void setFetchDirection(int direction) throws SQLException JavaDoc
409    {
410       checkState();
411       try
412       {
413          s.setFetchDirection(direction);
414       }
415       catch (Throwable JavaDoc t)
416       {
417          throw checkException(t);
418       }
419    }
420
421    public int getFetchDirection() throws SQLException JavaDoc
422    {
423       checkState();
424       try
425       {
426          return s.getFetchDirection();
427       }
428       catch (Throwable JavaDoc t)
429       {
430          throw checkException(t);
431       }
432    }
433
434    public void setFetchSize(int rows) throws SQLException JavaDoc
435    {
436       checkState();
437       try
438       {
439          s.setFetchSize(rows);
440       }
441       catch (Throwable JavaDoc t)
442       {
443          throw checkException(t);
444       }
445    }
446
447    public int getFetchSize() throws SQLException JavaDoc
448    {
449       checkState();
450       try
451       {
452          return s.getFetchSize();
453       }
454       catch (Throwable JavaDoc t)
455       {
456          throw checkException(t);
457       }
458    }
459
460    public int getResultSetConcurrency() throws SQLException JavaDoc
461    {
462       checkState();
463       try
464       {
465          return s.getResultSetConcurrency();
466       }
467       catch (Throwable JavaDoc t)
468       {
469          throw checkException(t);
470       }
471    }
472
473    public int getResultSetType() throws SQLException JavaDoc
474    {
475       checkState();
476       try
477       {
478          return s.getResultSetType();
479       }
480       catch (Throwable JavaDoc t)
481       {
482          throw checkException(t);
483       }
484    }
485
486    public void addBatch(String JavaDoc sql) throws SQLException JavaDoc
487    {
488       checkState();
489       try
490       {
491          s.addBatch(sql);
492       }
493       catch (Throwable JavaDoc t)
494       {
495          throw checkException(t);
496       }
497    }
498
499    public void clearBatch() throws SQLException JavaDoc
500    {
501       checkState();
502       try
503       {
504          s.clearBatch();
505       }
506       catch (Throwable JavaDoc t)
507       {
508          throw checkException(t);
509       }
510    }
511
512    public int[] executeBatch() throws SQLException JavaDoc
513    {
514       checkState();
515       try
516       {
517          checkConfiguredQueryTimeout();
518          return s.executeBatch();
519       }
520       catch (Throwable JavaDoc t)
521       {
522          throw checkException(t);
523       }
524    }
525
526    public ResultSet JavaDoc getGeneratedKeys() throws SQLException JavaDoc
527    {
528       checkState();
529       try
530       {
531          ResultSet JavaDoc resultSet = s.getGeneratedKeys();
532          return registerResultSet(resultSet);
533       }
534       catch (Throwable JavaDoc t)
535       {
536          throw checkException(t);
537       }
538    }
539
540    public int getResultSetHoldability() throws SQLException JavaDoc
541    {
542       checkState();
543       try
544       {
545          return s.getResultSetHoldability();
546       }
547       catch (Throwable JavaDoc t)
548       {
549          throw checkException(t);
550       }
551    }
552
553    public Statement JavaDoc getUnderlyingStatement() throws SQLException JavaDoc
554    {
555       checkState();
556       return s;
557    }
558
559    protected SQLException JavaDoc checkException(Throwable JavaDoc t)
560       throws SQLException JavaDoc
561    {
562       throw lc.checkException(t);
563    }
564
565    protected void checkTransaction()
566       throws SQLException JavaDoc
567    {
568       checkState();
569       lc.checkTransaction();
570    }
571
572    protected void checkConfiguredQueryTimeout() throws SQLException JavaDoc
573    {
574       lc.checkConfiguredQueryTimeout(this);
575    }
576    
577    protected void internalClose() throws SQLException JavaDoc
578    {
579       synchronized (lock)
580       {
581          closed = true;
582       }
583       try
584       {
585          closeResultSets();
586       }
587       finally
588       {
589          s.close();
590       }
591    }
592
593    void checkState() throws SQLException JavaDoc
594    {
595       synchronized (lock)
596       {
597          if (closed)
598             throw new SQLException JavaDoc("The statement is closed.");
599       }
600    }
601
602    protected ResultSet JavaDoc registerResultSet(ResultSet JavaDoc resultSet)
603    {
604       if (resultSet != null)
605          resultSet = new WrappedResultSet(this, resultSet);
606       
607       if (lc.getTrackStatements() == BaseWrapperManagedConnectionFactory.TRACK_STATEMENTS_FALSE_INT)
608          return resultSet;
609
610       synchronized (this)
611       {
612          if (resultSets == null)
613             resultSets = new HashMap JavaDoc();
614          
615          if (lc.getTrackStatements() == BaseWrapperManagedConnectionFactory.TRACK_STATEMENTS_TRUE_INT)
616             resultSets.put(resultSet, new Throwable JavaDoc("STACKTRACE"));
617          else
618             resultSets.put(resultSet, null);
619       }
620       return resultSet;
621    }
622
623    protected void unregisterResultSet(WrappedResultSet resultSet)
624    {
625       if (lc.getTrackStatements() == BaseWrapperManagedConnectionFactory.TRACK_STATEMENTS_FALSE_INT)
626          return;
627
628       synchronized (this)
629       {
630          if (resultSets != null)
631             resultSets.remove(resultSet);
632       }
633    }
634
635    protected void closeResultSets()
636    {
637       if (lc.getTrackStatements() == BaseWrapperManagedConnectionFactory.TRACK_STATEMENTS_FALSE_INT)
638          return;
639
640       synchronized (this)
641       {
642          if (resultSets == null)
643             return;
644          for (Iterator JavaDoc i = resultSets.entrySet().iterator(); i.hasNext();)
645          {
646             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) i.next();
647             WrappedResultSet resultSet = (WrappedResultSet) entry.getKey();
648             if (lc.getTrackStatements() == BaseWrapperManagedConnectionFactory.TRACK_STATEMENTS_TRUE_INT)
649             {
650                Throwable JavaDoc stackTrace = (Throwable JavaDoc) entry.getValue();
651                lc.getLogger().warn("Closing a result set you left open! Please close it yourself.", stackTrace);
652             }
653             try
654             {
655                resultSet.internalClose();
656             }
657             catch (Throwable JavaDoc t)
658             {
659                lc.getLogger().warn("Error closing a result set you left open! Please close it yourself.", t);
660             }
661          }
662          resultSets.clear();
663       }
664    }
665 }
666
Popular Tags