KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.Iterator JavaDoc;
58 import java.util.HashMap JavaDoc;
59 import java.util.Map JavaDoc;
60 import java.sql.SQLException JavaDoc;
61
62 import javax.transaction.Status JavaDoc;
63 import javax.transaction.Synchronization JavaDoc;
64 import javax.transaction.Transaction JavaDoc;
65 import javax.transaction.xa.XAResource JavaDoc;
66
67 import org.apache.log4j.Logger;
68
69 import org.lateralnz.common.util.DAOUtils;
70
71 /**
72  * a 'simple' transaction storing a map of named 'transaction-aware' connections. This class
73  * partially implements a javax.transaction.Transaction, but adds proprietary (to this package)
74  * methods for adding TransConnection objects.
75  * Note also that this isn't a true implementation of a transaction (by any stretch of
76  * the imagination). If two connections have been enlisted from different databases
77  * and a commit succeeds on one, then fails on the other, your data will (obviously) end up
78  * in an inconsistent state.
79  *
80  * @see org.lateralnz.panther.trans.TransConnection
81  *
82  * @author J R Briggs
83  */

84 public class SimpleTransaction implements Transaction JavaDoc {
85   private static final Logger log = Logger.getLogger(SimpleTransaction.class.getName());
86
87   private Map JavaDoc connMap = new HashMap JavaDoc(); // the map of trans connections
88
private int status; // the status of this transaction
89

90  /**
91   * create a new transaction and make it active
92   */

93   public SimpleTransaction() {
94     if (log.isDebugEnabled()) {
95       log.debug("starting new transaction");
96     }
97     status = Status.STATUS_ACTIVE;
98   }
99   
100  /**
101   * commit a transaction, and close any connections that have been flagged for
102   * closing
103   */

104   public void commit() {
105     status = Status.STATUS_COMMITTING;
106     Iterator JavaDoc iter = connMap.values().iterator();
107     while (iter.hasNext()) {
108       TransConnection conn = (TransConnection)iter.next();
109       try {
110         conn.doCommit();
111         
112         if (conn.isCloseFlagged()) {
113           conn.doClose();
114           iter.remove();
115         }
116       }
117       catch (SQLException JavaDoc se) {
118         se.printStackTrace();
119       }
120     }
121     status = Status.STATUS_COMMITTED;
122   }
123   
124   public boolean delistResource(XAResource JavaDoc xAResource, int param) {
125     TransConnection conn = (TransConnection)xAResource;
126     DAOUtils.close(conn);
127     connMap.remove(conn.getName());
128     return true;
129
130   }
131
132
133   public boolean enlistResource(XAResource JavaDoc xAResource) {
134     if (!(xAResource instanceof TransConnection)) {
135       throw new UnsupportedOperationException JavaDoc();
136     }
137     try {
138       TransConnection conn = (TransConnection)xAResource;
139       conn.setAutoCommit(false);
140       connMap.put(conn.getName(), conn);
141       return true;
142     }
143     catch (SQLException JavaDoc se) {
144       se.printStackTrace();
145       return false;
146     }
147   }
148
149   
150  /**
151   * get a resource by name
152   */

153   public Object JavaDoc getResource(String JavaDoc name) {
154     if (connMap.containsKey(name)) {
155       return connMap.get(name);
156     }
157     else {
158       return null;
159     }
160   }
161   
162  /**
163   * get the status of this transaction
164   */

165   public int getStatus() {
166     return status;
167   }
168   
169  /**
170   * not supported
171   */

172   public void registerSynchronization(Synchronization JavaDoc synchronization) {
173     throw new UnsupportedOperationException JavaDoc();
174   }
175   
176  /**
177   * rollback the connections in this transaction
178   */

179   public void rollback() {
180     status = Status.STATUS_ROLLING_BACK;
181     Iterator JavaDoc iter = connMap.values().iterator();
182     while (iter.hasNext()) {
183       TransConnection conn = (TransConnection)iter.next();
184       try {
185         if (log.isDebugEnabled()) {
186           log.debug("rollback connection " + conn);
187         }
188         conn.doRollback(null);
189
190         if (conn.isCloseFlagged()) {
191           if (log.isDebugEnabled()) {
192             log.debug("close flagged, so closing connection");
193           }
194           conn.doClose();
195           iter.remove();
196         }
197       }
198       catch (SQLException JavaDoc se) {
199         se.printStackTrace();
200       }
201     }
202     status = Status.STATUS_ROLLEDBACK;
203   }
204   
205  /**
206   * flag that a rollback is required
207   */

208   public void setRollbackOnly() {
209     status = Status.STATUS_MARKED_ROLLBACK;
210   }
211 }
Popular Tags