001: /*
002: * Licensed under the X license (see http://www.x.org/terms.htm)
003: */
004: package org.ofbiz.minerva.pool.jdbc;
005:
006: import java.sql.Connection;
007: import java.sql.ResultSet;
008: import java.sql.Statement;
009: import java.sql.SQLException;
010: import java.sql.SQLWarning;
011:
012: /**
013: * Wraps a Statement to track errors and the last used time for the owning
014: * connection. That time is updated every time a SQL action is performed
015: * (executeQuery, executeUpdate, etc.).
016: *
017: * @author Aaron Mulder (ammulder@alumni.princeton.edu)
018: */
019: public class StatementInPool implements Statement {
020:
021: private final static String CLOSED = "Statement has been closed!";
022: private Statement impl;
023: private ConnectionWrapper con;
024:
025: /**
026: * Creates a new statement from a source statement and wrapper connection.
027: */
028: public StatementInPool(Statement source, ConnectionWrapper owner) {
029: if (source == null || owner == null)
030: throw new NullPointerException();
031: impl = source;
032: con = owner;
033: }
034:
035: /**
036: * Updates the last used time for the owning connection to the current time.
037: */
038: public void setLastUsed() {
039: if (con != null)
040: con.setLastUsed();
041: }
042:
043: /**
044: * Indicates that an error occured on the owning connection.
045: */
046: public void setError(SQLException e) {
047: if (con != null)
048: con.setError(e);
049: }
050:
051: /**
052: * Gets a reference to the "real" Statement. This should only be used if
053: * you need to cast that to a specific type to call a proprietary method -
054: * you will defeat all the pooling if you use the underlying Statement
055: * directly.
056: */
057: public Statement getUnderlyingStatement() {
058: return impl;
059: }
060:
061: // ---- Implementation of java.sql.Statement ----
062:
063: public void addBatch(String arg0) throws SQLException {
064: if (impl == null)
065: throw new SQLException(CLOSED);
066: try {
067: impl.addBatch(arg0);
068: } catch (SQLException e) {
069: setError(e);
070: throw e;
071: }
072: }
073:
074: public void cancel() throws SQLException {
075: if (impl == null)
076: throw new SQLException(CLOSED);
077: try {
078: impl.cancel();
079: } catch (SQLException e) {
080: setError(e);
081: throw e;
082: }
083: }
084:
085: public void clearBatch() throws SQLException {
086: if (impl == null)
087: throw new SQLException(CLOSED);
088: try {
089: impl.clearBatch();
090: } catch (SQLException e) {
091: setError(e);
092: throw e;
093: }
094: }
095:
096: public void clearWarnings() throws SQLException {
097: if (impl == null)
098: throw new SQLException(CLOSED);
099: try {
100: impl.clearWarnings();
101: } catch (SQLException e) {
102: setError(e);
103: throw e;
104: }
105: }
106:
107: public void close() throws SQLException {
108: if (impl != null) {
109: impl.close();
110: con.statementClosed(this );
111: }
112: clearFields();
113: }
114:
115: protected void clearFields() {
116: con = null;
117: impl = null;
118: }
119:
120: public boolean execute(String arg0) throws SQLException {
121: if (impl == null)
122: throw new SQLException(CLOSED);
123: try {
124: setLastUsed();
125: return impl.execute(arg0);
126: } catch (SQLException e) {
127: setError(e);
128: throw e;
129: }
130: }
131:
132: public int[] executeBatch() throws SQLException {
133: if (impl == null)
134: throw new SQLException(CLOSED);
135: try {
136: setLastUsed();
137: return impl.executeBatch();
138: } catch (SQLException e) {
139: setError(e);
140: throw e;
141: }
142: }
143:
144: public ResultSet executeQuery(String arg0) throws SQLException {
145: if (impl == null)
146: throw new SQLException(CLOSED);
147: try {
148: setLastUsed();
149: return new ResultSetInPool(impl.executeQuery(arg0), this );
150: } catch (SQLException e) {
151: setError(e);
152: throw e;
153: }
154: }
155:
156: public int executeUpdate(String arg0) throws SQLException {
157: if (impl == null)
158: throw new SQLException(CLOSED);
159: try {
160: setLastUsed();
161: return impl.executeUpdate(arg0);
162: } catch (SQLException e) {
163: setError(e);
164: throw e;
165: }
166: }
167:
168: public Connection getConnection() throws SQLException {
169: if (impl == null)
170: throw new SQLException(CLOSED);
171: return con;
172: }
173:
174: public int getFetchDirection() throws SQLException {
175: if (impl == null)
176: throw new SQLException(CLOSED);
177: try {
178: return impl.getFetchDirection();
179: } catch (SQLException e) {
180: setError(e);
181: throw e;
182: }
183: }
184:
185: public int getFetchSize() throws SQLException {
186: if (impl == null)
187: throw new SQLException(CLOSED);
188: try {
189: return impl.getFetchSize();
190: } catch (SQLException e) {
191: setError(e);
192: throw e;
193: }
194: }
195:
196: public int getMaxFieldSize() throws SQLException {
197: if (impl == null)
198: throw new SQLException(CLOSED);
199: try {
200: return impl.getMaxFieldSize();
201: } catch (SQLException e) {
202: setError(e);
203: throw e;
204: }
205: }
206:
207: public int getMaxRows() throws SQLException {
208: if (impl == null)
209: throw new SQLException(CLOSED);
210: try {
211: return impl.getMaxRows();
212: } catch (SQLException e) {
213: setError(e);
214: throw e;
215: }
216: }
217:
218: public boolean getMoreResults() throws SQLException {
219: if (impl == null)
220: throw new SQLException(CLOSED);
221: try {
222: return impl.getMoreResults();
223: } catch (SQLException e) {
224: setError(e);
225: throw e;
226: }
227: }
228:
229: public int getQueryTimeout() throws SQLException {
230: if (impl == null)
231: throw new SQLException(CLOSED);
232: try {
233: return impl.getQueryTimeout();
234: } catch (SQLException e) {
235: setError(e);
236: throw e;
237: }
238: }
239:
240: public ResultSet getResultSet() throws SQLException {
241: if (impl == null)
242: throw new SQLException(CLOSED);
243: try {
244: ResultSet rs = impl.getResultSet();
245: return rs == null ? null : new ResultSetInPool(rs, this );
246: } catch (SQLException e) {
247: setError(e);
248: throw e;
249: }
250: }
251:
252: public int getResultSetConcurrency() throws SQLException {
253: if (impl == null)
254: throw new SQLException(CLOSED);
255: try {
256: return impl.getResultSetConcurrency();
257: } catch (SQLException e) {
258: setError(e);
259: throw e;
260: }
261: }
262:
263: public int getResultSetType() throws SQLException {
264: if (impl == null)
265: throw new SQLException(CLOSED);
266: try {
267: return impl.getResultSetType();
268: } catch (SQLException e) {
269: setError(e);
270: throw e;
271: }
272: }
273:
274: public int getUpdateCount() throws SQLException {
275: if (impl == null)
276: throw new SQLException(CLOSED);
277: try {
278: return impl.getUpdateCount();
279: } catch (SQLException e) {
280: setError(e);
281: throw e;
282: }
283: }
284:
285: public SQLWarning getWarnings() throws SQLException {
286: if (impl == null)
287: throw new SQLException(CLOSED);
288: try {
289: return impl.getWarnings();
290: } catch (SQLException e) {
291: setError(e);
292: throw e;
293: }
294: }
295:
296: public void setCursorName(String arg0) throws SQLException {
297: if (impl == null)
298: throw new SQLException(CLOSED);
299: try {
300: impl.setCursorName(arg0);
301: } catch (SQLException e) {
302: setError(e);
303: throw e;
304: }
305: }
306:
307: public void setEscapeProcessing(boolean arg0) throws SQLException {
308: if (impl == null)
309: throw new SQLException(CLOSED);
310: try {
311: impl.setEscapeProcessing(arg0);
312: } catch (SQLException e) {
313: setError(e);
314: throw e;
315: }
316: }
317:
318: public void setFetchDirection(int arg0) throws SQLException {
319: if (impl == null)
320: throw new SQLException(CLOSED);
321: try {
322: impl.setFetchDirection(arg0);
323: } catch (SQLException e) {
324: setError(e);
325: throw e;
326: }
327: }
328:
329: public void setFetchSize(int arg0) throws SQLException {
330: if (impl == null)
331: throw new SQLException(CLOSED);
332: try {
333: impl.setFetchSize(arg0);
334: } catch (SQLException e) {
335: setError(e);
336: throw e;
337: }
338: }
339:
340: public void setMaxFieldSize(int arg0) throws SQLException {
341: if (impl == null)
342: throw new SQLException(CLOSED);
343: try {
344: impl.setMaxFieldSize(arg0);
345: } catch (SQLException e) {
346: setError(e);
347: throw e;
348: }
349: }
350:
351: public void setMaxRows(int arg0) throws SQLException {
352: if (impl == null)
353: throw new SQLException(CLOSED);
354: try {
355: impl.setMaxRows(arg0);
356: } catch (SQLException e) {
357: setError(e);
358: throw e;
359: }
360: }
361:
362: public void setQueryTimeout(int arg0) throws SQLException {
363: if (impl == null)
364: throw new SQLException(CLOSED);
365: try {
366: impl.setQueryTimeout(arg0);
367: } catch (SQLException e) {
368: setError(e);
369: throw e;
370: }
371: }
372:
373: // ------- J2SE 1.4 methods comment; needed to compile -------
374:
375: /* (non-Javadoc)
376: * @see java.sql.Statement#getMoreResults(int)
377: */
378: public boolean getMoreResults(int arg0) throws SQLException {
379: // TODO Auto-generated method stub
380: return false;
381: }
382:
383: /* (non-Javadoc)
384: * @see java.sql.Statement#getGeneratedKeys()
385: */
386: public ResultSet getGeneratedKeys() throws SQLException {
387: // TODO Auto-generated method stub
388: return null;
389: }
390:
391: /* (non-Javadoc)
392: * @see java.sql.Statement#executeUpdate(java.lang.String, int)
393: */
394: public int executeUpdate(String arg0, int arg1) throws SQLException {
395: // TODO Auto-generated method stub
396: return 0;
397: }
398:
399: /* (non-Javadoc)
400: * @see java.sql.Statement#executeUpdate(java.lang.String, int[])
401: */
402: public int executeUpdate(String arg0, int[] arg1)
403: throws SQLException {
404: // TODO Auto-generated method stub
405: return 0;
406: }
407:
408: /* (non-Javadoc)
409: * @see java.sql.Statement#executeUpdate(java.lang.String, java.lang.String[])
410: */
411: public int executeUpdate(String arg0, String[] arg1)
412: throws SQLException {
413: // TODO Auto-generated method stub
414: return 0;
415: }
416:
417: /* (non-Javadoc)
418: * @see java.sql.Statement#execute(java.lang.String, int)
419: */
420: public boolean execute(String arg0, int arg1) throws SQLException {
421: // TODO Auto-generated method stub
422: return false;
423: }
424:
425: /* (non-Javadoc)
426: * @see java.sql.Statement#execute(java.lang.String, int[])
427: */
428: public boolean execute(String arg0, int[] arg1) throws SQLException {
429: // TODO Auto-generated method stub
430: return false;
431: }
432:
433: /* (non-Javadoc)
434: * @see java.sql.Statement#execute(java.lang.String, java.lang.String[])
435: */
436: public boolean execute(String arg0, String[] arg1)
437: throws SQLException {
438: // TODO Auto-generated method stub
439: return false;
440: }
441:
442: /* (non-Javadoc)
443: * @see java.sql.Statement#getResultSetHoldability()
444: */
445: public int getResultSetHoldability() throws SQLException {
446: // TODO Auto-generated method stub
447: return 0;
448: }
449:
450: // ---- End Implementation of Statement ----
451: }
|