KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lateralnz > simpletrans > TransConnection


1 /* ====================================================================
2  * The LateralNZ Software License, Version 1.0
3  *
4  * Copyright (c) 2003 LateralNZ. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  *
18  * 3. The end-user documentation included with the redistribution,
19  * if any, must include the following acknowledgment:
20  * "This product includes software developed by
21  * LateralNZ (http://www.lateralnz.org/) and other third parties."
22  * Alternately, this acknowledgment may appear in the software itself,
23  * if and wherever such third-party acknowledgments normally appear.
24  *
25  * 4. The names "LateralNZ" must not be used to endorse or promote
26  * products derived from this software without prior written
27  * permission. For written permission, please
28  * contact oss@lateralnz.org.
29  *
30  * 5. Products derived from this software may not be called "Panther",
31  * or "Lateral" or "LateralNZ", nor may "PANTHER" or "LATERAL" or
32  * "LATERALNZ" appear in their name, without prior written
33  * permission of LateralNZ.
34  *
35  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
36  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
37  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
39  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
42  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
43  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
44  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
45  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
46  * SUCH DAMAGE.
47  * ====================================================================
48  *
49  * This software consists of voluntary contributions made by many
50  * individuals on behalf of LateralNZ. For more
51  * information on Lateral, please see http://www.lateralnz.com/ or
52  * http://www.lateralnz.org
53  *
54  */

55 package org.lateralnz.simpletrans;
56
57 import java.io.Serializable JavaDoc;
58 import java.sql.*;
59 import java.util.Map JavaDoc;
60
61 import javax.transaction.xa.XAResource JavaDoc;
62 import javax.transaction.xa.XAException JavaDoc;
63
64 import org.apache.log4j.Logger;
65
66 import org.lateralnz.common.util.DAOUtils;
67
68 /**
69  * a 'transaction-aware' wrapper for a JDBC connection. This is used to ensure
70  * that a connection is only commited, rolled back or closed until required
71  * by a transaction
72  *
73  * @author J R Briggs
74  */

75 public class TransConnection implements Connection, XAResource JavaDoc, Serializable JavaDoc {
76   private static final Logger log = Logger.getLogger(TransConnection.class.getName());
77
78   private String JavaDoc name;
79   private Connection conn;
80   private boolean close = false;
81   private boolean inTrans = false;
82   
83   private long tempTime;
84   private java.util.ArrayList JavaDoc statements = null;
85   
86  /**
87   * wrap a JDBC connection with the specified name
88   */

89   public TransConnection(String JavaDoc name, Connection conn, boolean inTrans) throws SQLException {
90     this.name = name;
91     this.conn = conn;
92     this.inTrans = inTrans;
93     this.conn.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
94     
95 /* JRB 15/09/2004: this isn't needed by the look of things. setAutoCommit to false
96  * seems to already start the transaction (which is done in enlistResource in the
97  * transaction) -- at least in the postgres driver. Need to test with other
98  * databases to confirm this.
99  
100     Statement st = conn.createStatement();
101     try {
102       st.executeUpdate("start transaction");
103     }
104     finally {
105       DAOUtils.close(st);
106     }*/

107     statements = new java.util.ArrayList JavaDoc();
108     if (log.isDebugEnabled()) {
109       tempTime = System.currentTimeMillis();
110       log.debug("created new transaction connection for jdbc conn " + conn);
111     }
112   }
113   
114  /**
115   * get the name of this connection
116   */

117   public String JavaDoc getName() {
118     return name;
119   }
120   
121  /**
122   * @see java.sql.Connection#clearWarnings
123   */

124   public void clearWarnings() throws SQLException {
125     conn.clearWarnings();
126   }
127
128  /**
129   * if not in a transaction, this closes the connection immediately,
130   * otherwise just flags that a close is required
131   * @see java.sql.Connection#close
132   */

133   public void close() throws SQLException {
134     if (!inTrans) {
135       doClose();
136     }
137     else {
138       close = true;
139     }
140   }
141   
142  /**
143   * check if close has been flagged on this connection
144   */

145   public boolean isCloseFlagged() {
146     return close;
147   }
148   
149  /**
150   * performs the actual close on the underlying connection
151   */

152   protected void doClose() throws SQLException {
153     if (log.isDebugEnabled()) {
154       log.debug("closing connection " + conn);
155       tempTime = System.currentTimeMillis() - tempTime;
156       if (tempTime > 15000) {
157         log.debug("time between opening and closing conn " + conn + " was " + tempTime + " after performing " + statements.toString());
158       }
159     }
160     conn.close();
161   }
162   
163  /**
164   * if not in a transaction, a commit is performed immediately,
165   * otherwise nothing happens
166   * @see java.sql.Connection#commit
167   */

168   public void commit() throws SQLException {
169     if (!inTrans) {
170       doCommit();
171     }
172   }
173   
174  /**
175   * performs the actual commit on the underlying connection
176   */

177   public void doCommit() throws SQLException {
178     conn.commit();
179   }
180   
181  /**
182   * @see java.sql.Connection#createStatement
183   */

184   public Statement createStatement() throws SQLException {
185     return conn.createStatement();
186   }
187   
188  /**
189   * @see java.sql.Connection#createStatement
190   */

191   public Statement createStatement(int param, int param1) throws SQLException {
192     return conn.createStatement(param, param1);
193   }
194
195  /**
196   * @see java.sql.Connection#createStatement
197   */

198   public Statement createStatement(int param, int param1, int param2) throws SQLException {
199     return conn.createStatement(param, param1, param2);
200   }
201   
202  /**
203   * @see java.sql.Connection#getAutoCommit
204   */

205   public boolean getAutoCommit() throws SQLException {
206     return conn.getAutoCommit();
207   }
208   
209  /**
210   * @see java.sql.Connection#getCatalog
211   */

212   public String JavaDoc getCatalog() throws SQLException {
213     return conn.getCatalog();
214   }
215   
216  /**
217   * @see java.sql.Connection#getHoldability
218   */

219   public int getHoldability() throws SQLException {
220     return conn.getHoldability();
221   }
222   
223  /**
224   * @see java.sql.Connection#getMetaData
225   */

226   public DatabaseMetaData getMetaData() throws SQLException {
227     return conn.getMetaData();
228   }
229   
230  /**
231   * @see java.sql.Connection#getTransactionIsolation
232   */

233   public int getTransactionIsolation() throws SQLException {
234     return conn.getTransactionIsolation();
235   }
236   
237  /**
238   * @see java.sql.Connection#getTypeMap
239   */

240   public Map JavaDoc getTypeMap() throws SQLException {
241     return conn.getTypeMap();
242   }
243   
244  /**
245   * @see java.sql.Connection#getWarnings
246   */

247   public SQLWarning getWarnings() throws SQLException {
248     return conn.getWarnings();
249   }
250   
251  /**
252   * @see java.sql.Connection#isClosed
253   */

254   public boolean isClosed() throws SQLException {
255     return conn.isClosed();
256   }
257   
258  /**
259   * @see java.sql.Connection#isReadOnly
260   */

261   public boolean isReadOnly() throws SQLException {
262     return conn.isReadOnly();
263   }
264   
265  /**
266   * @see java.sql.Connection#nativeSQL
267   */

268   public String JavaDoc nativeSQL(String JavaDoc str) throws SQLException {
269     return conn.nativeSQL(str);
270   }
271   
272  /**
273   * @see java.sql.Connection#prepareCall
274   */

275   public CallableStatement prepareCall(String JavaDoc str) throws SQLException {
276     return conn.prepareCall(str);
277   }
278   
279  /**
280   * @see java.sql.Connection#prepareCall
281   */

282   public CallableStatement prepareCall(String JavaDoc str, int param, int param2) throws SQLException {
283     return conn.prepareCall(str, param, param2);
284   }
285   
286  /**
287   * @see java.sql.Connection#prepareCall
288   */

289   public CallableStatement prepareCall(String JavaDoc str, int param, int param2, int param3) throws SQLException {
290     return conn.prepareCall(str, param, param2, param3);
291   }
292   
293  /**
294   * @see java.sql.Connection#prepareStatement
295   */

296   public PreparedStatement prepareStatement(String JavaDoc str) throws SQLException {
297     statements.add(str);
298     return conn.prepareStatement(str);
299   }
300
301  /**
302   * @see java.sql.Connection#prepareStatement
303   */

304   public PreparedStatement prepareStatement(String JavaDoc str, int param) throws SQLException {
305     return conn.prepareStatement(str, param);
306   }
307   
308  /**
309   * @see java.sql.Connection#prepareStatement
310   */

311   public PreparedStatement prepareStatement(String JavaDoc str, int[] values) throws SQLException {
312     return conn.prepareStatement(str, values);
313   }
314   
315  /**
316   * @see java.sql.Connection#prepareStatement
317   */

318   public PreparedStatement prepareStatement(String JavaDoc str, String JavaDoc[] str1) throws SQLException {
319     return conn.prepareStatement(str, str1);
320   }
321   
322  /**
323   * @see java.sql.Connection#prepareStatement
324   */

325   public PreparedStatement prepareStatement(String JavaDoc str, int param, int param2) throws SQLException {
326     return conn.prepareStatement(str, param, param2);
327   }
328   
329  /**
330   * @see java.sql.Connection#prepareStatement
331   */

332   public PreparedStatement prepareStatement(String JavaDoc str, int param, int param2, int param3) throws SQLException {
333     return conn.prepareStatement(str, param, param2, param3);
334   }
335   
336  /**
337   * @see java.sql.Connection#releaseSavepoint
338   */

339   public void releaseSavepoint(Savepoint savepoint) throws SQLException {
340     conn.releaseSavepoint(savepoint);
341   }
342   
343  /**
344   * if not in a transaction, this rolls back the underlying connection immediately,
345   * otherwise nothing is done
346   * @see java.sql.Connection#rollback
347   */

348   public void rollback() throws SQLException {
349     if (!inTrans) {
350       doRollback(null);
351     }
352   }
353   
354  /**
355   * if not in a transaction, this rolls back the underlying connection immediately,
356   * otherwise nothing is done
357   * @see java.sql.Connection#rollback
358   */

359   public void rollback(Savepoint savepoint) throws SQLException {
360     if (!inTrans) {
361       doRollback(savepoint);
362     }
363   }
364
365  /**
366   * performs the actual rollback on the underlying Connection
367   * @see java.sql.Connection#rollback
368   */

369   public void doRollback(Savepoint savepoint) throws SQLException {
370     if (savepoint == null) {
371       conn.rollback();
372     }
373     else {
374       conn.rollback(savepoint);
375     }
376   }
377   
378  /**
379   * @see java.sql.Connection#setAutoCommit
380   */

381   public void setAutoCommit(boolean param) throws SQLException {
382     conn.setAutoCommit(param);
383   }
384   
385  /**
386   * @see java.sql.Connection#setCatalog
387   */

388   public void setCatalog(String JavaDoc str) throws SQLException {
389     conn.setCatalog(str);
390   }
391   
392  /**
393   * @see java.sql.Connection#setHoldability
394   */

395   public void setHoldability(int param) throws SQLException {
396     conn.setHoldability(param);
397   }
398   
399  /**
400   * @see java.sql.Connection#setReadOnly
401   */

402   public void setReadOnly(boolean param) throws SQLException {
403     conn.setReadOnly(param);
404   }
405   
406  /**
407   * @see java.sql.Connection#setSavepoint
408   */

409   public Savepoint setSavepoint() throws SQLException {
410     return conn.setSavepoint();
411   }
412   
413  /**
414   * @see java.sql.Connection#setSavepoint
415   */

416   public Savepoint setSavepoint(String JavaDoc str) throws SQLException {
417     return conn.setSavepoint(str);
418   }
419   
420  /**
421   * @see java.sql.Connection#setTransactionIsolation
422   */

423   public void setTransactionIsolation(int param) throws SQLException {
424     conn.setTransactionIsolation(param);
425   }
426   
427  /**
428   * @see java.sql.Connection#setTypeMap
429   */

430   public void setTypeMap(Map JavaDoc map) throws SQLException {
431     conn.setTypeMap(map);
432   }
433   
434   
435   /** these methods need implementation **/
436   
437   public void commit(javax.transaction.xa.Xid JavaDoc xid, boolean param) throws XAException JavaDoc {
438     throw new UnsupportedOperationException JavaDoc();
439   }
440   
441   public void end(javax.transaction.xa.Xid JavaDoc xid, int param) throws XAException JavaDoc {
442     throw new UnsupportedOperationException JavaDoc();
443   }
444   
445   public void forget(javax.transaction.xa.Xid JavaDoc xid) throws XAException JavaDoc {
446     throw new UnsupportedOperationException JavaDoc();
447   }
448   
449   public int getTransactionTimeout() throws XAException JavaDoc {
450     throw new UnsupportedOperationException JavaDoc();
451   }
452   
453   public boolean isSameRM(javax.transaction.xa.XAResource JavaDoc xAResource) throws XAException JavaDoc {
454     throw new UnsupportedOperationException JavaDoc();
455   }
456   
457   public int prepare(javax.transaction.xa.Xid JavaDoc xid) throws XAException JavaDoc {
458     return XAResource.XA_OK;
459   }
460   
461   public javax.transaction.xa.Xid JavaDoc[] recover(int param) throws XAException JavaDoc {
462     throw new UnsupportedOperationException JavaDoc();
463   }
464   
465   public void rollback(javax.transaction.xa.Xid JavaDoc xid) throws XAException JavaDoc {
466     try {
467       this.rollback();
468     }
469     catch (SQLException se) {
470       throw new XAException JavaDoc(se.getMessage());
471     }
472   }
473   
474   public boolean setTransactionTimeout(int param) throws XAException JavaDoc {
475     throw new UnsupportedOperationException JavaDoc();
476   }
477   
478   public void start(javax.transaction.xa.Xid JavaDoc xid, int param) throws XAException JavaDoc {
479     throw new UnsupportedOperationException JavaDoc();
480   }
481   
482 }
Popular Tags