KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > minerva > pool > jdbc > xa > wrapper > XAClientConnection


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

4 package org.ofbiz.minerva.pool.jdbc.xa.wrapper;
5
6 import java.io.ByteArrayOutputStream JavaDoc;
7 import java.io.PrintStream JavaDoc;
8 import java.sql.CallableStatement JavaDoc;
9 import java.sql.Connection JavaDoc;
10 import java.sql.DatabaseMetaData JavaDoc;
11 import java.sql.PreparedStatement JavaDoc;
12 import java.sql.SQLException JavaDoc;
13 import java.sql.SQLWarning JavaDoc;
14 import java.sql.Savepoint JavaDoc;
15 import java.sql.Statement JavaDoc;
16 import java.util.Collection JavaDoc;
17 import java.util.HashSet JavaDoc;
18 import java.util.Iterator JavaDoc;
19 import java.util.Map JavaDoc;
20 import java.util.Vector JavaDoc;
21
22 import org.ofbiz.minerva.pool.PoolEvent;
23 import org.ofbiz.minerva.pool.cache.LeastRecentlyUsedCache;
24 import org.ofbiz.minerva.pool.cache.ObjectCache;
25 import org.ofbiz.minerva.pool.jdbc.ConnectionInPool;
26 import org.ofbiz.minerva.pool.jdbc.ConnectionWrapper;
27 import org.ofbiz.minerva.pool.jdbc.PreparedStatementFactory;
28 import org.ofbiz.minerva.pool.jdbc.PreparedStatementInPool;
29 import org.ofbiz.minerva.pool.jdbc.StatementInPool;
30
31 import org.apache.log4j.Logger;
32
33 /**
34  * Wrapper for database connections used by an XAConnection. When close is
35  * called, it does not close the underlying connection, just informs the
36  * XAConnection that close was called. The connection will not be closed (or
37  * returned to the pool) until the transactional details are taken care of.
38  * This instance only lives as long as one client is using it - though we
39  * probably want to consider reusing it to save object allocations.
40  *
41  * @author Aaron Mulder (ammulder@alumni.princeton.edu)
42  */

43 public class XAClientConnection implements ConnectionWrapper {
44
45     private final static String JavaDoc CLOSED = "Connection has been closed!";
46
47     private Connection JavaDoc con;
48     private HashSet JavaDoc statements;
49     private Vector JavaDoc listeners;
50     private XAConnectionImpl xaCon;
51     private int preparedStatementCacheSize = 0;
52     private ObjectCache preparedStatementCache;
53     private String JavaDoc stackTrace = null;
54     private static Logger log = Logger.getLogger(XADataSourceImpl.class);
55
56     /**
57      * Creates a new connection wrapper.
58      * @param xaCon The handler for all the transactional details.
59      * @param con The "real" database connection to wrap.
60      */

61     public XAClientConnection(XAConnectionImpl xaCon, Connection JavaDoc con, boolean saveStackTrace) {
62         this.con = con;
63         this.xaCon = xaCon;
64         preparedStatementCache = (ObjectCache) ConnectionInPool.psCaches.get(con);
65         if (preparedStatementCache == null) {
66             PreparedStatementFactory factory = new PreparedStatementFactory(con);
67             preparedStatementCache = new LeastRecentlyUsedCache(factory, preparedStatementCacheSize);
68             ConnectionInPool.psCaches.put(con, preparedStatementCache);
69         }
70         statements = new HashSet JavaDoc();
71         listeners = new Vector JavaDoc();
72         if (saveStackTrace) {
73             try {
74                 ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
75                 PrintStream JavaDoc stream = new PrintStream JavaDoc(baos);
76                 new Throwable JavaDoc().printStackTrace(stream);
77                 baos.close();
78                 stackTrace = baos.toString();
79             } catch (Exception JavaDoc ex) {
80             }
81         }
82     }
83
84     /**
85      * Sets the number of PreparedStatements to be cached for each
86      * Connection. Your DB product may impose a limit on the number
87      * of open PreparedStatements.
88      */

89     public void setPSCacheSize(int maxSize) {
90         preparedStatementCacheSize = maxSize;
91         if (preparedStatementCache != null) {
92             preparedStatementCache.setSize(maxSize);
93         }
94     }
95
96     /**
97      * Gets the number of PreparedStatements to be cached for each
98      * Connection.
99      */

100     public int getPSCacheSize() {
101         return preparedStatementCacheSize;
102     }
103
104     /**
105      * Gets a reference to the "real" connection. This should only be used if
106      * you need to cast that to a specific type to call a proprietary method -
107      * you will defeat all the pooling if you use the underlying connection
108      * directly.
109      */

110     public Connection JavaDoc getUnderlyingConnection() {
111         return con;
112     }
113
114     /**
115      * Closes this connection wrapper permanently. All further calls with throw
116      * a SQLException.
117      */

118     public void shutdown() {
119         con = null;
120         statements = null;
121         listeners = null;
122         xaCon = null;
123     }
124
125     /**
126      * Updates the last used time for this connection to the current time.
127      * This is not used by the current implementation.
128      */

129     public void setLastUsed() {
130         xaCon.firePoolEvent(new PoolEvent(xaCon, PoolEvent.OBJECT_USED));
131     }
132
133     /**
134      * Indicates that an error occured on this connection.
135      */

136     public void setError(SQLException JavaDoc e) {
137         xaCon.setConnectionError(e);
138     }
139
140     /**
141      * Indicates that a statement has been closed and no longer needs to be
142      * tracked. Outstanding statements are closed when the connection is
143      * returned to the pool.
144      */

145     public void statementClosed(Statement JavaDoc st) {
146         statements.remove(st);
147         if ((con != null) && (st instanceof PreparedStatementInPool) && preparedStatementCacheSize != 0) {
148
149             // Now return the "real" statement to the pool
150
PreparedStatementInPool ps = (PreparedStatementInPool) st;
151             PreparedStatement JavaDoc ups = ps.getUnderlyingPreparedStatement();
152             preparedStatementCache.returnObject(ps.getSql(), ups);
153 /*
154             int rsType = ResultSet.TYPE_FORWARD_ONLY;
155             int rsConcur = ResultSet.CONCUR_READ_ONLY;
156
157             // We may have JDBC 1.0 driver
158             try {
159                 rsType = ups.getResultSetType();
160                 rsConcur = ups.getResultSetConcurrency();
161             } catch (Throwable th) {
162             }
163             PreparedStatementInPool.preparedStatementCache.put(
164                     new PSCacheKey(con, ps.getSql(), rsType, rsConcur), ups);
165 */

166         }
167     }
168
169     // ---- Implementation of java.sql.Connection ----
170
public Statement JavaDoc createStatement() throws SQLException JavaDoc {
171         if (con == null) throw new SQLException JavaDoc(CLOSED);
172         try {
173             StatementInPool st = new StatementInPool(con.createStatement(), this);
174             statements.add(st);
175             return st;
176         } catch (SQLException JavaDoc e) {
177             setError(e);
178             throw e;
179         }
180     }
181
182     public PreparedStatement JavaDoc prepareStatement(String JavaDoc sql) throws SQLException JavaDoc {
183         if (con == null) throw new SQLException JavaDoc(CLOSED);
184         try {
185             PreparedStatement JavaDoc ps;
186             if (preparedStatementCacheSize == 0) {
187                 // cache disabled
188
ps = con.prepareStatement(sql);
189             } else {
190                 ps = (PreparedStatement JavaDoc) preparedStatementCache.useObject(sql);
191             }
192             if (ps == null)
193                 throw new SQLException JavaDoc("Unable to create PreparedStatement!");
194             PreparedStatementInPool wrapper = new PreparedStatementInPool(ps, this, sql);
195             statements.add(wrapper);
196             return wrapper;
197         } catch (SQLException JavaDoc e) {
198             setError(e);
199             throw e;
200         }
201     }
202
203     public CallableStatement JavaDoc prepareCall(String JavaDoc sql) throws SQLException JavaDoc {
204         if (con == null) throw new SQLException JavaDoc(CLOSED);
205         try {
206             return con.prepareCall(sql);
207         } catch (SQLException JavaDoc e) {
208             setError(e);
209             throw e;
210         }
211     }
212
213     public String JavaDoc nativeSQL(String JavaDoc sql) throws SQLException JavaDoc {
214         if (con == null) throw new SQLException JavaDoc(CLOSED);
215         try {
216             return con.nativeSQL(sql);
217         } catch (SQLException JavaDoc e) {
218             setError(e);
219             throw e;
220         }
221     }
222
223     public void setAutoCommit(boolean autoCommit) throws SQLException JavaDoc {
224         if (con == null) throw new SQLException JavaDoc(CLOSED);
225         if (((XAResourceImpl) xaCon.getXAResource()).isTransaction() && autoCommit)
226             throw new SQLException JavaDoc("Cannot set AutoCommit for a transactional connection: See JDBC 2.0 Optional Package Specification section 7.1 (p25)");
227
228         try {
229             con.setAutoCommit(autoCommit);
230         } catch (SQLException JavaDoc e) {
231             setError(e);
232             throw e;
233         }
234
235     }
236
237     public boolean getAutoCommit() throws SQLException JavaDoc {
238         if (con == null) throw new SQLException JavaDoc(CLOSED);
239         try {
240             return con.getAutoCommit();
241         } catch (SQLException JavaDoc e) {
242             setError(e);
243             throw e;
244         }
245     }
246
247     public void commit() throws SQLException JavaDoc {
248         if (con == null) throw new SQLException JavaDoc(CLOSED);
249         if (((XAResourceImpl) xaCon.getXAResource()).isTransaction())
250             throw new SQLException JavaDoc("Cannot commit a transactional connection: See JDBC 2.0 Optional Package Specification section 7.1 (p25)");
251         try {
252             con.commit();
253         } catch (SQLException JavaDoc e) {
254             setError(e);
255             throw e;
256         }
257     }
258
259     public void rollback() throws SQLException JavaDoc {
260         if (con == null) throw new SQLException JavaDoc(CLOSED);
261         if (((XAResourceImpl) xaCon.getXAResource()).isTransaction())
262             throw new SQLException JavaDoc("Cannot rollback a transactional connection: See JDBC 2.0 Optional Package Specification section 7.1 (p25)");
263     }
264
265     public void forcedClose() throws SQLException JavaDoc {
266         if (stackTrace != null)
267             System.err.println("A forced close because a non-closed connection:\n" + stackTrace);
268         if (con == null) throw new SQLException JavaDoc(CLOSED);
269         Collection JavaDoc copy = (Collection JavaDoc) statements.clone();
270         Iterator JavaDoc it = copy.iterator();
271         while (it.hasNext())
272             try {
273                 ((Statement JavaDoc) it.next()).close();
274             } catch (SQLException JavaDoc e) {
275             }
276         shutdown();
277     }
278
279     public void close() throws SQLException JavaDoc {
280         if (con == null) throw new SQLException JavaDoc(CLOSED);
281         Collection JavaDoc copy = (Collection JavaDoc) statements.clone();
282         Iterator JavaDoc it = copy.iterator();
283         while (it.hasNext())
284             try {
285                 ((Statement JavaDoc) it.next()).close();
286             } catch (SQLException JavaDoc e) {
287                 log.warn("SQLException : ", e);
288             }
289
290         xaCon.clientConnectionClosed(this);
291         shutdown();
292     }
293
294     public boolean isClosed() throws SQLException JavaDoc {
295         if (con == null) return true;
296         try {
297             return con.isClosed();
298         } catch (SQLException JavaDoc e) {
299             setError(e);
300             throw e;
301         }
302     }
303
304     public DatabaseMetaData JavaDoc getMetaData() throws SQLException JavaDoc {
305         if (con == null) throw new SQLException JavaDoc(CLOSED);
306         try {
307             return con.getMetaData();
308         } catch (SQLException JavaDoc e) {
309             setError(e);
310             throw e;
311         }
312     }
313
314     public void setReadOnly(boolean readOnly) throws SQLException JavaDoc {
315         if (con == null) throw new SQLException JavaDoc(CLOSED);
316         try {
317             con.setReadOnly(readOnly);
318         } catch (SQLException JavaDoc e) {
319             setError(e);
320             throw e;
321         }
322     }
323
324     public boolean isReadOnly() throws SQLException JavaDoc {
325         if (con == null) throw new SQLException JavaDoc(CLOSED);
326         try {
327             return con.isReadOnly();
328         } catch (SQLException JavaDoc e) {
329             setError(e);
330             throw e;
331         }
332     }
333
334     public void setCatalog(String JavaDoc catalog) throws SQLException JavaDoc {
335         if (con == null) throw new SQLException JavaDoc(CLOSED);
336         try {
337             con.setCatalog(catalog);
338         } catch (SQLException JavaDoc e) {
339             setError(e);
340             throw e;
341         }
342     }
343
344     public String JavaDoc getCatalog() throws SQLException JavaDoc {
345         if (con == null) throw new SQLException JavaDoc(CLOSED);
346         try {
347             return con.getCatalog();
348         } catch (SQLException JavaDoc e) {
349             setError(e);
350             throw e;
351         }
352     }
353
354     public void setTransactionIsolation(int level) throws SQLException JavaDoc {
355         if (con == null) throw new SQLException JavaDoc(CLOSED);
356         try {
357             con.setTransactionIsolation(level);
358         } catch (SQLException JavaDoc e) {
359             setError(e);
360             throw e;
361         }
362     }
363
364     public int getTransactionIsolation() throws SQLException JavaDoc {
365         if (con == null) throw new SQLException JavaDoc(CLOSED);
366         try {
367             return con.getTransactionIsolation();
368         } catch (SQLException JavaDoc e) {
369             setError(e);
370             throw e;
371         }
372     }
373
374     public SQLWarning JavaDoc getWarnings() throws SQLException JavaDoc {
375         if (con == null) throw new SQLException JavaDoc(CLOSED);
376         try {
377             return con.getWarnings();
378         } catch (SQLException JavaDoc e) {
379             setError(e);
380             throw e;
381         }
382     }
383
384     public void clearWarnings() throws SQLException JavaDoc {
385         if (con == null) throw new SQLException JavaDoc(CLOSED);
386         try {
387             con.clearWarnings();
388         } catch (SQLException JavaDoc e) {
389             setError(e);
390             throw e;
391         }
392     }
393
394     public Statement JavaDoc createStatement(int resultSetType, int resultSetConcurrency) throws SQLException JavaDoc {
395         if (con == null) throw new SQLException JavaDoc(CLOSED);
396         try {
397             StatementInPool st = new StatementInPool(con.createStatement(resultSetType, resultSetConcurrency), this);
398             statements.add(st);
399             return st;
400         } catch (SQLException JavaDoc e) {
401             setError(e);
402             throw e;
403         }
404     }
405
406     public PreparedStatement JavaDoc prepareStatement(String JavaDoc sql, int resultSetType, int resultSetConcurrency) throws SQLException JavaDoc {
407         if (con == null) throw new SQLException JavaDoc(CLOSED);
408         try {
409             return con.prepareStatement(sql, resultSetType, resultSetConcurrency);
410         } catch (SQLException JavaDoc e) {
411             setError(e);
412             throw e;
413         }
414     }
415
416     public CallableStatement JavaDoc prepareCall(String JavaDoc sql, int resultSetType, int resultSetConcurrency) throws SQLException JavaDoc {
417         if (con == null) throw new SQLException JavaDoc(CLOSED);
418         try {
419             return con.prepareCall(sql, resultSetType, resultSetConcurrency);
420         } catch (SQLException JavaDoc e) {
421             setError(e);
422             throw e;
423         }
424     }
425
426     public Map JavaDoc getTypeMap() throws SQLException JavaDoc {
427         if (con == null) throw new SQLException JavaDoc(CLOSED);
428         try {
429             return con.getTypeMap();
430         } catch (SQLException JavaDoc e) {
431             setError(e);
432             throw e;
433         }
434     }
435
436     public void setTypeMap(Map JavaDoc map) throws SQLException JavaDoc {
437         if (con == null) throw new SQLException JavaDoc(CLOSED);
438         try {
439             con.setTypeMap(map);
440         } catch (SQLException JavaDoc e) {
441             setError(e);
442             throw e;
443         }
444     }
445
446     // JDK 1.4 methods
447

448     /* (non-Javadoc)
449      * @see java.sql.Connection#setHoldability(int)
450      */

451     public void setHoldability(int arg0) throws SQLException JavaDoc {
452         throw new SQLException JavaDoc("Method not implemented!");
453
454     }
455
456     /* (non-Javadoc)
457      * @see java.sql.Connection#getHoldability()
458      */

459     public int getHoldability() throws SQLException JavaDoc {
460         throw new SQLException JavaDoc("Method not implemented!");
461     }
462
463     /* (non-Javadoc)
464      * @see java.sql.Connection#setSavepoint()
465      */

466     public Savepoint JavaDoc setSavepoint() throws SQLException JavaDoc {
467         throw new SQLException JavaDoc("Method not implemented!");
468     }
469
470     /* (non-Javadoc)
471      * @see java.sql.Connection#setSavepoint(java.lang.String)
472      */

473     public Savepoint JavaDoc setSavepoint(String JavaDoc arg0) throws SQLException JavaDoc {
474         throw new SQLException JavaDoc("Method not implemented!");
475     }
476
477     /* (non-Javadoc)
478      * @see java.sql.Connection#rollback(java.sql.Savepoint)
479      */

480     public void rollback(Savepoint JavaDoc arg0) throws SQLException JavaDoc {
481         throw new SQLException JavaDoc("Method not implemented!");
482
483     }
484
485     /* (non-Javadoc)
486      * @see java.sql.Connection#releaseSavepoint(java.sql.Savepoint)
487      */

488     public void releaseSavepoint(Savepoint JavaDoc arg0) throws SQLException JavaDoc {
489         throw new SQLException JavaDoc("Method not implemented!");
490
491     }
492
493     /* (non-Javadoc)
494      * @see java.sql.Connection#createStatement(int, int, int)
495      */

496     public Statement JavaDoc createStatement(int arg0, int arg1, int arg2) throws SQLException JavaDoc {
497         throw new SQLException JavaDoc("Method not implemented!");
498     }
499
500     /* (non-Javadoc)
501      * @see java.sql.Connection#prepareStatement(java.lang.String, int, int, int)
502      */

503     public PreparedStatement JavaDoc prepareStatement(String JavaDoc arg0, int arg1, int arg2, int arg3) throws SQLException JavaDoc {
504         throw new SQLException JavaDoc("Method not implemented!");
505     }
506
507     /* (non-Javadoc)
508      * @see java.sql.Connection#prepareCall(java.lang.String, int, int, int)
509      */

510     public CallableStatement JavaDoc prepareCall(String JavaDoc arg0, int arg1, int arg2, int arg3) throws SQLException JavaDoc {
511         throw new SQLException JavaDoc("Method not implemented!");
512     }
513
514     /* (non-Javadoc)
515      * @see java.sql.Connection#prepareStatement(java.lang.String, int)
516      */

517     public PreparedStatement JavaDoc prepareStatement(String JavaDoc arg0, int arg1) throws SQLException JavaDoc {
518         throw new SQLException JavaDoc("Method not implemented!");
519     }
520
521     /* (non-Javadoc)
522      * @see java.sql.Connection#prepareStatement(java.lang.String, int[])
523      */

524     public PreparedStatement JavaDoc prepareStatement(String JavaDoc arg0, int[] arg1) throws SQLException JavaDoc {
525         throw new SQLException JavaDoc("Method not implemented!");
526     }
527
528     /* (non-Javadoc)
529      * @see java.sql.Connection#prepareStatement(java.lang.String, java.lang.String[])
530      */

531     public PreparedStatement JavaDoc prepareStatement(String JavaDoc arg0, String JavaDoc[] arg1) throws SQLException JavaDoc {
532         throw new SQLException JavaDoc("Method not implemented!");
533     }
534 }
535
Popular Tags