001: /*
002: * XAPool: Open Source XA JDBC Pool
003: * Copyright (C) 2003 Objectweb.org
004: * Initial Developer: Lutris Technologies Inc.
005: * Contact: xapool-public@lists.debian-sf.objectweb.org
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 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
020: * USA
021: */
022: package org.enhydra.jdbc.core;
023:
024: import org.enhydra.jdbc.util.JdbcUtil;
025: import org.enhydra.jdbc.util.RequestCache;
026:
027: import java.sql.Connection;
028: import java.sql.ResultSet;
029: import java.sql.SQLException;
030: import java.sql.SQLWarning;
031: import java.sql.Statement;
032:
033: /**
034: * A very simple implementation of Statement. When created
035: * it is supplied with another Statement to which all
036: * of this class' methods delegate their work.
037: */
038: public abstract class CoreStatement extends JdbcUtil implements
039: Statement {
040:
041: public Statement statement;
042: public String theQuery;
043: public boolean inCache = false;
044:
045: public boolean getInCache() {
046: return inCache;
047: }
048:
049: public void addBatch(String s) throws SQLException {
050: //preInvoke();
051: try {
052: statement.addBatch(s);
053: } catch (SQLException e) {
054: catchInvoke(e);
055: }
056: }
057:
058: public void cancel() throws SQLException {
059: //preInvoke();
060: try {
061: statement.cancel();
062: } catch (SQLException e) {
063: catchInvoke(e);
064: }
065: }
066:
067: public void clearBatch() throws SQLException {
068: //preInvoke();
069: try {
070: statement.clearBatch();
071: } catch (SQLException e) {
072: catchInvoke(e);
073: }
074: }
075:
076: public void clearWarnings() throws SQLException {
077: //preInvoke();
078: try {
079: statement.clearWarnings();
080: } catch (SQLException e) {
081: catchInvoke(e);
082: }
083: }
084:
085: public void close() throws SQLException {
086: if (statement != null) {
087: RequestCache uc = RequestCache.getInstance();
088: if (!uc.isInCache(theQuery)) {
089: statement.close();
090: }
091: }
092: }
093:
094: public boolean execute(String s) throws SQLException {
095: //preInvoke();
096: try {
097: return statement.execute(s);
098: } catch (SQLException e) {
099: catchInvoke(e);
100: }
101: return false;
102: }
103:
104: public int[] executeBatch() throws SQLException {
105: //preInvoke();
106: try {
107: return statement.executeBatch();
108: } catch (SQLException e) {
109: catchInvoke(e);
110: }
111: return null;
112: }
113:
114: public ResultSet executeQuery(String s) throws SQLException {
115: //preInvoke();
116: try {
117:
118: // si dans le cache, on renvoie le resultset
119: // log.debug("CoreStatement:executeQuery");
120: // sinon on cree le statement
121: RequestCache uc = RequestCache.getInstance();
122: ResultSet rset = null;
123: theQuery = s;
124: if (uc.isInCache(s)) {
125: rset = (ResultSet) (uc.getResult(s));
126: rset.beforeFirst();
127: inCache = true;
128: } else {
129: String rsql = uc.getSqlPattern(s);
130: rset = statement.executeQuery(s);
131:
132: if (rsql != null) {
133: // we need to cache the ResultSet object
134: uc.setResult(rsql, rset);
135: uc.setLink(s, rsql);
136: inCache = true;
137: } else {
138: // sql request is not a cacheable request :-(
139: uc.setLink(s, "null");
140: }
141: }
142:
143: return rset;
144:
145: } catch (SQLException e) {
146: catchInvoke(e);
147: }
148: return null;
149: }
150:
151: public int executeUpdate(String s) throws SQLException {
152: //preInvoke();
153: try {
154: return statement.executeUpdate(s);
155: } catch (SQLException e) {
156: catchInvoke(e);
157: }
158: return 0;
159: }
160:
161: public Connection getConnection() throws SQLException {
162: //preInvoke();
163: try {
164: return statement.getConnection();
165: } catch (SQLException e) {
166: catchInvoke(e);
167: }
168: return null;
169: }
170:
171: public int getFetchDirection() throws SQLException {
172: //preInvoke();
173: try {
174: return statement.getFetchDirection();
175: } catch (SQLException e) {
176: catchInvoke(e);
177: }
178: return 0;
179: }
180:
181: public int getFetchSize() throws SQLException {
182: //preInvoke();
183: try {
184: return statement.getFetchSize();
185: } catch (SQLException e) {
186: catchInvoke(e);
187: }
188: return 0;
189: }
190:
191: public ResultSet getGeneratedKeys() throws SQLException {
192: //preInvoke();
193: try {
194: return statement.getGeneratedKeys();
195: } catch (SQLException e) {
196: catchInvoke(e);
197: }
198: return null;
199: }
200:
201: public int getMaxFieldSize() throws SQLException {
202: //preInvoke();
203: try {
204: return statement.getMaxFieldSize();
205: } catch (SQLException e) {
206: catchInvoke(e);
207: }
208: return 0;
209: }
210:
211: public int getMaxRows() throws SQLException {
212: //preInvoke();
213: try {
214: return statement.getMaxRows();
215: } catch (SQLException e) {
216: catchInvoke(e);
217: }
218: return 0;
219: }
220:
221: public boolean getMoreResults() throws SQLException {
222: //preInvoke();
223: try {
224: return statement.getMoreResults();
225: } catch (SQLException e) {
226: catchInvoke(e);
227: }
228: return false;
229: }
230:
231: public int getQueryTimeout() throws SQLException {
232: //preInvoke();
233: try {
234: return statement.getQueryTimeout();
235: } catch (SQLException e) {
236: catchInvoke(e);
237: }
238: return 0;
239: }
240:
241: public ResultSet getResultSet() throws SQLException {
242: //preInvoke();
243: try {
244: return statement.getResultSet();
245: } catch (SQLException e) {
246: catchInvoke(e);
247: }
248: return null;
249: }
250:
251: public int getResultSetConcurrency() throws SQLException {
252: //preInvoke();
253: try {
254: return statement.getResultSetConcurrency();
255: } catch (SQLException e) {
256: catchInvoke(e);
257: }
258: return 0;
259: }
260:
261: public int getResultSetType() throws SQLException {
262: //preInvoke();
263: try {
264: return statement.getResultSetType();
265: } catch (SQLException e) {
266: catchInvoke(e);
267: }
268: return 0;
269: }
270:
271: public int getUpdateCount() throws SQLException {
272: //preInvoke();
273: try {
274: return statement.getUpdateCount();
275: } catch (SQLException e) {
276: catchInvoke(e);
277: }
278: return 0;
279: }
280:
281: public SQLWarning getWarnings() throws SQLException {
282: //preInvoke();
283: try {
284: return statement.getWarnings();
285: } catch (SQLException e) {
286: catchInvoke(e);
287: }
288: return null;
289: }
290:
291: public void setCursorName(String name) throws SQLException {
292: //preInvoke();
293: try {
294: statement.setCursorName(name);
295: } catch (SQLException e) {
296: catchInvoke(e);
297: }
298: }
299:
300: public void setEscapeProcessing(boolean enable) throws SQLException {
301: //preInvoke();
302: try {
303: statement.setEscapeProcessing(enable);
304: } catch (SQLException e) {
305: catchInvoke(e);
306: }
307: }
308:
309: public void setFetchDirection(int direction) throws SQLException {
310: //preInvoke();
311: try {
312: statement.setFetchDirection(direction);
313: } catch (SQLException e) {
314: catchInvoke(e);
315: }
316: }
317:
318: public void setFetchSize(int rows) throws SQLException {
319: //preInvoke();
320: try {
321: statement.setFetchSize(rows);
322: } catch (SQLException e) {
323: catchInvoke(e);
324: }
325: }
326:
327: public void setMaxFieldSize(int max) throws SQLException {
328: //preInvoke();
329: try {
330: statement.setMaxFieldSize(max);
331: } catch (SQLException e) {
332: catchInvoke(e);
333: }
334: }
335:
336: public void setMaxRows(int max) throws SQLException {
337: //preInvoke();
338: try {
339: statement.setMaxRows(max);
340: } catch (SQLException e) {
341: catchInvoke(e);
342: }
343: }
344:
345: public void setQueryTimeout(int seconds) throws SQLException {
346: //preInvoke();
347: try {
348: statement.setQueryTimeout(seconds);
349: } catch (SQLException e) {
350: catchInvoke(e);
351: }
352: }
353:
354: /*
355: * Add those following methods to compile on JDK 1.4.
356: * Instead those methods are defined in the java.sql.Statement interface
357: * only since JDK 1.4.
358: */
359: public boolean execute(String sql, int autoGeneratedKeys)
360: throws SQLException {
361: try {
362: return statement.execute(sql, autoGeneratedKeys);
363: } catch (SQLException e) {
364: catchInvoke(e);
365: }
366: return false;
367: }
368:
369: public boolean execute(String sql, int[] columnIndexes)
370: throws SQLException {
371: try {
372: return statement.execute(sql, columnIndexes);
373: } catch (SQLException e) {
374: catchInvoke(e);
375: }
376: return false;
377: }
378:
379: public boolean execute(String sql, String[] columnNames)
380: throws SQLException {
381: try {
382: return statement.execute(sql, columnNames);
383: } catch (SQLException e) {
384: catchInvoke(e);
385: }
386: return false;
387: }
388:
389: public int executeUpdate(String sql, int autoGeneratedKeys)
390: throws SQLException {
391: try {
392: return statement.executeUpdate(sql, autoGeneratedKeys);
393: } catch (SQLException e) {
394: catchInvoke(e);
395: }
396: return 0;
397: }
398:
399: public int executeUpdate(String sql, int[] columnIndexes)
400: throws SQLException {
401: try {
402: return statement.executeUpdate(sql, columnIndexes);
403: } catch (SQLException e) {
404: catchInvoke(e);
405: }
406: return 0;
407: }
408:
409: public int executeUpdate(String sql, String[] columnNames)
410: throws SQLException {
411: try {
412: return statement.executeUpdate(sql, columnNames);
413: } catch (SQLException e) {
414: catchInvoke(e);
415: }
416: return 0;
417: }
418:
419: public boolean getMoreResults(int current) throws SQLException {
420: try {
421: return statement.getMoreResults(current);
422: } catch (SQLException e) {
423: catchInvoke(e);
424: }
425: return false;
426: }
427:
428: public int getResultSetHoldability() throws SQLException {
429: try {
430: return statement.getResultSetHoldability();
431: } catch (SQLException e) {
432: catchInvoke(e);
433: }
434: return 0;
435: }
436:
437: /**
438: * Methods used to do some works before and during the catch
439: * clause, to prevent the pool that a connection is broken.
440: */
441: //abstract public void preInvoke() throws SQLException;
442: abstract public void catchInvoke(SQLException e)
443: throws SQLException;
444:
445: }
|