KickJava   Java API By Example, From Geeks To Geeks.

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


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.PreparedStatement JavaDoc;
25 import java.sql.ResultSet JavaDoc;
26 import java.sql.SQLException JavaDoc;
27 import java.sql.Ref JavaDoc;
28 import java.sql.Blob JavaDoc;
29 import java.sql.Clob JavaDoc;
30 import java.sql.Array JavaDoc;
31 import java.sql.ResultSetMetaData JavaDoc;
32 import java.sql.ParameterMetaData JavaDoc;
33 import java.sql.SQLWarning JavaDoc;
34 import java.sql.Connection JavaDoc;
35 import java.math.BigDecimal JavaDoc;
36 import java.util.Calendar JavaDoc;
37
38 import EDU.oswego.cs.dl.util.concurrent.SynchronizedBoolean;
39 import EDU.oswego.cs.dl.util.concurrent.SynchronizedInt;
40
41 /**
42  * Wrapper class for cached PreparedStatements. Keeps a refcount. When this refcount reaches 0,
43  * it will close ps.
44  *
45  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
46  * @author <a HREF="mailto:adrian@jboss.com">Adrian Brock</a>
47  * @version $Revision: 38344 $
48  */

49 public class CachedPreparedStatement implements PreparedStatement JavaDoc
50 {
51    private PreparedStatement JavaDoc ps;
52    private SynchronizedBoolean cached = new SynchronizedBoolean(true);
53    private SynchronizedInt inUse = new SynchronizedInt(1);
54
55    private int defaultMaxFieldSize;
56    private int defaultMaxRows;
57    private int defaultQueryTimeout;
58    private int defaultFetchDirection;
59    private int defaultFetchSize;
60    private int currentMaxFieldSize;
61    private int currentMaxRows;
62    private int currentQueryTimeout;
63    private int currentFetchDirection;
64    private int currentFetchSize;
65
66    public CachedPreparedStatement(PreparedStatement JavaDoc ps) throws SQLException JavaDoc
67    {
68       this.ps = ps;
69
70       // Remember the defaults
71
defaultMaxFieldSize = ps.getMaxFieldSize();
72       defaultMaxRows = ps.getMaxRows();
73       defaultQueryTimeout = ps.getQueryTimeout();
74       defaultFetchDirection = ps.getFetchDirection();
75       defaultFetchSize = ps.getFetchSize();
76       currentMaxFieldSize = defaultMaxFieldSize;
77       currentMaxRows = defaultMaxRows;
78       currentQueryTimeout = defaultQueryTimeout;
79       currentFetchDirection = defaultFetchDirection;
80       currentFetchSize = defaultFetchSize;
81    }
82
83    public PreparedStatement JavaDoc getUnderlyingPreparedStatement()
84    {
85       return ps;
86    }
87
88    public ResultSet JavaDoc executeQuery() throws SQLException JavaDoc
89    {
90       return ps.executeQuery();
91    }
92
93    public int executeUpdate() throws SQLException JavaDoc
94    {
95       return ps.executeUpdate();
96    }
97
98    public void setNull(int parameterIndex, int sqlType) throws SQLException JavaDoc
99    {
100       ps.setNull(parameterIndex, sqlType);
101    }
102
103    public void setBoolean(int parameterIndex, boolean x) throws SQLException JavaDoc
104    {
105       ps.setBoolean(parameterIndex, x);
106    }
107
108    public void setByte(int parameterIndex, byte x) throws SQLException JavaDoc
109    {
110       ps.setByte(parameterIndex, x);
111    }
112
113    public void setShort(int parameterIndex, short x) throws SQLException JavaDoc
114    {
115       ps.setShort(parameterIndex, x);
116    }
117
118    public void setInt(int parameterIndex, int x) throws SQLException JavaDoc
119    {
120       ps.setInt(parameterIndex, x);
121    }
122
123    public void setLong(int parameterIndex, long x) throws SQLException JavaDoc
124    {
125       ps.setLong(parameterIndex, x);
126    }
127
128    public void setFloat(int parameterIndex, float x) throws SQLException JavaDoc
129    {
130       ps.setFloat(parameterIndex, x);
131    }
132
133    public void setDouble(int parameterIndex, double x) throws SQLException JavaDoc
134    {
135       ps.setDouble(parameterIndex, x);
136    }
137
138    public void setBigDecimal(int parameterIndex, BigDecimal JavaDoc x) throws SQLException JavaDoc
139    {
140       ps.setBigDecimal(parameterIndex, x);
141    }
142
143    public void setString(int parameterIndex, String JavaDoc x) throws SQLException JavaDoc
144    {
145       ps.setString(parameterIndex, x);
146    }
147
148    public void setBytes(int parameterIndex, byte x[]) throws SQLException JavaDoc
149    {
150       ps.setBytes(parameterIndex, x);
151    }
152
153    public void setDate(int parameterIndex, java.sql.Date JavaDoc x)
154            throws SQLException JavaDoc
155    {
156       ps.setDate(parameterIndex, x);
157    }
158
159    public void setTime(int parameterIndex, java.sql.Time JavaDoc x)
160            throws SQLException JavaDoc
161    {
162       ps.setTime(parameterIndex, x);
163    }
164
165    public void setTimestamp(int parameterIndex, java.sql.Timestamp JavaDoc x)
166            throws SQLException JavaDoc
167    {
168       ps.setTimestamp(parameterIndex, x);
169    }
170
171    public void setAsciiStream(int parameterIndex, java.io.InputStream JavaDoc x, int length)
172            throws SQLException JavaDoc
173    {
174       ps.setAsciiStream(parameterIndex, x, length);
175    }
176
177    /**
178     * @deprecated
179     */

180    public void setUnicodeStream(int parameterIndex, java.io.InputStream JavaDoc x,
181                                 int length) throws SQLException JavaDoc
182    {
183       ps.setUnicodeStream(parameterIndex, x, length);
184    }
185
186    public void setBinaryStream(int parameterIndex, java.io.InputStream JavaDoc x,
187                                int length) throws SQLException JavaDoc
188    {
189       ps.setBinaryStream(parameterIndex, x, length);
190    }
191
192    public void clearParameters() throws SQLException JavaDoc
193    {
194       ps.clearParameters();
195    }
196
197    public void setObject(int parameterIndex, Object JavaDoc x, int targetSqlType, int scale)
198            throws SQLException JavaDoc
199    {
200       ps.setObject(parameterIndex, x, targetSqlType, scale);
201    }
202
203    public void setObject(int parameterIndex, Object JavaDoc x, int targetSqlType)
204            throws SQLException JavaDoc
205    {
206       ps.setObject(parameterIndex, x, targetSqlType);
207    }
208
209    public void setObject(int parameterIndex, Object JavaDoc x) throws SQLException JavaDoc
210    {
211       ps.setObject(parameterIndex, x);
212    }
213
214    public boolean execute() throws SQLException JavaDoc
215    {
216       return ps.execute();
217    }
218
219    public void addBatch() throws SQLException JavaDoc
220    {
221       ps.addBatch();
222    }
223
224    public void setCharacterStream(int parameterIndex,
225                                   java.io.Reader JavaDoc reader,
226                                   int length) throws SQLException JavaDoc
227    {
228       ps.setCharacterStream(parameterIndex, reader, length);
229    }
230
231    public void setRef(int i, Ref JavaDoc x) throws SQLException JavaDoc
232    {
233       ps.setRef(i, x);
234    }
235
236    public void setBlob(int i, Blob JavaDoc x) throws SQLException JavaDoc
237    {
238       ps.setBlob(i, x);
239    }
240
241    public void setClob(int i, Clob JavaDoc x) throws SQLException JavaDoc
242    {
243       ps.setClob(i, x);
244    }
245
246    public void setArray(int i, Array JavaDoc x) throws SQLException JavaDoc
247    {
248       ps.setArray(i, x);
249    }
250
251    public ResultSetMetaData JavaDoc getMetaData() throws SQLException JavaDoc
252    {
253       return ps.getMetaData();
254    }
255
256    public void setDate(int parameterIndex, java.sql.Date JavaDoc x, Calendar JavaDoc cal)
257            throws SQLException JavaDoc
258    {
259       ps.setDate(parameterIndex, x, cal);
260    }
261
262    public void setTime(int parameterIndex, java.sql.Time JavaDoc x, Calendar JavaDoc cal)
263            throws SQLException JavaDoc
264    {
265       ps.setTime(parameterIndex, x, cal);
266    }
267
268    public void setTimestamp(int parameterIndex, java.sql.Timestamp JavaDoc x, Calendar JavaDoc cal)
269            throws SQLException JavaDoc
270    {
271       ps.setTimestamp(parameterIndex, x, cal);
272    }
273
274    public void setNull(int paramIndex, int sqlType, String JavaDoc typeName)
275            throws SQLException JavaDoc
276    {
277       ps.setNull(paramIndex, sqlType, typeName);
278    }
279
280    public void setURL(int parameterIndex, java.net.URL JavaDoc x) throws SQLException JavaDoc
281    {
282       ps.setURL(parameterIndex, x);
283    }
284
285    public ParameterMetaData JavaDoc getParameterMetaData() throws SQLException JavaDoc
286    {
287       return ps.getParameterMetaData();
288    }
289
290    public ResultSet JavaDoc executeQuery(String JavaDoc sql) throws SQLException JavaDoc
291    {
292       return ps.executeQuery(sql);
293    }
294
295    public int executeUpdate(String JavaDoc sql) throws SQLException JavaDoc
296    {
297       return ps.executeUpdate(sql);
298    }
299
300    public boolean isInUse()
301    {
302       return inUse.get() > 0;
303    }
304    
305    public void inUse()
306    {
307       inUse.increment();
308    }
309
310    public void agedOut() throws SQLException JavaDoc
311    {
312       cached.set(false);
313       if (inUse.get() == 0)
314          ps.close();
315    }
316
317    public void close() throws SQLException JavaDoc
318    {
319       inUse.decrement();
320       if (inUse.get() == 0)
321       {
322          if (cached.get() == false)
323             ps.close();
324          else
325          {
326             // Reset the defaults
327
if (defaultMaxFieldSize != currentMaxFieldSize)
328             {
329                ps.setMaxFieldSize(defaultMaxFieldSize);
330                currentMaxFieldSize = defaultMaxFieldSize;
331             }
332             if (defaultMaxRows != currentMaxRows)
333             {
334                ps.setMaxRows(defaultMaxRows);
335                currentMaxRows = defaultMaxRows;
336             }
337             if (defaultQueryTimeout != currentQueryTimeout)
338             {
339                ps.setQueryTimeout(defaultQueryTimeout);
340                currentQueryTimeout = defaultQueryTimeout;
341             }
342             if (defaultFetchDirection != currentFetchDirection)
343             {
344                ps.setFetchDirection(defaultFetchDirection);
345                currentFetchDirection = defaultFetchDirection;
346             }
347             if (defaultFetchSize != currentFetchSize)
348             {
349                ps.setFetchSize(defaultFetchSize);
350                currentFetchSize = defaultFetchSize;
351             }
352          }
353       }
354    }
355
356    public int getMaxFieldSize() throws SQLException JavaDoc
357    {
358       return ps.getMaxFieldSize();
359    }
360
361    public void setMaxFieldSize(int max) throws SQLException JavaDoc
362    {
363       ps.setMaxFieldSize(max);
364       currentMaxFieldSize = max;
365    }
366
367    public int getMaxRows() throws SQLException JavaDoc
368    {
369       return ps.getMaxRows();
370    }
371
372    public void setMaxRows(int max) throws SQLException JavaDoc
373    {
374       ps.setMaxRows(max);
375       currentMaxRows = max;
376    }
377
378    public void setEscapeProcessing(boolean enable) throws SQLException JavaDoc
379    {
380       ps.setEscapeProcessing(enable);
381    }
382
383    public int getQueryTimeout() throws SQLException JavaDoc
384    {
385       return ps.getQueryTimeout();
386    }
387
388    public void setQueryTimeout(int seconds) throws SQLException JavaDoc
389    {
390       ps.setQueryTimeout(seconds);
391       currentQueryTimeout = seconds;
392    }
393
394    public void cancel() throws SQLException JavaDoc
395    {
396       ps.cancel();
397    }
398
399    public SQLWarning JavaDoc getWarnings() throws SQLException JavaDoc
400    {
401       return ps.getWarnings();
402    }
403
404    public void clearWarnings() throws SQLException JavaDoc
405    {
406       ps.clearWarnings();
407    }
408
409    public void setCursorName(String JavaDoc name) throws SQLException JavaDoc
410    {
411       ps.setCursorName(name);
412    }
413
414    public boolean execute(String JavaDoc sql) throws SQLException JavaDoc
415    {
416       return ps.execute(sql);
417    }
418
419    public ResultSet JavaDoc getResultSet() throws SQLException JavaDoc
420    {
421       return ps.getResultSet();
422    }
423
424    public int getUpdateCount() throws SQLException JavaDoc
425    {
426       return ps.getUpdateCount();
427    }
428
429    public boolean getMoreResults() throws SQLException JavaDoc
430    {
431       return ps.getMoreResults();
432    }
433
434    public void setFetchDirection(int direction) throws SQLException JavaDoc
435    {
436       ps.setFetchDirection(direction);
437       currentFetchDirection = direction;
438    }
439
440    public int getFetchDirection() throws SQLException JavaDoc
441    {
442       return ps.getFetchDirection();
443    }
444
445    public void setFetchSize(int rows) throws SQLException JavaDoc
446    {
447       ps.setFetchSize(rows);
448       currentFetchSize = rows;
449    }
450
451    public int getFetchSize() throws SQLException JavaDoc
452    {
453       return ps.getFetchSize();
454    }
455
456    public int getResultSetConcurrency() throws SQLException JavaDoc
457    {
458       return ps.getResultSetConcurrency();
459    }
460
461    public int getResultSetType() throws SQLException JavaDoc
462    {
463       return ps.getResultSetType();
464    }
465
466    public void addBatch(String JavaDoc sql) throws SQLException JavaDoc
467    {
468       ps.addBatch(sql);
469    }
470
471    public void clearBatch() throws SQLException JavaDoc
472    {
473       ps.clearBatch();
474    }
475
476    public int[] executeBatch() throws SQLException JavaDoc
477    {
478       return ps.executeBatch();
479    }
480
481    public Connection JavaDoc getConnection() throws SQLException JavaDoc
482    {
483       return ps.getConnection();
484    }
485
486    public boolean getMoreResults(int current) throws SQLException JavaDoc
487    {
488       return ps.getMoreResults(current);
489    }
490
491    public ResultSet JavaDoc getGeneratedKeys() throws SQLException JavaDoc
492    {
493       return ps.getGeneratedKeys();
494    }
495
496    public int executeUpdate(String JavaDoc sql, int autoGeneratedKeys) throws SQLException JavaDoc
497    {
498       return ps.executeUpdate(sql, autoGeneratedKeys);
499    }
500
501    public int executeUpdate(String JavaDoc sql, int columnIndexes[]) throws SQLException JavaDoc
502    {
503       return ps.executeUpdate(sql, columnIndexes);
504    }
505
506    public int executeUpdate(String JavaDoc sql, String JavaDoc columnNames[]) throws SQLException JavaDoc
507    {
508       return ps.executeUpdate(sql, columnNames);
509    }
510
511    public boolean execute(String JavaDoc sql, int autoGeneratedKeys) throws SQLException JavaDoc
512    {
513       return ps.execute(sql, autoGeneratedKeys);
514    }
515
516    public boolean execute(String JavaDoc sql, int columnIndexes[]) throws SQLException JavaDoc
517    {
518       return ps.execute(sql, columnIndexes);
519    }
520
521    public boolean execute(String JavaDoc sql, String JavaDoc columnNames[]) throws SQLException JavaDoc
522    {
523       return ps.execute(sql, columnNames);
524    }
525
526    public int getResultSetHoldability() throws SQLException JavaDoc
527    {
528       return ps.getResultSetHoldability();
529    }
530 }
531
Popular Tags