001: /**
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 1999-2004 Bull S.A.
004: * Contact: jonas-team@objectweb.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * --------------------------------------------------------------------------
022: * $Id: JStatement.java 8307 2006-04-27 13:36:00Z durieuxp $
023: * --------------------------------------------------------------------------
024: */package org.objectweb.jonas.dbm;
025:
026: import java.io.InputStream;
027: import java.io.Reader;
028: import java.math.BigDecimal;
029: import java.net.URL;
030: import java.sql.Array;
031: import java.sql.Blob;
032: import java.sql.Clob;
033: import java.sql.Connection;
034: import java.sql.Date;
035: import java.sql.ParameterMetaData;
036: import java.sql.PreparedStatement;
037: import java.sql.Ref;
038: import java.sql.ResultSet;
039: import java.sql.ResultSetMetaData;
040: import java.sql.SQLException;
041: import java.sql.SQLWarning;
042: import java.sql.Time;
043: import java.sql.Timestamp;
044: import java.util.Calendar;
045:
046: import org.objectweb.jonas.common.Log;
047: import org.objectweb.util.monolog.api.BasicLevel;
048: import org.objectweb.util.monolog.api.Logger;
049:
050: /**
051: * Wrapper on a PreparedStatement. This wrapper is used to track close method
052: * in order to avoid closing the statement, and putting it instead in a pool.
053: * @author durieuxp
054: */
055: public class JStatement implements PreparedStatement {
056:
057: private boolean changed = false;
058: private boolean opened;
059: private boolean closing = false;
060:
061: /**
062: * Physical PreparedStatement object on which the wrapper is.
063: */
064: private PreparedStatement ps;
065:
066: /**
067: * Managed Connection the Statement belongs to
068: */
069: private JManagedConnection mc;
070:
071: private int hashCode;
072: private String sql;
073: private Logger logger = Log.getLogger(Log.JONAS_DBM_PREFIX + ".ps");
074:
075: public JStatement(PreparedStatement ps, JManagedConnection mc,
076: String sql) {
077: logger.log(BasicLevel.DEBUG, "constructor");
078: this .ps = ps;
079: this .mc = mc;
080: this .sql = sql;
081: hashCode = sql.hashCode();
082: opened = true;
083: }
084:
085: public String getSql() {
086: return sql;
087: }
088:
089: /**
090: * @return hashcode of the object
091: */
092: public int hashCode() {
093: return hashCode;
094: }
095:
096: /**
097: * @param stmt given statement for comparing it
098: * @return true if given object is equals to this current object
099: */
100: public boolean equals(Object stmt) {
101: if (stmt == null) {
102: return false;
103: }
104: // different hashcode, cannot be equals
105: if (this .hashCode != stmt.hashCode()) {
106: return false;
107: }
108:
109: // if got same hashcode, try to see if cast is ok.
110: if (!(stmt instanceof JStatement)) {
111: logger.log(BasicLevel.WARN, "Bad class:" + stmt);
112: return false;
113: }
114: JStatement psw = (JStatement) stmt;
115: if (sql == null && psw.getSql() != null) {
116: return false;
117: }
118: if (sql != null && !sql.equals(psw.getSql())) {
119: return false;
120: }
121: try {
122: if (psw.getResultSetType() != getResultSetType()) {
123: return false;
124: }
125: if (psw.getResultSetConcurrency() != getResultSetConcurrency()) {
126: return false;
127: }
128: } catch (SQLException e) {
129: logger.log(BasicLevel.WARN, "Cannot compare statements:"
130: + e);
131: return false;
132: }
133: logger.log(BasicLevel.DEBUG, "Found");
134: return true;
135: }
136:
137: /**
138: * Force a close on the Prepare Statement.
139: * Usually, it's the caller that did not close it explicitly
140: * @return true if it was open
141: */
142: public boolean forceClose() {
143: if (opened) {
144: logger.log(BasicLevel.WARN,
145: "Statements should be closed explicitly.");
146: opened = false;
147: return true;
148: } else {
149: return false;
150: }
151: }
152:
153: public void reuse() throws SQLException {
154: logger.log(BasicLevel.DEBUG, "");
155: ps.clearParameters();
156: ps.clearWarnings();
157: opened = true;
158: if (changed) {
159: logger.log(BasicLevel.DEBUG, "changed");
160: ps.clearBatch();
161: ps.setFetchDirection(ResultSet.FETCH_FORWARD);
162: ps.setMaxFieldSize(0);
163: ps.setMaxRows(0);
164: ps.setQueryTimeout(0);
165: changed = false;
166: }
167: }
168:
169: public boolean isClosed() {
170: return !opened && !closing;
171: }
172:
173: /**
174: * Physically close this Statement
175: * @throws SQLException
176: */
177: public void forget() {
178: logger.log(BasicLevel.DEBUG, "");
179: try {
180: ps.close();
181: } catch (SQLException e) {
182: logger.log(BasicLevel.ERROR,
183: "Cannot close the PreparedStatement:" + e);
184: }
185: }
186:
187: // ------------------------------------------------------------------
188: // PreparedStatement implementation.
189: // Most of the methods are just redirected on the PreparedStatement.
190: // ------------------------------------------------------------------
191:
192: public int executeUpdate() throws SQLException {
193: logger.log(BasicLevel.DEBUG, "");
194: return ps.executeUpdate();
195: }
196:
197: public void addBatch() throws SQLException {
198: logger.log(BasicLevel.DEBUG, "");
199: changed = true;
200: ps.addBatch();
201: }
202:
203: public void clearParameters() throws SQLException {
204: logger.log(BasicLevel.DEBUG, "");
205: ps.clearParameters();
206: }
207:
208: public boolean execute() throws SQLException {
209: logger.log(BasicLevel.DEBUG, "");
210: return ps.execute();
211: }
212:
213: public void setByte(int parameterIndex, byte x) throws SQLException {
214: ps.setByte(parameterIndex, x);
215: }
216:
217: public void setDouble(int parameterIndex, double x)
218: throws SQLException {
219: ps.setDouble(parameterIndex, x);
220: }
221:
222: public void setFloat(int parameterIndex, float x)
223: throws SQLException {
224: ps.setFloat(parameterIndex, x);
225: }
226:
227: public void setInt(int parameterIndex, int x) throws SQLException {
228: ps.setInt(parameterIndex, x);
229: }
230:
231: public void setNull(int parameterIndex, int sqlType)
232: throws SQLException {
233: logger.log(BasicLevel.DEBUG, "");
234: ps.setNull(parameterIndex, sqlType);
235: }
236:
237: public void setLong(int parameterIndex, long x) throws SQLException {
238: ps.setLong(parameterIndex, x);
239: }
240:
241: public void setShort(int parameterIndex, short x)
242: throws SQLException {
243: ps.setShort(parameterIndex, x);
244: }
245:
246: public void setBoolean(int parameterIndex, boolean x)
247: throws SQLException {
248: ps.setBoolean(parameterIndex, x);
249: }
250:
251: public void setBytes(int parameterIndex, byte[] x)
252: throws SQLException {
253: ps.setBytes(parameterIndex, x);
254: }
255:
256: public void setAsciiStream(int parameterIndex, InputStream x,
257: int length) throws SQLException {
258: logger.log(BasicLevel.DEBUG, "");
259: ps.setAsciiStream(parameterIndex, x, length);
260: }
261:
262: public void setBinaryStream(int parameterIndex, InputStream x,
263: int length) throws SQLException {
264: logger.log(BasicLevel.DEBUG, "");
265: ps.setBinaryStream(parameterIndex, x, length);
266: }
267:
268: public void setUnicodeStream(int parameterIndex, InputStream x,
269: int length) throws SQLException {
270: logger.log(BasicLevel.DEBUG, "");
271: ps.setUnicodeStream(parameterIndex, x, length);
272: }
273:
274: public void setCharacterStream(int parameterIndex, Reader reader,
275: int length) throws SQLException {
276: logger.log(BasicLevel.DEBUG, "");
277: ps.setCharacterStream(parameterIndex, reader, length);
278: }
279:
280: public void setObject(int parameterIndex, Object x)
281: throws SQLException {
282: logger.log(BasicLevel.DEBUG, "");
283: ps.setObject(parameterIndex, x);
284: }
285:
286: public void setObject(int parameterIndex, Object x,
287: int targetSqlType) throws SQLException {
288: logger.log(BasicLevel.DEBUG, "");
289: ps.setObject(parameterIndex, x, targetSqlType);
290: }
291:
292: public void setObject(int parameterIndex, Object x,
293: int targetSqlType, int scale) throws SQLException {
294: logger.log(BasicLevel.DEBUG, "");
295: ps.setObject(parameterIndex, x, targetSqlType, scale);
296: }
297:
298: public void setNull(int paramIndex, int sqlType, String typeName)
299: throws SQLException {
300: logger.log(BasicLevel.DEBUG, "");
301: ps.setNull(paramIndex, sqlType, typeName);
302: }
303:
304: public void setString(int parameterIndex, String x)
305: throws SQLException {
306: ps.setString(parameterIndex, x);
307: }
308:
309: public void setBigDecimal(int parameterIndex, BigDecimal x)
310: throws SQLException {
311: logger.log(BasicLevel.DEBUG, "");
312: ps.setBigDecimal(parameterIndex, x);
313: }
314:
315: public void setURL(int parameterIndex, URL x) throws SQLException {
316: logger.log(BasicLevel.DEBUG, "");
317: ps.setURL(parameterIndex, x);
318: }
319:
320: public void setArray(int i, Array x) throws SQLException {
321: logger.log(BasicLevel.DEBUG, "");
322: ps.setArray(i, x);
323: }
324:
325: public void setBlob(int i, Blob x) throws SQLException {
326: logger.log(BasicLevel.DEBUG, "");
327: ps.setBlob(i, x);
328: }
329:
330: public void setClob(int i, Clob x) throws SQLException {
331: logger.log(BasicLevel.DEBUG, "");
332: ps.setClob(i, x);
333: }
334:
335: public void setDate(int parameterIndex, Date x) throws SQLException {
336: logger.log(BasicLevel.DEBUG, "");
337: ps.setDate(parameterIndex, x);
338: }
339:
340: public ParameterMetaData getParameterMetaData() throws SQLException {
341: logger.log(BasicLevel.DEBUG, "");
342: return ps.getParameterMetaData();
343: }
344:
345: public void setRef(int i, Ref x) throws SQLException {
346: logger.log(BasicLevel.DEBUG, "");
347: ps.setRef(i, x);
348: }
349:
350: public ResultSet executeQuery() throws SQLException {
351: logger.log(BasicLevel.DEBUG, "");
352: return ps.executeQuery();
353: }
354:
355: public ResultSetMetaData getMetaData() throws SQLException {
356: logger.log(BasicLevel.DEBUG, "");
357: return ps.getMetaData();
358: }
359:
360: public void setTime(int parameterIndex, Time x) throws SQLException {
361: logger.log(BasicLevel.DEBUG, "");
362: ps.setTime(parameterIndex, x);
363: }
364:
365: public void setTimestamp(int parameterIndex, Timestamp x)
366: throws SQLException {
367: logger.log(BasicLevel.DEBUG, "");
368: ps.setTimestamp(parameterIndex, x);
369: }
370:
371: public void setDate(int parameterIndex, Date x, Calendar cal)
372: throws SQLException {
373: logger.log(BasicLevel.DEBUG, "");
374: ps.setDate(parameterIndex, x, cal);
375: }
376:
377: public void setTime(int parameterIndex, Time x, Calendar cal)
378: throws SQLException {
379: logger.log(BasicLevel.DEBUG, "");
380: ps.setTime(parameterIndex, x, cal);
381: }
382:
383: public void setTimestamp(int parameterIndex, Timestamp x,
384: Calendar cal) throws SQLException {
385: logger.log(BasicLevel.DEBUG, "");
386: ps.setTimestamp(parameterIndex, x, cal);
387: }
388:
389: public int getFetchDirection() throws SQLException {
390: return ps.getFetchDirection();
391: }
392:
393: public int getFetchSize() throws SQLException {
394: return ps.getFetchSize();
395: }
396:
397: public int getMaxFieldSize() throws SQLException {
398: return ps.getMaxFieldSize();
399: }
400:
401: public int getMaxRows() throws SQLException {
402: return ps.getMaxRows();
403: }
404:
405: public int getQueryTimeout() throws SQLException {
406: return ps.getQueryTimeout();
407: }
408:
409: public int getResultSetConcurrency() throws SQLException {
410: return ps.getResultSetConcurrency();
411: }
412:
413: public int getResultSetHoldability() throws SQLException {
414: return ps.getResultSetHoldability();
415: }
416:
417: public int getResultSetType() throws SQLException {
418: return ps.getResultSetType();
419: }
420:
421: public int getUpdateCount() throws SQLException {
422: return ps.getUpdateCount();
423: }
424:
425: public void cancel() throws SQLException {
426: logger.log(BasicLevel.DEBUG, "");
427: ps.cancel();
428: }
429:
430: public void clearBatch() throws SQLException {
431: logger.log(BasicLevel.DEBUG, "");
432: ps.clearBatch();
433: }
434:
435: public void clearWarnings() throws SQLException {
436: logger.log(BasicLevel.DEBUG, "");
437: ps.clearWarnings();
438: }
439:
440: public void close() throws SQLException {
441: if (!opened) {
442: logger.log(BasicLevel.DEBUG, "Statement already closed");
443: return;
444: }
445: logger.log(BasicLevel.DEBUG, "");
446: opened = false;
447: closing = true;
448: mc.notifyPsClose(this );
449: closing = false;
450: }
451:
452: public boolean getMoreResults() throws SQLException {
453: logger.log(BasicLevel.DEBUG, "");
454: return ps.getMoreResults();
455: }
456:
457: public int[] executeBatch() throws SQLException {
458: logger.log(BasicLevel.DEBUG, "");
459: return ps.executeBatch();
460: }
461:
462: public void setFetchDirection(int direction) throws SQLException {
463: logger.log(BasicLevel.DEBUG, "");
464: changed = true;
465: ps.setFetchDirection(direction);
466: }
467:
468: public void setFetchSize(int rows) throws SQLException {
469: logger.log(BasicLevel.DEBUG, "");
470: changed = true;
471: ps.setFetchSize(rows);
472: }
473:
474: public void setMaxFieldSize(int max) throws SQLException {
475: logger.log(BasicLevel.DEBUG, "");
476: changed = true;
477: ps.setMaxFieldSize(max);
478: }
479:
480: public void setMaxRows(int max) throws SQLException {
481: logger.log(BasicLevel.DEBUG, "");
482: changed = true;
483: ps.setMaxRows(max);
484: }
485:
486: public void setQueryTimeout(int seconds) throws SQLException {
487: logger.log(BasicLevel.DEBUG, "");
488: changed = true;
489: ps.setQueryTimeout(seconds);
490: }
491:
492: public boolean getMoreResults(int current) throws SQLException {
493: return ps.getMoreResults(current);
494: }
495:
496: public void setEscapeProcessing(boolean enable) throws SQLException {
497: logger.log(BasicLevel.DEBUG, "");
498: ps.setEscapeProcessing(enable);
499: }
500:
501: public int executeUpdate(String sql) throws SQLException {
502: logger.log(BasicLevel.DEBUG, sql);
503: return ps.executeUpdate(sql);
504: }
505:
506: public void addBatch(String sql) throws SQLException {
507: logger.log(BasicLevel.DEBUG, sql);
508: changed = true;
509: ps.addBatch(sql);
510: }
511:
512: public void setCursorName(String name) throws SQLException {
513: logger.log(BasicLevel.DEBUG, name);
514: ps.setCursorName(name);
515: }
516:
517: public boolean execute(String sql) throws SQLException {
518: logger.log(BasicLevel.DEBUG, sql);
519: changed = true;
520: return ps.execute(sql);
521: }
522:
523: public int executeUpdate(String sql, int autoGeneratedKeys)
524: throws SQLException {
525: logger.log(BasicLevel.DEBUG, sql);
526: changed = true;
527: return ps.executeUpdate(sql, autoGeneratedKeys);
528: }
529:
530: public boolean execute(String sql, int autoGeneratedKeys)
531: throws SQLException {
532: logger.log(BasicLevel.DEBUG, sql);
533: changed = true;
534: return ps.execute(sql, autoGeneratedKeys);
535: }
536:
537: public int executeUpdate(String sql, int[] columnIndexes)
538: throws SQLException {
539: logger.log(BasicLevel.DEBUG, sql);
540: changed = true;
541: return ps.executeUpdate(sql, columnIndexes);
542: }
543:
544: public boolean execute(String sql, int[] columnIndexes)
545: throws SQLException {
546: logger.log(BasicLevel.DEBUG, sql);
547: changed = true;
548: return ps.execute(sql, columnIndexes);
549: }
550:
551: public Connection getConnection() throws SQLException {
552: return ps.getConnection();
553: }
554:
555: public ResultSet getGeneratedKeys() throws SQLException {
556: return ps.getGeneratedKeys();
557: }
558:
559: public ResultSet getResultSet() throws SQLException {
560: return ps.getResultSet();
561: }
562:
563: public SQLWarning getWarnings() throws SQLException {
564: return ps.getWarnings();
565: }
566:
567: public int executeUpdate(String sql, String[] columnNames)
568: throws SQLException {
569: logger.log(BasicLevel.DEBUG, sql);
570: return ps.executeUpdate(sql, columnNames);
571: }
572:
573: public boolean execute(String sql, String[] columnNames)
574: throws SQLException {
575: logger.log(BasicLevel.DEBUG, sql);
576: return ps.execute(sql, columnNames);
577: }
578:
579: public ResultSet executeQuery(String sql) throws SQLException {
580: logger.log(BasicLevel.DEBUG, sql);
581: return ps.executeQuery(sql);
582: }
583:
584: }
|