KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > easybeans > tests > common > ejbs > stateless > beanmanaged > transaction > SLSBBeanManagedTransaction


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: SLSBBeanManagedTransaction.java 808 2006-07-03 13:58:29Z pinheirg $
23  * --------------------------------------------------------------------------
24  */

25 package org.objectweb.easybeans.tests.common.ejbs.stateless.beanmanaged.transaction;
26
27 import java.sql.SQLException JavaDoc;
28
29 import javax.ejb.EJBContext JavaDoc;
30 import javax.ejb.Remote JavaDoc;
31 import javax.ejb.Stateless JavaDoc;
32 import javax.ejb.TransactionManagement JavaDoc;
33 import javax.ejb.TransactionManagementType JavaDoc;
34 import javax.naming.NamingException JavaDoc;
35 import javax.transaction.HeuristicMixedException JavaDoc;
36 import javax.transaction.HeuristicRollbackException JavaDoc;
37 import javax.transaction.NotSupportedException JavaDoc;
38 import javax.transaction.RollbackException JavaDoc;
39 import javax.transaction.SystemException JavaDoc;
40 import javax.transaction.UserTransaction JavaDoc;
41
42 import org.objectweb.easybeans.tests.common.db.TableManager;
43 import org.objectweb.easybeans.tests.common.exception.TransactionException;
44 import org.objectweb.easybeans.tests.common.helper.EJBContextHelper;
45 import org.objectweb.easybeans.tests.common.helper.TransactionHelper;
46
47 /**
48  * Inserts the table test in the database using the UserTransaction. This class
49  * has many methods with differents combinations of begin/commit.
50  * @author Gisele Pinheiro Souza
51  * @author Eduardo Studzinski Estima de Castro
52  */

53 @Stateless JavaDoc(name="SLSBBeanManagedTransaction")
54 @Remote JavaDoc(ItfBeanManagedTransaction.class)
55 @TransactionManagement JavaDoc(value = TransactionManagementType.BEAN)
56 public class SLSBBeanManagedTransaction implements ItfBeanManagedTransaction {
57
58     /**
59      * Deletes the table and makes an UserTransaction.begin() before create the
60      * table and an UserTransaction.commit() after create.
61      * @param callOnlyTransaction says if the table must be created or only the
62      * UserTransaction.begin() and UserTransaction.commit() must be
63      * called.
64      * @param dbName database where the table must be inserted.
65      * @throws SQLException if a database error occurs.
66      * @throws NamingException if a lookup error occurs.
67      * @throws SystemException if an unexpected error occurs.
68      * @throws NotSupportedException if the resquest cannot be made.
69      * @throws HeuristicRollbackException if a heuristic decision was made and
70      * some relevant update was rolled back.
71      * @throws RollbackException if the transaction was rolled back instead of
72      * committed.
73      * @throws HeuristicMixedException if a heuristic decision was made and some
74      * relevant update was commited and others rolled back.
75      * @throws TransactionException if a rollback was made.
76      */

77     public void deleteTableWithBeginCommit(final boolean callOnlyTransaction, final String JavaDoc dbName) throws SQLException JavaDoc,
78             NamingException JavaDoc, SystemException JavaDoc, NotSupportedException JavaDoc, HeuristicRollbackException JavaDoc, RollbackException JavaDoc,
79             HeuristicMixedException JavaDoc, TransactionException {
80         UserTransaction JavaDoc utx = TransactionHelper.getUserTransaction();
81         utx.begin();
82         try {
83             if (!callOnlyTransaction) {
84                 TableManager tableManager = new TableManager(dbName);
85                 tableManager.deleteTable(TABLE);
86             }
87             utx.commit();
88         } catch (Exception JavaDoc e) {
89             utx.rollback();
90             throw new TransactionException("Error during commit.", e);
91         }
92
93     }
94
95     /**
96      * Deletes the table and makes an UserTransaction.commit() after create.
97      * @param callOnlyTransaction says if the table must be created or only the
98      * UserTransaction.begin() and UserTransaction.commit() must be
99      * called.
100      * @param dbName database where the table must be inserted.
101      * @throws SQLException if a database error occurs.
102      * @throws NamingException if a lookup error occurs.
103      * @throws SystemException if an unexpected error occurs.
104      * @throws NotSupportedException if the resquest cannot be made.
105      * @throws HeuristicRollbackException if a heuristic decision was made and
106      * some relevant update was rolled back.
107      * @throws RollbackException if the transaction was rolled back instead of
108      * committed.
109      * @throws HeuristicMixedException if a heuristic decision was made and some
110      * relevant update was commited and others rolled back.
111      */

112     public void deleteTableWithoutBegin(final boolean callOnlyTransaction, final String JavaDoc dbName) throws SQLException JavaDoc,
113             NamingException JavaDoc, SystemException JavaDoc, NotSupportedException JavaDoc, HeuristicRollbackException JavaDoc, RollbackException JavaDoc,
114             HeuristicMixedException JavaDoc {
115         UserTransaction JavaDoc utx = TransactionHelper.getUserTransaction();
116         if (!callOnlyTransaction) {
117             TableManager tableManager = new TableManager(dbName);
118             tableManager.deleteTable(TABLE);
119         }
120         utx.commit();
121
122     }
123
124     /**
125      * Calls userTransaction.begin(), inserts the table and calls userTransaction.commit().
126      * @param callOnlyTransaction says if the table must be created or only the
127      * UserTransaction.begin() and UserTransaction.commit() must be
128      * called.
129      * @param dbName database where the table must be inserted.
130      * @throws SQLException if a database error occurs.
131      * @throws NamingException if a lookup error occurs.
132      * @throws SystemException if an unexpected error occurs.
133      * @throws NotSupportedException if the resquest cannot be made.
134      * @throws HeuristicRollbackException if a heuristic decision was made and
135      * some relevant update was rolled back.
136      * @throws RollbackException if the transaction was rolled back instead of
137      * committed.
138      * @throws HeuristicMixedException if a heuristic decision was made and some
139      * relevant update was commited and others rolled back.
140      * @throws TransactionException if a rollback was made.
141      */

142     public void insertTableWithBeginCommit(final boolean callOnlyTransaction, final String JavaDoc dbName) throws SQLException JavaDoc,
143             NamingException JavaDoc, SystemException JavaDoc, NotSupportedException JavaDoc, HeuristicRollbackException JavaDoc, RollbackException JavaDoc,
144             HeuristicMixedException JavaDoc, TransactionException {
145         UserTransaction JavaDoc utx = TransactionHelper.getUserTransaction();
146         utx.begin();
147         try {
148             if (!callOnlyTransaction) {
149                 TableManager tableManager = new TableManager(dbName);
150                 tableManager.insertTable(TABLE);
151             }
152             utx.commit();
153         } catch (Exception JavaDoc e) {
154             utx.rollback();
155             throw new TransactionException("Error during commit.", e);
156         }
157
158     }
159
160     /**
161      * Inserts the table and makes an UserTransaction.begin() before create the
162      * table.
163      * @param callOnlyTransaction says if the table must be created or only the
164      * UserTransaction.begin() and UserTransaction.commit() must be
165      * called.
166      * @param dbName database where the table must be inserted.
167      * @throws SQLException if a database error occurs.
168      * @throws NamingException if a lookup error occurs.
169      * @throws SystemException if an unexpected error occurs.
170      * @throws NotSupportedException if the resquest cannot be made.
171      */

172     public void insertTableWithoutCommit(final boolean callOnlyTransaction, final String JavaDoc dbName) throws SQLException JavaDoc,
173             NamingException JavaDoc, SystemException JavaDoc, NotSupportedException JavaDoc {
174         UserTransaction JavaDoc utx = TransactionHelper.getUserTransaction();
175         utx.begin();
176         if (!callOnlyTransaction) {
177             TableManager tableManager = new TableManager(dbName);
178             tableManager.insertTable(TABLE);
179         }
180     }
181
182     /**
183      * Makes a setRollbackOnly that must to throw en exception.
184      * @throws NamingException if a lookup error occurs.
185      * @throws IllegalStateException if the bean try to call the setRollBackOnly
186      */

187     public void setRollbackOnly() throws NamingException JavaDoc, IllegalStateException JavaDoc {
188         EJBContext JavaDoc ejbContext = EJBContextHelper.getEJBContext();
189         ejbContext.setRollbackOnly();
190     }
191
192     /**
193      * Makes a getRollbackOnly that must to throw en exception.
194      * @throws NamingException if a lookup error occurs.
195      * @throws IllegalStateException if the bean try to call the getRollBackOnly
196      */

197     public void getRollbackOnly() throws NamingException JavaDoc, IllegalStateException JavaDoc {
198         EJBContext JavaDoc ejbContext = EJBContextHelper.getEJBContext();
199         ejbContext.getRollbackOnly();
200     }
201
202 }
203
Popular Tags