001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.resource.adapter.jdbc;
023:
024: import java.sql.PreparedStatement;
025: import java.sql.ResultSet;
026: import java.sql.SQLException;
027: import java.sql.Ref;
028: import java.sql.Blob;
029: import java.sql.Clob;
030: import java.sql.Array;
031: import java.sql.ResultSetMetaData;
032: import java.sql.ParameterMetaData;
033: import java.sql.SQLWarning;
034: import java.sql.Connection;
035: import java.math.BigDecimal;
036: import java.util.Calendar;
037:
038: import EDU.oswego.cs.dl.util.concurrent.SynchronizedBoolean;
039: import EDU.oswego.cs.dl.util.concurrent.SynchronizedInt;
040:
041: /**
042: * Wrapper class for cached PreparedStatements. Keeps a refcount. When this refcount reaches 0,
043: * it will close ps.
044: *
045: * @author <a href="mailto:bill@jboss.org">Bill Burke</a>
046: * @author <a href="mailto:adrian@jboss.com">Adrian Brock</a>
047: * @version $Revision: 57189 $
048: */
049: public class CachedPreparedStatement implements PreparedStatement {
050: private PreparedStatement ps;
051: private SynchronizedBoolean cached = new SynchronizedBoolean(true);
052: private SynchronizedInt inUse = new SynchronizedInt(1);
053:
054: private int defaultMaxFieldSize;
055: private int defaultMaxRows;
056: private int defaultQueryTimeout;
057: private int defaultFetchDirection;
058: private int defaultFetchSize;
059: private int currentMaxFieldSize;
060: private int currentMaxRows;
061: private int currentQueryTimeout;
062: private int currentFetchDirection;
063: private int currentFetchSize;
064:
065: public CachedPreparedStatement(PreparedStatement ps)
066: throws SQLException {
067: this .ps = ps;
068:
069: // Remember the defaults
070: defaultMaxFieldSize = ps.getMaxFieldSize();
071: defaultMaxRows = ps.getMaxRows();
072: defaultQueryTimeout = ps.getQueryTimeout();
073: defaultFetchDirection = ps.getFetchDirection();
074: defaultFetchSize = ps.getFetchSize();
075: currentMaxFieldSize = defaultMaxFieldSize;
076: currentMaxRows = defaultMaxRows;
077: currentQueryTimeout = defaultQueryTimeout;
078: currentFetchDirection = defaultFetchDirection;
079: currentFetchSize = defaultFetchSize;
080: }
081:
082: public PreparedStatement getUnderlyingPreparedStatement() {
083: return ps;
084: }
085:
086: public ResultSet executeQuery() throws SQLException {
087: return ps.executeQuery();
088: }
089:
090: public int executeUpdate() throws SQLException {
091: return ps.executeUpdate();
092: }
093:
094: public void setNull(int parameterIndex, int sqlType)
095: throws SQLException {
096: ps.setNull(parameterIndex, sqlType);
097: }
098:
099: public void setBoolean(int parameterIndex, boolean x)
100: throws SQLException {
101: ps.setBoolean(parameterIndex, x);
102: }
103:
104: public void setByte(int parameterIndex, byte x) throws SQLException {
105: ps.setByte(parameterIndex, x);
106: }
107:
108: public void setShort(int parameterIndex, short x)
109: throws SQLException {
110: ps.setShort(parameterIndex, x);
111: }
112:
113: public void setInt(int parameterIndex, int x) throws SQLException {
114: ps.setInt(parameterIndex, x);
115: }
116:
117: public void setLong(int parameterIndex, long x) throws SQLException {
118: ps.setLong(parameterIndex, x);
119: }
120:
121: public void setFloat(int parameterIndex, float x)
122: throws SQLException {
123: ps.setFloat(parameterIndex, x);
124: }
125:
126: public void setDouble(int parameterIndex, double x)
127: throws SQLException {
128: ps.setDouble(parameterIndex, x);
129: }
130:
131: public void setBigDecimal(int parameterIndex, BigDecimal x)
132: throws SQLException {
133: ps.setBigDecimal(parameterIndex, x);
134: }
135:
136: public void setString(int parameterIndex, String x)
137: throws SQLException {
138: ps.setString(parameterIndex, x);
139: }
140:
141: public void setBytes(int parameterIndex, byte x[])
142: throws SQLException {
143: ps.setBytes(parameterIndex, x);
144: }
145:
146: public void setDate(int parameterIndex, java.sql.Date x)
147: throws SQLException {
148: ps.setDate(parameterIndex, x);
149: }
150:
151: public void setTime(int parameterIndex, java.sql.Time x)
152: throws SQLException {
153: ps.setTime(parameterIndex, x);
154: }
155:
156: public void setTimestamp(int parameterIndex, java.sql.Timestamp x)
157: throws SQLException {
158: ps.setTimestamp(parameterIndex, x);
159: }
160:
161: public void setAsciiStream(int parameterIndex,
162: java.io.InputStream x, int length) throws SQLException {
163: ps.setAsciiStream(parameterIndex, x, length);
164: }
165:
166: /**
167: * @deprecated
168: */
169: public void setUnicodeStream(int parameterIndex,
170: java.io.InputStream x, int length) throws SQLException {
171: ps.setUnicodeStream(parameterIndex, x, length);
172: }
173:
174: public void setBinaryStream(int parameterIndex,
175: java.io.InputStream x, int length) throws SQLException {
176: ps.setBinaryStream(parameterIndex, x, length);
177: }
178:
179: public void clearParameters() throws SQLException {
180: ps.clearParameters();
181: }
182:
183: public void setObject(int parameterIndex, Object x,
184: int targetSqlType, int scale) throws SQLException {
185: ps.setObject(parameterIndex, x, targetSqlType, scale);
186: }
187:
188: public void setObject(int parameterIndex, Object x,
189: int targetSqlType) throws SQLException {
190: ps.setObject(parameterIndex, x, targetSqlType);
191: }
192:
193: public void setObject(int parameterIndex, Object x)
194: throws SQLException {
195: ps.setObject(parameterIndex, x);
196: }
197:
198: public boolean execute() throws SQLException {
199: return ps.execute();
200: }
201:
202: public void addBatch() throws SQLException {
203: ps.addBatch();
204: }
205:
206: public void setCharacterStream(int parameterIndex,
207: java.io.Reader reader, int length) throws SQLException {
208: ps.setCharacterStream(parameterIndex, reader, length);
209: }
210:
211: public void setRef(int i, Ref x) throws SQLException {
212: ps.setRef(i, x);
213: }
214:
215: public void setBlob(int i, Blob x) throws SQLException {
216: ps.setBlob(i, x);
217: }
218:
219: public void setClob(int i, Clob x) throws SQLException {
220: ps.setClob(i, x);
221: }
222:
223: public void setArray(int i, Array x) throws SQLException {
224: ps.setArray(i, x);
225: }
226:
227: public ResultSetMetaData getMetaData() throws SQLException {
228: return ps.getMetaData();
229: }
230:
231: public void setDate(int parameterIndex, java.sql.Date x,
232: Calendar cal) throws SQLException {
233: ps.setDate(parameterIndex, x, cal);
234: }
235:
236: public void setTime(int parameterIndex, java.sql.Time x,
237: Calendar cal) throws SQLException {
238: ps.setTime(parameterIndex, x, cal);
239: }
240:
241: public void setTimestamp(int parameterIndex, java.sql.Timestamp x,
242: Calendar cal) throws SQLException {
243: ps.setTimestamp(parameterIndex, x, cal);
244: }
245:
246: public void setNull(int paramIndex, int sqlType, String typeName)
247: throws SQLException {
248: ps.setNull(paramIndex, sqlType, typeName);
249: }
250:
251: public void setURL(int parameterIndex, java.net.URL x)
252: throws SQLException {
253: ps.setURL(parameterIndex, x);
254: }
255:
256: public ParameterMetaData getParameterMetaData() throws SQLException {
257: return ps.getParameterMetaData();
258: }
259:
260: public ResultSet executeQuery(String sql) throws SQLException {
261: return ps.executeQuery(sql);
262: }
263:
264: public int executeUpdate(String sql) throws SQLException {
265: return ps.executeUpdate(sql);
266: }
267:
268: public boolean isInUse() {
269: return inUse.get() > 0;
270: }
271:
272: public void inUse() {
273: inUse.increment();
274: }
275:
276: public void agedOut() throws SQLException {
277: cached.set(false);
278: if (inUse.get() == 0)
279: ps.close();
280: }
281:
282: public void close() throws SQLException {
283: inUse.decrement();
284: if (inUse.get() == 0) {
285: if (cached.get() == false)
286: ps.close();
287: else {
288: // Reset the defaults
289: if (defaultMaxFieldSize != currentMaxFieldSize) {
290: ps.setMaxFieldSize(defaultMaxFieldSize);
291: currentMaxFieldSize = defaultMaxFieldSize;
292: }
293: if (defaultMaxRows != currentMaxRows) {
294: ps.setMaxRows(defaultMaxRows);
295: currentMaxRows = defaultMaxRows;
296: }
297: if (defaultQueryTimeout != currentQueryTimeout) {
298: ps.setQueryTimeout(defaultQueryTimeout);
299: currentQueryTimeout = defaultQueryTimeout;
300: }
301: if (defaultFetchDirection != currentFetchDirection) {
302: ps.setFetchDirection(defaultFetchDirection);
303: currentFetchDirection = defaultFetchDirection;
304: }
305: if (defaultFetchSize != currentFetchSize) {
306: ps.setFetchSize(defaultFetchSize);
307: currentFetchSize = defaultFetchSize;
308: }
309: }
310: }
311: }
312:
313: public int getMaxFieldSize() throws SQLException {
314: return ps.getMaxFieldSize();
315: }
316:
317: public void setMaxFieldSize(int max) throws SQLException {
318: ps.setMaxFieldSize(max);
319: currentMaxFieldSize = max;
320: }
321:
322: public int getMaxRows() throws SQLException {
323: return ps.getMaxRows();
324: }
325:
326: public void setMaxRows(int max) throws SQLException {
327: ps.setMaxRows(max);
328: currentMaxRows = max;
329: }
330:
331: public void setEscapeProcessing(boolean enable) throws SQLException {
332: ps.setEscapeProcessing(enable);
333: }
334:
335: public int getQueryTimeout() throws SQLException {
336: return ps.getQueryTimeout();
337: }
338:
339: public void setQueryTimeout(int seconds) throws SQLException {
340: ps.setQueryTimeout(seconds);
341: currentQueryTimeout = seconds;
342: }
343:
344: public void cancel() throws SQLException {
345: ps.cancel();
346: }
347:
348: public SQLWarning getWarnings() throws SQLException {
349: return ps.getWarnings();
350: }
351:
352: public void clearWarnings() throws SQLException {
353: ps.clearWarnings();
354: }
355:
356: public void setCursorName(String name) throws SQLException {
357: ps.setCursorName(name);
358: }
359:
360: public boolean execute(String sql) throws SQLException {
361: return ps.execute(sql);
362: }
363:
364: public ResultSet getResultSet() throws SQLException {
365: return ps.getResultSet();
366: }
367:
368: public int getUpdateCount() throws SQLException {
369: return ps.getUpdateCount();
370: }
371:
372: public boolean getMoreResults() throws SQLException {
373: return ps.getMoreResults();
374: }
375:
376: public void setFetchDirection(int direction) throws SQLException {
377: ps.setFetchDirection(direction);
378: currentFetchDirection = direction;
379: }
380:
381: public int getFetchDirection() throws SQLException {
382: return ps.getFetchDirection();
383: }
384:
385: public void setFetchSize(int rows) throws SQLException {
386: ps.setFetchSize(rows);
387: currentFetchSize = rows;
388: }
389:
390: public int getFetchSize() throws SQLException {
391: return ps.getFetchSize();
392: }
393:
394: public int getResultSetConcurrency() throws SQLException {
395: return ps.getResultSetConcurrency();
396: }
397:
398: public int getResultSetType() throws SQLException {
399: return ps.getResultSetType();
400: }
401:
402: public void addBatch(String sql) throws SQLException {
403: ps.addBatch(sql);
404: }
405:
406: public void clearBatch() throws SQLException {
407: ps.clearBatch();
408: }
409:
410: public int[] executeBatch() throws SQLException {
411: return ps.executeBatch();
412: }
413:
414: public Connection getConnection() throws SQLException {
415: return ps.getConnection();
416: }
417:
418: public boolean getMoreResults(int current) throws SQLException {
419: return ps.getMoreResults(current);
420: }
421:
422: public ResultSet getGeneratedKeys() throws SQLException {
423: return ps.getGeneratedKeys();
424: }
425:
426: public int executeUpdate(String sql, int autoGeneratedKeys)
427: throws SQLException {
428: return ps.executeUpdate(sql, autoGeneratedKeys);
429: }
430:
431: public int executeUpdate(String sql, int columnIndexes[])
432: throws SQLException {
433: return ps.executeUpdate(sql, columnIndexes);
434: }
435:
436: public int executeUpdate(String sql, String columnNames[])
437: throws SQLException {
438: return ps.executeUpdate(sql, columnNames);
439: }
440:
441: public boolean execute(String sql, int autoGeneratedKeys)
442: throws SQLException {
443: return ps.execute(sql, autoGeneratedKeys);
444: }
445:
446: public boolean execute(String sql, int columnIndexes[])
447: throws SQLException {
448: return ps.execute(sql, columnIndexes);
449: }
450:
451: public boolean execute(String sql, String columnNames[])
452: throws SQLException {
453: return ps.execute(sql, columnNames);
454: }
455:
456: public int getResultSetHoldability() throws SQLException {
457: return ps.getResultSetHoldability();
458: }
459: }
|