001: /**
002: * Library name : Primrose - A Java Database Connection Pool.
003: * Published by Ben Keeping, http://primrose.org.uk .
004: * Copyright (C) 2004 Ben Keeping, primrose.org.uk
005: * Email: Use "Contact Us Form" on website
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) any later version.
011: *
012: * This library 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 library; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020: */package uk.org.primrose.pool.core;
021:
022: import java.sql.*;
023:
024: /**
025: * A wrapper for a vendor specific implementation of Statement.
026: * Allows for complete logging of SQL transactions, aswell as identifying
027: * unclosed statements before Connection close() calls.
028: */
029: public class PoolStatement implements Statement {
030: Statement s = null;
031: ConnectionHolder connHolder = null;
032:
033: public PoolStatement(Statement s, ConnectionHolder connHolder) {
034: this .connHolder = connHolder;
035: this .connHolder.statementObjects.push(this );
036: this .s = s;
037: }
038:
039: public PoolStatement() {
040: }
041:
042: protected void closeNoPop() throws SQLException {
043: s.close();
044: }
045:
046: public void close() throws SQLException {
047: for (int i = 0; i < connHolder.statementObjects.size(); i++) {
048: PoolStatement ts = connHolder.statementObjects.get(i);
049: if (ts == this ) {
050: connHolder.statementObjects.remove(this );
051: break;
052: }
053: }
054:
055: if (this .connHolder.bDumpConnectionOnSQLException) {
056: try {
057: s.close();
058: } catch (SQLException sqle) {
059: this .connHolder.closeBehaviour = Pool.ON_CLOSE_SHOULD_DIE;
060: this .connHolder.logger
061: .warn("Closing connection due to SQLException on close()");
062: this .connHolder.myPool.notifyExceptionEvent();
063: throw sqle;
064: }
065: } else {
066: s.close();
067: }
068: }
069:
070: public ResultSet executeQuery(String sql) throws SQLException {
071: this .connHolder.sql = sql;
072: if (this .connHolder.bDumpConnectionOnSQLException) {
073: try {
074: PoolResultSet prs = new PoolResultSet(s
075: .executeQuery(sql), this .connHolder);
076: return prs;
077: } catch (SQLException sqle) {
078: this .connHolder.closeBehaviour = Pool.ON_CLOSE_SHOULD_DIE;
079: this .connHolder.logger
080: .warn("Closing connection due to SQLException on execute");
081: this .connHolder.myPool.notifyExceptionEvent();
082: throw sqle;
083: }
084: } else {
085: PoolResultSet prs = new PoolResultSet(s.executeQuery(sql),
086: this .connHolder);
087: return prs;
088: }
089: }
090:
091: public int executeUpdate(String sql) throws SQLException {
092: this .connHolder.sql = sql;
093: if (this .connHolder.bDumpConnectionOnSQLException) {
094: try {
095: return s.executeUpdate(sql);
096: } catch (SQLException sqle) {
097: this .connHolder.closeBehaviour = Pool.ON_CLOSE_SHOULD_DIE;
098: this .connHolder.logger
099: .warn("Closing connection due to SQLException on execute");
100: this .connHolder.myPool.notifyExceptionEvent();
101: throw sqle;
102: }
103: } else {
104: return s.executeUpdate(sql);
105: }
106: }
107:
108: public int executeUpdate(String sql, int autoGeneratedKeys)
109: throws SQLException {
110:
111: this .connHolder.sql = sql;
112: if (this .connHolder.bDumpConnectionOnSQLException) {
113: try {
114: return s.executeUpdate(sql, autoGeneratedKeys);
115: } catch (SQLException sqle) {
116: this .connHolder.closeBehaviour = Pool.ON_CLOSE_SHOULD_DIE;
117: this .connHolder.logger
118: .warn("Closing connection due to SQLException on execute");
119: this .connHolder.myPool.notifyExceptionEvent();
120: throw sqle;
121: }
122: } else {
123: return s.executeUpdate(sql, autoGeneratedKeys);
124: }
125: }
126:
127: public int executeUpdate(String sql, int columnIndexes[])
128: throws SQLException {
129: this .connHolder.sql = sql;
130: if (this .connHolder.bDumpConnectionOnSQLException) {
131: try {
132: return s.executeUpdate(sql, columnIndexes);
133: } catch (SQLException sqle) {
134: this .connHolder.closeBehaviour = Pool.ON_CLOSE_SHOULD_DIE;
135: this .connHolder.logger
136: .warn("Closing connection due to SQLException on execute");
137: this .connHolder.myPool.notifyExceptionEvent();
138: throw sqle;
139: }
140: } else {
141: return s.executeUpdate(sql, columnIndexes);
142: }
143: }
144:
145: public int executeUpdate(String sql, String columnNames[])
146: throws SQLException {
147: this .connHolder.sql = sql;
148: if (this .connHolder.bDumpConnectionOnSQLException) {
149: try {
150: return s.executeUpdate(sql, columnNames);
151: } catch (SQLException sqle) {
152: this .connHolder.closeBehaviour = Pool.ON_CLOSE_SHOULD_DIE;
153: this .connHolder.logger
154: .warn("Closing connection due to SQLException on execute");
155: this .connHolder.myPool.notifyExceptionEvent();
156: throw sqle;
157: }
158: } else {
159: return s.executeUpdate(sql, columnNames);
160: }
161: }
162:
163: public boolean execute(String sql, int autoGeneratedKeys)
164: throws SQLException {
165: this .connHolder.sql = sql;
166: if (this .connHolder.bDumpConnectionOnSQLException) {
167: try {
168: return s.execute(sql, autoGeneratedKeys);
169: } catch (SQLException sqle) {
170: this .connHolder.closeBehaviour = Pool.ON_CLOSE_SHOULD_DIE;
171: this .connHolder.logger
172: .warn("Closing connection due to SQLException on execute");
173: this .connHolder.myPool.notifyExceptionEvent();
174: throw sqle;
175: }
176: } else {
177: return s.execute(sql, autoGeneratedKeys);
178: }
179: }
180:
181: public boolean execute(String sql, int columnIndexes[])
182: throws SQLException {
183: this .connHolder.sql = sql;
184: if (this .connHolder.bDumpConnectionOnSQLException) {
185: try {
186: return s.execute(sql, columnIndexes);
187: } catch (SQLException sqle) {
188: this .connHolder.closeBehaviour = Pool.ON_CLOSE_SHOULD_DIE;
189: this .connHolder.logger
190: .warn("Closing connection due to SQLException on execute");
191: this .connHolder.myPool.notifyExceptionEvent();
192: throw sqle;
193: }
194: } else {
195: return s.execute(sql, columnIndexes);
196: }
197: }
198:
199: public boolean execute(String sql, String columnNames[])
200: throws SQLException {
201: this .connHolder.sql = sql;
202: if (this .connHolder.bDumpConnectionOnSQLException) {
203: try {
204: return s.execute(sql, columnNames);
205: } catch (SQLException sqle) {
206: this .connHolder.closeBehaviour = Pool.ON_CLOSE_SHOULD_DIE;
207: this .connHolder.logger
208: .warn("Closing connection due to SQLException on execute");
209: this .connHolder.myPool.notifyExceptionEvent();
210: throw sqle;
211: }
212: } else {
213: return s.execute(sql, columnNames);
214: }
215: }
216:
217: public int[] executeBatch() throws SQLException {
218: if (this .connHolder.bDumpConnectionOnSQLException) {
219: try {
220: return s.executeBatch();
221: } catch (SQLException sqle) {
222: this .connHolder.closeBehaviour = Pool.ON_CLOSE_SHOULD_DIE;
223: this .connHolder.logger
224: .warn("Closing connection due to SQLException on execute");
225: this .connHolder.myPool.notifyExceptionEvent();
226: throw sqle;
227: }
228: } else {
229: return s.executeBatch();
230: }
231: }
232:
233: public int getMaxFieldSize() throws SQLException {
234: return s.getMaxFieldSize();
235: }
236:
237: public void setMaxFieldSize(int max) throws SQLException {
238: s.setMaxFieldSize(max);
239: }
240:
241: public int getMaxRows() throws SQLException {
242: return s.getMaxRows();
243: }
244:
245: public void setMaxRows(int max) throws SQLException {
246: s.setMaxRows(max);
247: }
248:
249: public void setEscapeProcessing(boolean enable) throws SQLException {
250: s.setEscapeProcessing(enable);
251: }
252:
253: public int getQueryTimeout() throws SQLException {
254: return s.getQueryTimeout();
255: }
256:
257: public void setQueryTimeout(int seconds) throws SQLException {
258: s.setQueryTimeout(seconds);
259: }
260:
261: public void cancel() throws SQLException {
262: s.cancel();
263: }
264:
265: public SQLWarning getWarnings() throws SQLException {
266: return s.getWarnings();
267: }
268:
269: public void clearWarnings() throws SQLException {
270: s.clearWarnings();
271: }
272:
273: public void setCursorName(String name) throws SQLException {
274: s.setCursorName(name);
275: }
276:
277: public boolean execute(String sql) throws SQLException {
278: this .connHolder.sql = sql;
279: return s.execute(sql);
280: }
281:
282: public ResultSet getResultSet() throws SQLException {
283: PoolResultSet prs = new PoolResultSet(s.getResultSet(),
284: this .connHolder);
285: return prs;
286: }
287:
288: public int getUpdateCount() throws SQLException {
289: return s.getUpdateCount();
290: }
291:
292: public boolean getMoreResults() throws SQLException {
293: return s.getMoreResults();
294: }
295:
296: public void setFetchDirection(int direction) throws SQLException {
297: s.setFetchDirection(direction);
298: }
299:
300: public int getFetchDirection() throws SQLException {
301: return s.getFetchDirection();
302: }
303:
304: public void setFetchSize(int rows) throws SQLException {
305: s.setFetchSize(rows);
306: }
307:
308: public int getFetchSize() throws SQLException {
309: return s.getFetchSize();
310: }
311:
312: public int getResultSetConcurrency() throws SQLException {
313: return s.getResultSetConcurrency();
314: }
315:
316: public int getResultSetType() throws SQLException {
317: return s.getResultSetType();
318: }
319:
320: public void addBatch(String sql) throws SQLException {
321: s.addBatch(sql);
322: }
323:
324: public void clearBatch() throws SQLException {
325: s.clearBatch();
326: }
327:
328: public Connection getConnection() throws SQLException {
329: return s.getConnection();
330: }
331:
332: public boolean getMoreResults(int current) throws SQLException {
333: return s.getMoreResults(current);
334: }
335:
336: public ResultSet getGeneratedKeys() throws SQLException {
337: PoolResultSet prs = new PoolResultSet(s.getGeneratedKeys(),
338: this .connHolder);
339: return prs;
340: }
341:
342: public int getResultSetHoldability() throws SQLException {
343: return s.getResultSetHoldability();
344: }
345:
346: }
|