KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > easybeans > tests > common > ejbs > stateful > beanmanaged > transaction > SFSBBeanManagedTransaction


1 /**
2  * EasyBeans
3  * Copyright (C) 2006 Bull S.A.S.
4  * Contact: easybeans@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: SFSBBeanManagedTransaction.java 808 2006-07-03 13:58:29Z pinheirg $
23  * --------------------------------------------------------------------------
24  */

25 package org.objectweb.easybeans.tests.common.ejbs.stateful.beanmanaged.transaction;
26
27 import java.sql.SQLException JavaDoc;
28
29 import javax.annotation.Resource;
30 import javax.ejb.Remote JavaDoc;
31 import javax.ejb.SessionContext JavaDoc;
32 import javax.ejb.Stateful JavaDoc;
33 import javax.ejb.TransactionManagement JavaDoc;
34 import javax.ejb.TransactionManagementType JavaDoc;
35 import javax.naming.NamingException JavaDoc;
36 import javax.transaction.HeuristicMixedException JavaDoc;
37 import javax.transaction.HeuristicRollbackException JavaDoc;
38 import javax.transaction.NotSupportedException JavaDoc;
39 import javax.transaction.RollbackException JavaDoc;
40 import javax.transaction.Status JavaDoc;
41 import javax.transaction.SystemException JavaDoc;
42 import javax.transaction.UserTransaction JavaDoc;
43
44 import org.objectweb.easybeans.tests.common.db.TableManager;
45 import org.objectweb.easybeans.tests.common.exception.TransactionException;
46 import org.objectweb.easybeans.tests.common.helper.TransactionHelper;
47
48 /**
49  * Inserts the table test in the database. This class uses usertransactions for
50  * making the operations.
51  * @author Gisele Pinheiro Souza
52  * @author Eduardo Studzinski Estima de Castro
53  */

54 @Stateful JavaDoc(name="SFSBBeanManagedTransaction")
55 @Remote JavaDoc(ItfBeanManagedTransaction.class)
56 @TransactionManagement JavaDoc(value = TransactionManagementType.BEAN)
57 public class SFSBBeanManagedTransaction implements ItfBeanManagedTransaction {
58
59     /**
60      * The bean sessionContext.
61      */

62     @Resource
63     private SessionContext JavaDoc sessionContext;
64
65     /**
66      * Transaction used during the database operations.
67      */

68     private UserTransaction JavaDoc utx = null;
69
70     /**
71      * Class used to create the tables.
72      */

73     private TableManager tableManager = null;
74
75     /**
76      * Says if the table must be created. Used to tests if the container accepts
77      * the transaction order.
78      */

79     private boolean bolOnlyCreateTrans = false;
80
81     /**
82      * Deletes the table called test. The bean transaction is started and
83      * commited in this method.
84      * @throws SQLException if a database error occurs.
85      * @throws NamingException if a lookup error occurs.
86      * @throws SystemException if an unexpected error occurs.
87      * @throws NotSupportedException if the resquest cannot be made.
88      * @throws HeuristicRollbackException if a heuristic decision was made and
89      * some relevant update was rolled back.
90      * @throws RollbackException if the transaction was rolled back instead of
91      * committed.
92      * @throws HeuristicMixedException if a heuristic decision was made and some
93      * relevant update was commited and others rolled back.
94      * @throws TransactionException if a rollback was executed.
95      */

96     public void dropTableWithBeginCommitTransaction() throws SQLException JavaDoc, NamingException JavaDoc, SystemException JavaDoc,
97             NotSupportedException JavaDoc, HeuristicRollbackException JavaDoc, RollbackException JavaDoc, HeuristicMixedException JavaDoc,
98             TransactionException {
99         utx.begin();
100         try {
101             if (!bolOnlyCreateTrans) {
102                 tableManager.deleteTable(TABLE);
103             }
104             utx.commit();
105         } catch (Exception JavaDoc e) {
106             utx.rollback();
107             throw new TransactionException("Error during commit.", e);
108         }
109
110     }
111
112     /**
113      * Deletes the table called test. The transaction is not open, but it is
114      * commited in this method.
115      * @throws SQLException if a database error occurs.
116      * @throws NamingException if a lookup error occurs.
117      * @throws SystemException if an unexpected error occurs.
118      * @throws NotSupportedException if the resquest cannot be made.
119      * @throws HeuristicRollbackException if a heuristic decision was made and
120      * some relevant update was rolled back.
121      * @throws RollbackException if the transaction was rolled back instead of
122      * committed.
123      * @throws HeuristicMixedException if a heuristic decision was made and some
124      * relevant update was commited and others rolled back.
125      * @throws TransactionException if a rollback was executed.
126      */

127     public void dropTableWithoutBeginTransaction() throws SQLException JavaDoc, NamingException JavaDoc, SystemException JavaDoc,
128             NotSupportedException JavaDoc, HeuristicRollbackException JavaDoc, RollbackException JavaDoc, HeuristicMixedException JavaDoc,
129             TransactionException {
130         try {
131             if (!bolOnlyCreateTrans) {
132                 tableManager.deleteTable(TABLE);
133             }
134             utx.commit();
135         } catch (Exception JavaDoc e) {
136             utx.rollback();
137             throw new TransactionException("Error during commit.", e);
138         }
139     }
140
141     /**
142      * Inserts the table called test. The transaction is open and commited in
143      * this method.
144      * @throws SQLException if a database error occurs.
145      * @throws NamingException if a lookup error occurs.
146      * @throws SystemException if an unexpected error occurs.
147      * @throws NotSupportedException if the resquest cannot be made.
148      * @throws HeuristicRollbackException if a heuristic decision was made and
149      * some relevant update was rolled back.
150      * @throws RollbackException if the transaction was rolled back instead of
151      * committed.
152      * @throws HeuristicMixedException if a heuristic decision was made and some
153      * relevant update was commited and others rolled back.
154      * @throws TransactionException if a rollback was executed.
155      */

156     public void insertTableWithBeginCommitTransaction() throws SQLException JavaDoc, NamingException JavaDoc, SystemException JavaDoc,
157             NotSupportedException JavaDoc, HeuristicRollbackException JavaDoc, RollbackException JavaDoc, HeuristicMixedException JavaDoc,
158             TransactionException {
159
160         utx.begin();
161         try {
162             if (!bolOnlyCreateTrans) {
163                 tableManager.insertTable(TABLE);
164             }
165             utx.commit();
166         } catch (Exception JavaDoc e) {
167             utx.rollback();
168             throw new TransactionException("Error during commit.", e);
169         }
170     }
171
172     /**
173      * Inserts the table called test. The transaction will be open, but it will not
174      * be commited in this method.
175      * @throws SQLException if a database error occurs.
176      * @throws NamingException if a lookup error occurs.
177      * @throws SystemException if an unexpected error occurs.
178      * @throws NotSupportedException if the resquest cannot be made.
179      */

180     public void insertTableWithoutCommitTransaction() throws SQLException JavaDoc, NamingException JavaDoc, SystemException JavaDoc,
181             NotSupportedException JavaDoc {
182         utx.begin();
183         if (!bolOnlyCreateTrans) {
184            tableManager.insertTable(TABLE);
185         }
186     }
187
188     /**
189      * Creates the table called test.The bean transaction is started and
190      * commited in this method and, also, there is a nested transaction.
191      * @throws SQLException if a database error occurs.
192      * @throws NamingException if a lookup error occurs.
193      * @throws SystemException if an unexpected error occurs.
194      * @throws NotSupportedException if the resquest cannot be made.
195      * @throws HeuristicRollbackException if a heuristic decision was made and
196      * some relevant update was rolled back.
197      * @throws RollbackException if the transaction was rolled back instead of
198      * committed.
199      * @throws HeuristicMixedException if a heuristic decision was made and some
200      * relevant update was commited and others rolled back.
201      * @throws TransactionException if a rollback was executed.
202      */

203     public void insertTableWithNestedTrans() throws SQLException JavaDoc, NamingException JavaDoc, SystemException JavaDoc,
204             NotSupportedException JavaDoc, HeuristicRollbackException JavaDoc, RollbackException JavaDoc, HeuristicMixedException JavaDoc,
205             TransactionException {
206         // does a begin in the bean transaction
207
utx.begin();
208         // creates a new transaction
209
UserTransaction JavaDoc utxNested = TransactionHelper.getUserTransaction();
210
211         // does a begin in other transaction
212
utxNested.begin();
213         // creates the table
214
if (!bolOnlyCreateTrans) {
215             tableManager.insertTable(TABLE);
216         }
217         // does the commit in the nested transaction
218
utxNested.commit();
219         // does the commit in the bean transaction
220
utx.commit();
221
222     }
223
224     /**
225      * Creates the table called test.A new transaction is created and it makes
226      * the begin and commit transaction. The bean transaction is not started or
227      * commited in this method.
228      * @throws SQLException if a database error occurs.
229      * @throws NamingException if a lookup error occurs.
230      * @throws SystemException if an unexpected error occurs.
231      * @throws NotSupportedException if the resquest cannot be made.
232      * @throws HeuristicRollbackException if a heuristic decision was made and
233      * some relevant update was rolled back.
234      * @throws RollbackException if the transaction was rolled back instead of
235      * committed.
236      * @throws HeuristicMixedException if a heuristic decision was made and some
237      * relevant update was commited and others rolled back.
238      * @throws TransactionException if a rollback was executed.
239      */

240     public void insertTableWithNewTransaction() throws SQLException JavaDoc, NamingException JavaDoc, SystemException JavaDoc,
241             NotSupportedException JavaDoc, HeuristicRollbackException JavaDoc, RollbackException JavaDoc, HeuristicMixedException JavaDoc,
242             TransactionException {
243         // creates a new transaction
244
UserTransaction JavaDoc utxNested = TransactionHelper.getUserTransaction();
245
246         // does a begin in other transaction
247
utxNested.begin();
248         try {
249             // creates the table
250
if (!bolOnlyCreateTrans) {
251                 tableManager.insertTable(TABLE);
252             }
253             // does the commit in the nested transaction
254
// does the commit in the nested transaction
255
utxNested.commit();
256         } catch (Exception JavaDoc e) {
257             utxNested.rollback();
258             if (this.getTransactionStatus() != Status.STATUS_NO_TRANSACTION) {
259                 utx.rollback();
260             }
261             throw new TransactionException("Error during commit.", e);
262         }
263     }
264
265     /**
266      * Inserts the table called test. The transaction is openned and after
267      * insert the table, an rollback is called.
268      * @throws SQLException if a database error occurs.
269      * @throws NamingException if a lookup error occurs.
270      * @throws SystemException if an unexpected error occurs.
271      * @throws NotSupportedException if the resquest cannot be made.
272      */

273     public void insertTableWithBeginRollback() throws SQLException JavaDoc, NamingException JavaDoc, SystemException JavaDoc,
274             NotSupportedException JavaDoc {
275         utx.begin();
276         if (!bolOnlyCreateTrans) {
277             tableManager.insertTable(TABLE);
278         }
279         utx.rollback();
280     }
281
282     /**
283      * Makes a rollback in the transaction.
284      * @throws IllegalStateException if a transaction is not associated with a transaction.
285      * @throws SecurityException if the transaction is not allowed to make a rollback.
286      * @throws SystemException if an unexpected error occurs.
287      */

288     public void setRollback() throws IllegalStateException JavaDoc, SecurityException JavaDoc, SystemException JavaDoc{
289         utx.rollback();
290     }
291
292     /**
293      * Gets the transaction.
294      * @param callOnlyTransaction if only the transactions must be called.
295      * @param dbName the database where the table is inserted.
296      * @throws SQLException if a database error occurs.
297      * @throws NamingException if a lookup error occurs.
298      */

299     public void startup(final boolean callOnlyTransaction, final String JavaDoc dbName) throws NamingException JavaDoc, SQLException JavaDoc {
300         utx = TransactionHelper.getUserTransaction();
301         bolOnlyCreateTrans = callOnlyTransaction;
302         tableManager = new TableManager(dbName);
303
304     }
305
306     /**
307      * Obtains the bean transaction status.
308      * @return the bean transaction status.
309      * @throws SystemException if an unexpected error occurs.
310      */

311     public int getTransactionStatus() throws SystemException JavaDoc {
312         return utx.getStatus();
313     }
314
315     /**
316      * Makes a setRollbackOnly that must to throw en exception.
317      * @throws NamingException if a lookup error occurs.
318      * @throws IllegalStateException if the bean try to call the setRollBackOnly
319      */

320     public void setRollbackOnly() throws NamingException JavaDoc, IllegalStateException JavaDoc{
321         sessionContext.setRollbackOnly();
322     }
323
324     /**
325      * Makes a getRollbackOnly that must to throw en exception.
326      * @throws NamingException if a lookup error occurs.
327      * @throws IllegalStateException if the bean try to call the getRollBackOnly
328      */

329     public void getRollbackOnly() throws NamingException JavaDoc, IllegalStateException JavaDoc{
330         sessionContext.getRollbackOnly();
331      }
332 }
333
Popular Tags