KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > easybeans > component > jdbcpool > JStatement


1 /**
2  * EasyBeans
3  * Copyright (C) 2006 Bull S.A.S.
4  * Contact: easybeans@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: JStatement.java 1022 2006-08-04 11:02:28Z benoitf $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.easybeans.component.jdbcpool;
27
28 import java.io.InputStream JavaDoc;
29 import java.io.Reader JavaDoc;
30 import java.math.BigDecimal JavaDoc;
31 import java.net.URL JavaDoc;
32 import java.sql.Array JavaDoc;
33 import java.sql.Blob JavaDoc;
34 import java.sql.Clob JavaDoc;
35 import java.sql.Connection JavaDoc;
36 import java.sql.Date JavaDoc;
37 import java.sql.ParameterMetaData JavaDoc;
38 import java.sql.PreparedStatement JavaDoc;
39 import java.sql.Ref JavaDoc;
40 import java.sql.ResultSet JavaDoc;
41 import java.sql.ResultSetMetaData JavaDoc;
42 import java.sql.SQLException JavaDoc;
43 import java.sql.SQLWarning JavaDoc;
44 import java.sql.Time JavaDoc;
45 import java.sql.Timestamp JavaDoc;
46 import java.util.Calendar JavaDoc;
47
48 import org.objectweb.easybeans.log.JLog;
49 import org.objectweb.easybeans.log.JLogFactory;
50
51 /**
52  * Wrapper on a PreparedStatement. This wrapper is used to track close method in
53  * order to avoid closing the statement, and putting it instead in a pool.
54  * @author Philippe Durieux
55  * @author Florent Benoit
56  */

57 public class JStatement implements PreparedStatement JavaDoc {
58
59     /**
60      * Properties of this statement has been changed ? Needs to be be cleared when reused.
61      */

62     private boolean changed = false;
63
64     /**
65      * Is that this statement is opened ?
66      */

67     private boolean opened = false;
68
69     /**
70      * Being closed. (in close method).
71      */

72     private boolean closing = false;
73
74     /**
75      * Physical PreparedStatement object on which the wrapper is.
76      */

77     private PreparedStatement JavaDoc ps;
78
79     /**
80      * Managed Connection the Statement belongs to.
81      */

82     private JManagedConnection mc;
83
84     /**
85      * Hashcode computed in constructor.
86      */

87     private int hashCode;
88
89     /**
90      * SQL used as statement.
91      */

92     private String JavaDoc sql;
93
94     /**
95      * Logger.
96      */

97     private JLog logger = JLogFactory.getLog(JStatement.class);
98
99     /**
100      * Builds a new statement with the given wrapped statement of given connection and given sql query.
101      * @param ps the prepared statement.
102      * @param mc managed connection
103      * @param sql query.
104      */

105     public JStatement(final PreparedStatement JavaDoc ps, final JManagedConnection mc, final String JavaDoc sql) {
106         this.ps = ps;
107         this.mc = mc;
108         this.sql = sql;
109         hashCode = sql.hashCode();
110         opened = true;
111     }
112
113     /**
114      * @return Sql query used.
115      */

116     public String JavaDoc getSql() {
117         return sql;
118     }
119
120     /**
121      * @return hashcode of the object
122      */

123     @Override JavaDoc
124     public int hashCode() {
125         return hashCode;
126     }
127
128     /**
129      * @param stmt given statement for comparing it
130      * @return true if given object is equals to this current object
131      */

132     @Override JavaDoc
133     public boolean equals(final Object JavaDoc stmt) {
134         if (stmt == null) {
135             return false;
136         }
137         // different hashcode, cannot be equals
138
if (this.hashCode != stmt.hashCode()) {
139             return false;
140         }
141
142         // if got same hashcode, try to see if cast is ok.
143
if (!(stmt instanceof JStatement)) {
144             logger.warn("Bad class {0}", stmt);
145             return false;
146         }
147         JStatement psw = (JStatement) stmt;
148         if (sql == null && psw.getSql() != null) {
149             return false;
150         }
151         if (sql != null && !sql.equals(psw.getSql())) {
152             return false;
153         }
154         try {
155             if (psw.getResultSetType() != getResultSetType()) {
156                 return false;
157             }
158             if (psw.getResultSetConcurrency() != getResultSetConcurrency()) {
159                 return false;
160             }
161         } catch (SQLException JavaDoc e) {
162             logger.warn("Cannot compare statements", e);
163             return false;
164         }
165         logger.debug("Found");
166         return true;
167     }
168
169     /**
170      * Force a close on the Prepare Statement. Usually, it's the caller that did
171      * not close it explicitly
172      * @return true if it was open
173      */

174     public boolean forceClose() {
175         if (opened) {
176             logger.debug("Statements should be closed explicitly.");
177             opened = false;
178             return true;
179         }
180         return false;
181     }
182
183     /**
184      * Reuses this statement so reset properties.
185      * @throws SQLException if reset fails
186      */

187     public void reuse() throws SQLException JavaDoc {
188         ps.clearParameters();
189         ps.clearWarnings();
190         opened = true;
191         if (changed) {
192             logger.debug("Properties statement have been changed, reset default properties");
193             ps.clearBatch();
194             ps.setFetchDirection(ResultSet.FETCH_FORWARD);
195             ps.setMaxFieldSize(0);
196             ps.setMaxRows(0);
197             ps.setQueryTimeout(0);
198             changed = false;
199         }
200     }
201
202     /**
203      * @return true if this statement has been closed, else false.
204      */

205     public boolean isClosed() {
206         return !opened && !closing;
207     }
208
209     /**
210      * Physically close this Statement.
211      * @throws SQLException
212      */

213     public void forget() {
214         try {
215             ps.close();
216         } catch (SQLException JavaDoc e) {
217             logger.error("Cannot close the PreparedStatement", e);
218         }
219     }
220
221     /**
222      * {@inheritDoc}
223      */

224     public int executeUpdate() throws SQLException JavaDoc {
225         return ps.executeUpdate();
226     }
227
228     /**
229      * {@inheritDoc}
230      */

231     public void addBatch() throws SQLException JavaDoc {
232         changed = true;
233         ps.addBatch();
234     }
235
236     /**
237      * {@inheritDoc}
238      */

239     public void clearParameters() throws SQLException JavaDoc {
240         ps.clearParameters();
241     }
242
243     /**
244      * {@inheritDoc}
245      */

246     public boolean execute() throws SQLException JavaDoc {
247         return ps.execute();
248     }
249
250     /**
251      * {@inheritDoc}
252      */

253     public void setByte(final int parameterIndex, final byte x) throws SQLException JavaDoc {
254         ps.setByte(parameterIndex, x);
255     }
256
257     /**
258      * {@inheritDoc}
259      */

260     public void setDouble(final int parameterIndex, final double x) throws SQLException JavaDoc {
261         ps.setDouble(parameterIndex, x);
262     }
263
264     /**
265      * {@inheritDoc}
266      */

267     public void setFloat(final int parameterIndex, final float x) throws SQLException JavaDoc {
268         ps.setFloat(parameterIndex, x);
269     }
270
271     /**
272      * {@inheritDoc}
273      */

274     public void setInt(final int parameterIndex, final int x) throws SQLException JavaDoc {
275         ps.setInt(parameterIndex, x);
276     }
277
278     /**
279      * {@inheritDoc}
280      */

281     public void setNull(final int parameterIndex, final int sqlType) throws SQLException JavaDoc {
282         ps.setNull(parameterIndex, sqlType);
283     }
284
285     /**
286      * {@inheritDoc}
287      */

288     public void setLong(final int parameterIndex, final long x) throws SQLException JavaDoc {
289         ps.setLong(parameterIndex, x);
290     }
291
292     /**
293      * {@inheritDoc}
294      */

295     public void setShort(final int parameterIndex, final short x) throws SQLException JavaDoc {
296         ps.setShort(parameterIndex, x);
297     }
298
299     /**
300      * {@inheritDoc}
301      */

302     public void setBoolean(final int parameterIndex, final boolean x) throws SQLException JavaDoc {
303         ps.setBoolean(parameterIndex, x);
304     }
305
306     /**
307      * {@inheritDoc}
308      */

309     public void setBytes(final int parameterIndex, final byte[] x) throws SQLException JavaDoc {
310         ps.setBytes(parameterIndex, x);
311     }
312
313     /**
314      * {@inheritDoc}
315      */

316     public void setAsciiStream(final int parameterIndex, final InputStream JavaDoc x, final int length) throws SQLException JavaDoc {
317         ps.setAsciiStream(parameterIndex, x, length);
318     }
319
320     /**
321      * {@inheritDoc}
322      */

323     public void setBinaryStream(final int parameterIndex, final InputStream JavaDoc x, final int length) throws SQLException JavaDoc {
324         ps.setBinaryStream(parameterIndex, x, length);
325     }
326
327     /**
328      * {@inheritDoc}
329      */

330     @SuppressWarnings JavaDoc("deprecation")
331     public void setUnicodeStream(final int parameterIndex, final InputStream JavaDoc x, final int length) throws SQLException JavaDoc {
332         ps.setUnicodeStream(parameterIndex, x, length);
333     }
334
335     /**
336      * {@inheritDoc}
337      */

338     public void setCharacterStream(final int parameterIndex, final Reader JavaDoc reader, final int length) throws SQLException JavaDoc {
339         ps.setCharacterStream(parameterIndex, reader, length);
340     }
341
342     /**
343      * {@inheritDoc}
344      */

345     public void setObject(final int parameterIndex, final Object JavaDoc x) throws SQLException JavaDoc {
346         ps.setObject(parameterIndex, x);
347     }
348
349     /**
350      * {@inheritDoc}
351      */

352     public void setObject(final int parameterIndex, final Object JavaDoc x, final int targetSqlType) throws SQLException JavaDoc {
353         ps.setObject(parameterIndex, x, targetSqlType);
354     }
355
356     /**
357      * {@inheritDoc}
358      */

359     public void setObject(final int parameterIndex, final Object JavaDoc x,
360             final int targetSqlType, final int scale) throws SQLException JavaDoc {
361         ps.setObject(parameterIndex, x, targetSqlType, scale);
362     }
363
364     /**
365      * {@inheritDoc}
366      */

367     public void setNull(final int paramIndex, final int sqlType, final String JavaDoc typeName) throws SQLException JavaDoc {
368         ps.setNull(paramIndex, sqlType, typeName);
369     }
370
371     /**
372      * {@inheritDoc}
373      */

374     public void setString(final int parameterIndex, final String JavaDoc x) throws SQLException JavaDoc {
375         ps.setString(parameterIndex, x);
376     }
377
378     /**
379      * {@inheritDoc}
380      */

381     public void setBigDecimal(final int parameterIndex, final BigDecimal JavaDoc x) throws SQLException JavaDoc {
382         ps.setBigDecimal(parameterIndex, x);
383     }
384
385     /**
386      * {@inheritDoc}
387      */

388     public void setURL(final int parameterIndex, final URL JavaDoc x) throws SQLException JavaDoc {
389         ps.setURL(parameterIndex, x);
390     }
391
392     /**
393      * {@inheritDoc}
394      */

395     public void setArray(final int i, final Array JavaDoc x) throws SQLException JavaDoc {
396         ps.setArray(i, x);
397     }
398
399     /**
400      * {@inheritDoc}
401      */

402     public void setBlob(final int i, final Blob JavaDoc x) throws SQLException JavaDoc {
403         ps.setBlob(i, x);
404     }
405
406     /**
407      * {@inheritDoc}
408      */

409     public void setClob(final int i, final Clob JavaDoc x) throws SQLException JavaDoc {
410         ps.setClob(i, x);
411     }
412
413     /**
414      * {@inheritDoc}
415      */

416     public void setDate(final int parameterIndex, final Date JavaDoc x) throws SQLException JavaDoc {
417         ps.setDate(parameterIndex, x);
418     }
419
420     /**
421      * {@inheritDoc}
422      */

423     public ParameterMetaData JavaDoc getParameterMetaData() throws SQLException JavaDoc {
424         return ps.getParameterMetaData();
425     }
426
427     /**
428      * {@inheritDoc}
429      */

430     public void setRef(final int i, final Ref JavaDoc x) throws SQLException JavaDoc {
431         ps.setRef(i, x);
432     }
433
434     /**
435      * {@inheritDoc}
436      */

437     public ResultSet JavaDoc executeQuery() throws SQLException JavaDoc {
438         return ps.executeQuery();
439     }
440
441     /**
442      * {@inheritDoc}
443      */

444     public ResultSetMetaData JavaDoc getMetaData() throws SQLException JavaDoc {
445         return ps.getMetaData();
446     }
447
448     /**
449      * {@inheritDoc}
450      */

451     public void setTime(final int parameterIndex, final Time JavaDoc x) throws SQLException JavaDoc {
452         ps.setTime(parameterIndex, x);
453     }
454
455     /**
456      * {@inheritDoc}
457      */

458     public void setTimestamp(final int parameterIndex, final Timestamp JavaDoc x) throws SQLException JavaDoc {
459         ps.setTimestamp(parameterIndex, x);
460     }
461
462     /**
463      * {@inheritDoc}
464      */

465     public void setDate(final int parameterIndex, final Date JavaDoc x, final Calendar JavaDoc cal) throws SQLException JavaDoc {
466         ps.setDate(parameterIndex, x, cal);
467     }
468
469     /**
470      * {@inheritDoc}
471      */

472     public void setTime(final int parameterIndex, final Time JavaDoc x, final Calendar JavaDoc cal) throws SQLException JavaDoc {
473         ps.setTime(parameterIndex, x, cal);
474     }
475
476     /**
477      * {@inheritDoc}
478      */

479     public void setTimestamp(final int parameterIndex, final Timestamp JavaDoc x, final Calendar JavaDoc cal) throws SQLException JavaDoc {
480         ps.setTimestamp(parameterIndex, x, cal);
481     }
482
483     /**
484      * {@inheritDoc}
485      */

486     public int getFetchDirection() throws SQLException JavaDoc {
487         return ps.getFetchDirection();
488     }
489
490     /**
491      * {@inheritDoc}
492      */

493     public int getFetchSize() throws SQLException JavaDoc {
494         return ps.getFetchSize();
495     }
496
497     /**
498      * {@inheritDoc}
499      */

500     public int getMaxFieldSize() throws SQLException JavaDoc {
501         return ps.getMaxFieldSize();
502     }
503
504     /**
505      * {@inheritDoc}
506      */

507     public int getMaxRows() throws SQLException JavaDoc {
508         return ps.getMaxRows();
509     }
510
511     /**
512      * {@inheritDoc}
513      */

514     public int getQueryTimeout() throws SQLException JavaDoc {
515         return ps.getQueryTimeout();
516     }
517
518     /**
519      * {@inheritDoc}
520      */

521     public int getResultSetConcurrency() throws SQLException JavaDoc {
522         return ps.getResultSetConcurrency();
523     }
524
525     /**
526      * {@inheritDoc}
527      */

528     public int getResultSetHoldability() throws SQLException JavaDoc {
529         return ps.getResultSetHoldability();
530     }
531
532     /**
533      * {@inheritDoc}
534      */

535     public int getResultSetType() throws SQLException JavaDoc {
536         return ps.getResultSetType();
537     }
538
539     /**
540      * {@inheritDoc}
541      */

542     public int getUpdateCount() throws SQLException JavaDoc {
543         return ps.getUpdateCount();
544     }
545
546     /**
547      * {@inheritDoc}
548      */

549     public void cancel() throws SQLException JavaDoc {
550         ps.cancel();
551     }
552
553     /**
554      * {@inheritDoc}
555      */

556     public void clearBatch() throws SQLException JavaDoc {
557         ps.clearBatch();
558     }
559
560     /**
561      * {@inheritDoc}
562      */

563     public void clearWarnings() throws SQLException JavaDoc {
564         ps.clearWarnings();
565     }
566
567     /**
568      * {@inheritDoc}
569      */

570     public void close() throws SQLException JavaDoc {
571         if (!opened) {
572             logger.debug("Statement already closed");
573             return;
574         }
575         opened = false;
576         closing = true;
577         mc.notifyPsClose(this);
578         closing = false;
579     }
580
581     /**
582      * {@inheritDoc}
583      */

584     public boolean getMoreResults() throws SQLException JavaDoc {
585         return ps.getMoreResults();
586     }
587
588     /**
589      * {@inheritDoc}
590      */

591     public int[] executeBatch() throws SQLException JavaDoc {
592         return ps.executeBatch();
593     }
594
595     /**
596      * {@inheritDoc}
597      */

598     public void setFetchDirection(final int direction) throws SQLException JavaDoc {
599         changed = true;
600         ps.setFetchDirection(direction);
601     }
602
603     /**
604      * {@inheritDoc}
605      */

606     public void setFetchSize(final int rows) throws SQLException JavaDoc {
607         changed = true;
608         ps.setFetchSize(rows);
609     }
610
611     /**
612      * {@inheritDoc}
613      */

614     public void setMaxFieldSize(final int max) throws SQLException JavaDoc {
615         changed = true;
616         ps.setMaxFieldSize(max);
617     }
618
619     /**
620      * {@inheritDoc}
621      */

622     public void setMaxRows(final int max) throws SQLException JavaDoc {
623         changed = true;
624         ps.setMaxRows(max);
625     }
626
627     /**
628      * {@inheritDoc}
629      */

630     public void setQueryTimeout(final int seconds) throws SQLException JavaDoc {
631         changed = true;
632         ps.setQueryTimeout(seconds);
633     }
634
635     /**
636      * {@inheritDoc}
637      */

638     public boolean getMoreResults(final int current) throws SQLException JavaDoc {
639         return ps.getMoreResults(current);
640     }
641
642     /**
643      * {@inheritDoc}
644      */

645     public void setEscapeProcessing(final boolean enable) throws SQLException JavaDoc {
646         ps.setEscapeProcessing(enable);
647     }
648
649     /**
650      * {@inheritDoc}
651      */

652     public int executeUpdate(final String JavaDoc sql) throws SQLException JavaDoc {
653         return ps.executeUpdate(sql);
654     }
655
656     /**
657      * {@inheritDoc}
658      */

659     public void addBatch(final String JavaDoc sql) throws SQLException JavaDoc {
660         changed = true;
661         ps.addBatch(sql);
662     }
663
664     /**
665      * {@inheritDoc}
666      */

667     public void setCursorName(final String JavaDoc name) throws SQLException JavaDoc {
668         ps.setCursorName(name);
669     }
670
671     /**
672      * {@inheritDoc}
673      */

674     public boolean execute(final String JavaDoc sql) throws SQLException JavaDoc {
675         changed = true;
676         return ps.execute(sql);
677     }
678
679     /**
680      * {@inheritDoc}
681      */

682     public int executeUpdate(final String JavaDoc sql, final int autoGeneratedKeys) throws SQLException JavaDoc {
683         changed = true;
684         return ps.executeUpdate(sql, autoGeneratedKeys);
685     }
686
687     /**
688      * {@inheritDoc}
689      */

690     public boolean execute(final String JavaDoc sql, final int autoGeneratedKeys) throws SQLException JavaDoc {
691         changed = true;
692         return ps.execute(sql, autoGeneratedKeys);
693     }
694
695     /**
696      * {@inheritDoc}
697      */

698     public int executeUpdate(final String JavaDoc sql, final int[] columnIndexes) throws SQLException JavaDoc {
699         changed = true;
700         return ps.executeUpdate(sql, columnIndexes);
701     }
702
703     /**
704      * {@inheritDoc}
705      */

706     public boolean execute(final String JavaDoc sql, final int[] columnIndexes) throws SQLException JavaDoc {
707         changed = true;
708         return ps.execute(sql, columnIndexes);
709     }
710
711     /**
712      * {@inheritDoc}
713      */

714     public Connection JavaDoc getConnection() throws SQLException JavaDoc {
715         return ps.getConnection();
716     }
717
718     /**
719      * {@inheritDoc}
720      */

721     public ResultSet JavaDoc getGeneratedKeys() throws SQLException JavaDoc {
722         return ps.getGeneratedKeys();
723     }
724
725     /**
726      * {@inheritDoc}
727      */

728     public ResultSet JavaDoc getResultSet() throws SQLException JavaDoc {
729         return ps.getResultSet();
730     }
731
732     /**
733      * {@inheritDoc}
734      */

735     public SQLWarning JavaDoc getWarnings() throws SQLException JavaDoc {
736         return ps.getWarnings();
737     }
738
739     /**
740      * {@inheritDoc}
741      */

742     public int executeUpdate(final String JavaDoc sql, final String JavaDoc[] columnNames) throws SQLException JavaDoc {
743         return ps.executeUpdate(sql, columnNames);
744     }
745
746     /**
747      * {@inheritDoc}
748      */

749     public boolean execute(final String JavaDoc sql, final String JavaDoc[] columnNames) throws SQLException JavaDoc {
750         return ps.execute(sql, columnNames);
751     }
752
753     /**
754      * {@inheritDoc}
755      */

756     public ResultSet JavaDoc executeQuery(final String JavaDoc sql) throws SQLException JavaDoc {
757         return ps.executeQuery(sql);
758     }
759
760 }
761
Popular Tags