KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > minerva > pool > jdbc > StatementInPool


1 /*
2  * Licensed under the X license (see http://www.x.org/terms.htm)
3  */

4 package org.ofbiz.minerva.pool.jdbc;
5
6 import java.sql.Connection JavaDoc;
7 import java.sql.ResultSet JavaDoc;
8 import java.sql.Statement JavaDoc;
9 import java.sql.SQLException JavaDoc;
10 import java.sql.SQLWarning JavaDoc;
11
12
13 /**
14  * Wraps a Statement to track errors and the last used time for the owning
15  * connection. That time is updated every time a SQL action is performed
16  * (executeQuery, executeUpdate, etc.).
17  *
18  * @author Aaron Mulder (ammulder@alumni.princeton.edu)
19  */

20 public class StatementInPool implements Statement JavaDoc {
21
22     private final static String JavaDoc CLOSED = "Statement has been closed!";
23     private Statement JavaDoc impl;
24     private ConnectionWrapper con;
25
26     /**
27      * Creates a new statement from a source statement and wrapper connection.
28      */

29     public StatementInPool(Statement JavaDoc source, ConnectionWrapper owner) {
30         if (source == null || owner == null) throw new NullPointerException JavaDoc();
31         impl = source;
32         con = owner;
33     }
34
35     /**
36      * Updates the last used time for the owning connection to the current time.
37      */

38     public void setLastUsed() {
39         if (con != null)
40             con.setLastUsed();
41     }
42
43     /**
44      * Indicates that an error occured on the owning connection.
45      */

46     public void setError(SQLException JavaDoc e) {
47         if (con != null)
48             con.setError(e);
49     }
50
51     /**
52      * Gets a reference to the "real" Statement. This should only be used if
53      * you need to cast that to a specific type to call a proprietary method -
54      * you will defeat all the pooling if you use the underlying Statement
55      * directly.
56      */

57     public Statement JavaDoc getUnderlyingStatement() {
58         return impl;
59     }
60
61     // ---- Implementation of java.sql.Statement ----
62

63     public void addBatch(String JavaDoc arg0) throws SQLException JavaDoc {
64         if (impl == null) throw new SQLException JavaDoc(CLOSED);
65         try {
66             impl.addBatch(arg0);
67         } catch (SQLException JavaDoc e) {
68             setError(e);
69             throw e;
70         }
71     }
72
73     public void cancel() throws SQLException JavaDoc {
74         if (impl == null) throw new SQLException JavaDoc(CLOSED);
75         try {
76             impl.cancel();
77         } catch (SQLException JavaDoc e) {
78             setError(e);
79             throw e;
80         }
81     }
82
83     public void clearBatch() throws SQLException JavaDoc {
84         if (impl == null) throw new SQLException JavaDoc(CLOSED);
85         try {
86             impl.clearBatch();
87         } catch (SQLException JavaDoc e) {
88             setError(e);
89             throw e;
90         }
91     }
92
93     public void clearWarnings() throws SQLException JavaDoc {
94         if (impl == null) throw new SQLException JavaDoc(CLOSED);
95         try {
96             impl.clearWarnings();
97         } catch (SQLException JavaDoc e) {
98             setError(e);
99             throw e;
100         }
101     }
102
103     public void close() throws SQLException JavaDoc {
104         if (impl != null) {
105             impl.close();
106             con.statementClosed(this);
107         }
108         clearFields();
109     }
110
111     protected void clearFields() {
112         con = null;
113         impl = null;
114     }
115
116     public boolean execute(String JavaDoc arg0) throws SQLException JavaDoc {
117         if (impl == null) throw new SQLException JavaDoc(CLOSED);
118         try {
119             setLastUsed();
120             return impl.execute(arg0);
121         } catch (SQLException JavaDoc e) {
122             setError(e);
123             throw e;
124         }
125     }
126
127     public int[] executeBatch() throws SQLException JavaDoc {
128         if (impl == null) throw new SQLException JavaDoc(CLOSED);
129         try {
130             setLastUsed();
131             return impl.executeBatch();
132         } catch (SQLException JavaDoc e) {
133             setError(e);
134             throw e;
135         }
136     }
137
138     public ResultSet JavaDoc executeQuery(String JavaDoc arg0) throws SQLException JavaDoc {
139         if (impl == null) throw new SQLException JavaDoc(CLOSED);
140         try {
141             setLastUsed();
142             return new ResultSetInPool(impl.executeQuery(arg0), this);
143         } catch (SQLException JavaDoc e) {
144             setError(e);
145             throw e;
146         }
147     }
148
149     public int executeUpdate(String JavaDoc arg0) throws SQLException JavaDoc {
150         if (impl == null) throw new SQLException JavaDoc(CLOSED);
151         try {
152             setLastUsed();
153             return impl.executeUpdate(arg0);
154         } catch (SQLException JavaDoc e) {
155             setError(e);
156             throw e;
157         }
158     }
159
160     public Connection JavaDoc getConnection() throws SQLException JavaDoc {
161         if (impl == null) throw new SQLException JavaDoc(CLOSED);
162         return con;
163     }
164
165     public int getFetchDirection() throws SQLException JavaDoc {
166         if (impl == null) throw new SQLException JavaDoc(CLOSED);
167         try {
168             return impl.getFetchDirection();
169         } catch (SQLException JavaDoc e) {
170             setError(e);
171             throw e;
172         }
173     }
174
175     public int getFetchSize() throws SQLException JavaDoc {
176         if (impl == null) throw new SQLException JavaDoc(CLOSED);
177         try {
178             return impl.getFetchSize();
179         } catch (SQLException JavaDoc e) {
180             setError(e);
181             throw e;
182         }
183     }
184
185     public int getMaxFieldSize() throws SQLException JavaDoc {
186         if (impl == null) throw new SQLException JavaDoc(CLOSED);
187         try {
188             return impl.getMaxFieldSize();
189         } catch (SQLException JavaDoc e) {
190             setError(e);
191             throw e;
192         }
193     }
194
195     public int getMaxRows() throws SQLException JavaDoc {
196         if (impl == null) throw new SQLException JavaDoc(CLOSED);
197         try {
198             return impl.getMaxRows();
199         } catch (SQLException JavaDoc e) {
200             setError(e);
201             throw e;
202         }
203     }
204
205     public boolean getMoreResults() throws SQLException JavaDoc {
206         if (impl == null) throw new SQLException JavaDoc(CLOSED);
207         try {
208             return impl.getMoreResults();
209         } catch (SQLException JavaDoc e) {
210             setError(e);
211             throw e;
212         }
213     }
214
215     public int getQueryTimeout() throws SQLException JavaDoc {
216         if (impl == null) throw new SQLException JavaDoc(CLOSED);
217         try {
218             return impl.getQueryTimeout();
219         } catch (SQLException JavaDoc e) {
220             setError(e);
221             throw e;
222         }
223     }
224
225     public ResultSet JavaDoc getResultSet() throws SQLException JavaDoc {
226         if (impl == null) throw new SQLException JavaDoc(CLOSED);
227         try {
228             ResultSet JavaDoc rs = impl.getResultSet();
229             return rs == null ? null : new ResultSetInPool(rs, this);
230         } catch (SQLException JavaDoc e) {
231             setError(e);
232             throw e;
233         }
234     }
235
236     public int getResultSetConcurrency() throws SQLException JavaDoc {
237         if (impl == null) throw new SQLException JavaDoc(CLOSED);
238         try {
239             return impl.getResultSetConcurrency();
240         } catch (SQLException JavaDoc e) {
241             setError(e);
242             throw e;
243         }
244     }
245
246     public int getResultSetType() throws SQLException JavaDoc {
247         if (impl == null) throw new SQLException JavaDoc(CLOSED);
248         try {
249             return impl.getResultSetType();
250         } catch (SQLException JavaDoc e) {
251             setError(e);
252             throw e;
253         }
254     }
255
256     public int getUpdateCount() throws SQLException JavaDoc {
257         if (impl == null) throw new SQLException JavaDoc(CLOSED);
258         try {
259             return impl.getUpdateCount();
260         } catch (SQLException JavaDoc e) {
261             setError(e);
262             throw e;
263         }
264     }
265
266     public SQLWarning JavaDoc getWarnings() throws SQLException JavaDoc {
267         if (impl == null) throw new SQLException JavaDoc(CLOSED);
268         try {
269             return impl.getWarnings();
270         } catch (SQLException JavaDoc e) {
271             setError(e);
272             throw e;
273         }
274     }
275
276     public void setCursorName(String JavaDoc arg0) throws SQLException JavaDoc {
277         if (impl == null) throw new SQLException JavaDoc(CLOSED);
278         try {
279             impl.setCursorName(arg0);
280         } catch (SQLException JavaDoc e) {
281             setError(e);
282             throw e;
283         }
284     }
285
286     public void setEscapeProcessing(boolean arg0) throws SQLException JavaDoc {
287         if (impl == null) throw new SQLException JavaDoc(CLOSED);
288         try {
289             impl.setEscapeProcessing(arg0);
290         } catch (SQLException JavaDoc e) {
291             setError(e);
292             throw e;
293         }
294     }
295
296     public void setFetchDirection(int arg0) throws SQLException JavaDoc {
297         if (impl == null) throw new SQLException JavaDoc(CLOSED);
298         try {
299             impl.setFetchDirection(arg0);
300         } catch (SQLException JavaDoc e) {
301             setError(e);
302             throw e;
303         }
304     }
305
306     public void setFetchSize(int arg0) throws SQLException JavaDoc {
307         if (impl == null) throw new SQLException JavaDoc(CLOSED);
308         try {
309             impl.setFetchSize(arg0);
310         } catch (SQLException JavaDoc e) {
311             setError(e);
312             throw e;
313         }
314     }
315
316     public void setMaxFieldSize(int arg0) throws SQLException JavaDoc {
317         if (impl == null) throw new SQLException JavaDoc(CLOSED);
318         try {
319             impl.setMaxFieldSize(arg0);
320         } catch (SQLException JavaDoc e) {
321             setError(e);
322             throw e;
323         }
324     }
325
326     public void setMaxRows(int arg0) throws SQLException JavaDoc {
327         if (impl == null) throw new SQLException JavaDoc(CLOSED);
328         try {
329             impl.setMaxRows(arg0);
330         } catch (SQLException JavaDoc e) {
331             setError(e);
332             throw e;
333         }
334     }
335
336     public void setQueryTimeout(int arg0) throws SQLException JavaDoc {
337         if (impl == null) throw new SQLException JavaDoc(CLOSED);
338         try {
339             impl.setQueryTimeout(arg0);
340         } catch (SQLException JavaDoc e) {
341             setError(e);
342             throw e;
343         }
344     }
345
346
347     // ------- J2SE 1.4 methods comment; needed to compile -------
348

349     /* (non-Javadoc)
350      * @see java.sql.Statement#getMoreResults(int)
351      */

352     public boolean getMoreResults(int arg0) throws SQLException JavaDoc {
353         // TODO Auto-generated method stub
354
return false;
355     }
356
357     /* (non-Javadoc)
358      * @see java.sql.Statement#getGeneratedKeys()
359      */

360     public ResultSet JavaDoc getGeneratedKeys() throws SQLException JavaDoc {
361         // TODO Auto-generated method stub
362
return null;
363     }
364
365     /* (non-Javadoc)
366      * @see java.sql.Statement#executeUpdate(java.lang.String, int)
367      */

368     public int executeUpdate(String JavaDoc arg0, int arg1) throws SQLException JavaDoc {
369         // TODO Auto-generated method stub
370
return 0;
371     }
372
373     /* (non-Javadoc)
374      * @see java.sql.Statement#executeUpdate(java.lang.String, int[])
375      */

376     public int executeUpdate(String JavaDoc arg0, int[] arg1) throws SQLException JavaDoc {
377         // TODO Auto-generated method stub
378
return 0;
379     }
380
381     /* (non-Javadoc)
382      * @see java.sql.Statement#executeUpdate(java.lang.String, java.lang.String[])
383      */

384     public int executeUpdate(String JavaDoc arg0, String JavaDoc[] arg1) throws SQLException JavaDoc {
385         // TODO Auto-generated method stub
386
return 0;
387     }
388
389     /* (non-Javadoc)
390      * @see java.sql.Statement#execute(java.lang.String, int)
391      */

392     public boolean execute(String JavaDoc arg0, int arg1) throws SQLException JavaDoc {
393         // TODO Auto-generated method stub
394
return false;
395     }
396
397     /* (non-Javadoc)
398      * @see java.sql.Statement#execute(java.lang.String, int[])
399      */

400     public boolean execute(String JavaDoc arg0, int[] arg1) throws SQLException JavaDoc {
401         // TODO Auto-generated method stub
402
return false;
403     }
404
405     /* (non-Javadoc)
406      * @see java.sql.Statement#execute(java.lang.String, java.lang.String[])
407      */

408     public boolean execute(String JavaDoc arg0, String JavaDoc[] arg1) throws SQLException JavaDoc {
409         // TODO Auto-generated method stub
410
return false;
411     }
412
413     /* (non-Javadoc)
414      * @see java.sql.Statement#getResultSetHoldability()
415      */

416     public int getResultSetHoldability() throws SQLException JavaDoc {
417         // TODO Auto-generated method stub
418
return 0;
419     }
420
421     // ---- End Implementation of Statement ----
422
}
423
Popular Tags