001: /**
002: * EasyBeans
003: * Copyright (C) 2006 Bull S.A.S.
004: * Contact: easybeans@ow2.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * --------------------------------------------------------------------------
022: * $Id: SFSBBeanManagedTransaction.java 1970 2007-10-16 11:49:25Z benoitf $
023: * --------------------------------------------------------------------------
024: */package org.ow2.easybeans.tests.common.ejbs.stateful.beanmanaged.transaction;
025:
026: import java.sql.SQLException;
027:
028: import javax.annotation.Resource;
029: import javax.ejb.Remote;
030: import javax.ejb.SessionContext;
031: import javax.ejb.Stateful;
032: import javax.ejb.TransactionManagement;
033: import javax.ejb.TransactionManagementType;
034: import javax.naming.NamingException;
035: import javax.transaction.HeuristicMixedException;
036: import javax.transaction.HeuristicRollbackException;
037: import javax.transaction.NotSupportedException;
038: import javax.transaction.RollbackException;
039: import javax.transaction.Status;
040: import javax.transaction.SystemException;
041: import javax.transaction.UserTransaction;
042:
043: import org.ow2.easybeans.tests.common.db.TableManager;
044: import org.ow2.easybeans.tests.common.exception.TransactionException;
045: import org.ow2.easybeans.tests.common.helper.TransactionHelper;
046:
047: /**
048: * Inserts the table test in the database. This class uses usertransactions for
049: * making the operations.
050: * @author Gisele Pinheiro Souza
051: * @author Eduardo Studzinski Estima de Castro
052: */
053: @Stateful(name="SFSBBeanManagedTransaction")
054: @Remote(ItfBeanManagedTransaction.class)
055: @TransactionManagement(value=TransactionManagementType.BEAN)
056: public class SFSBBeanManagedTransaction implements
057: ItfBeanManagedTransaction {
058:
059: /**
060: * The bean sessionContext.
061: */
062: @Resource
063: private SessionContext sessionContext;
064:
065: /**
066: * Transaction used during the database operations.
067: */
068: private UserTransaction utx = null;
069:
070: /**
071: * Class used to create the tables.
072: */
073: private TableManager tableManager = null;
074:
075: /**
076: * Says if the table must be created. Used to tests if the container accepts
077: * the transaction order.
078: */
079: private boolean bolOnlyCreateTrans = false;
080:
081: /**
082: * Deletes the table called test. The bean transaction is started and
083: * commited in this method.
084: * @throws SQLException if a database error occurs.
085: * @throws NamingException if a lookup error occurs.
086: * @throws SystemException if an unexpected error occurs.
087: * @throws NotSupportedException if the resquest cannot be made.
088: * @throws HeuristicRollbackException if a heuristic decision was made and
089: * some relevant update was rolled back.
090: * @throws RollbackException if the transaction was rolled back instead of
091: * committed.
092: * @throws HeuristicMixedException if a heuristic decision was made and some
093: * relevant update was commited and others rolled back.
094: * @throws TransactionException if a rollback was executed.
095: */
096: public void dropTableWithBeginCommitTransaction()
097: throws SQLException, NamingException, SystemException,
098: NotSupportedException, HeuristicRollbackException,
099: RollbackException, HeuristicMixedException,
100: TransactionException {
101: utx.begin();
102: try {
103: if (!bolOnlyCreateTrans) {
104: tableManager.deleteTable(TABLE);
105: }
106: utx.commit();
107: } catch (Exception e) {
108: utx.rollback();
109: throw new TransactionException("Error during commit.", e);
110: }
111:
112: }
113:
114: /**
115: * Deletes the table called test. The transaction is not open, but it is
116: * commited in this method.
117: * @throws SQLException if a database error occurs.
118: * @throws NamingException if a lookup error occurs.
119: * @throws SystemException if an unexpected error occurs.
120: * @throws NotSupportedException if the resquest cannot be made.
121: * @throws HeuristicRollbackException if a heuristic decision was made and
122: * some relevant update was rolled back.
123: * @throws RollbackException if the transaction was rolled back instead of
124: * committed.
125: * @throws HeuristicMixedException if a heuristic decision was made and some
126: * relevant update was commited and others rolled back.
127: * @throws TransactionException if a rollback was executed.
128: */
129: public void dropTableWithoutBeginTransaction() throws SQLException,
130: NamingException, SystemException, NotSupportedException,
131: HeuristicRollbackException, RollbackException,
132: HeuristicMixedException, TransactionException {
133: try {
134: if (!bolOnlyCreateTrans) {
135: tableManager.deleteTable(TABLE);
136: }
137: utx.commit();
138: } catch (Exception e) {
139: utx.rollback();
140: throw new TransactionException("Error during commit.", e);
141: }
142: }
143:
144: /**
145: * Inserts the table called test. The transaction is open and commited in
146: * this method.
147: * @throws SQLException if a database error occurs.
148: * @throws NamingException if a lookup error occurs.
149: * @throws SystemException if an unexpected error occurs.
150: * @throws NotSupportedException if the resquest cannot be made.
151: * @throws HeuristicRollbackException if a heuristic decision was made and
152: * some relevant update was rolled back.
153: * @throws RollbackException if the transaction was rolled back instead of
154: * committed.
155: * @throws HeuristicMixedException if a heuristic decision was made and some
156: * relevant update was commited and others rolled back.
157: * @throws TransactionException if a rollback was executed.
158: */
159: public void insertTableWithBeginCommitTransaction()
160: throws SQLException, NamingException, SystemException,
161: NotSupportedException, HeuristicRollbackException,
162: RollbackException, HeuristicMixedException,
163: TransactionException {
164:
165: utx.begin();
166: try {
167: if (!bolOnlyCreateTrans) {
168: tableManager.insertTable(TABLE);
169: }
170: utx.commit();
171: } catch (Exception e) {
172: utx.rollback();
173: throw new TransactionException("Error during commit.", e);
174: }
175: }
176:
177: /**
178: * Inserts the table called test. The transaction will be open, but it will not
179: * be commited in this method.
180: * @throws SQLException if a database error occurs.
181: * @throws NamingException if a lookup error occurs.
182: * @throws SystemException if an unexpected error occurs.
183: * @throws NotSupportedException if the resquest cannot be made.
184: */
185: public void insertTableWithoutCommitTransaction()
186: throws SQLException, NamingException, SystemException,
187: NotSupportedException {
188: utx.begin();
189: if (!bolOnlyCreateTrans) {
190: tableManager.insertTable(TABLE);
191: }
192: }
193:
194: /**
195: * Creates the table called test.The bean transaction is started and
196: * commited in this method and, also, there is a nested transaction.
197: * @throws SQLException if a database error occurs.
198: * @throws NamingException if a lookup error occurs.
199: * @throws SystemException if an unexpected error occurs.
200: * @throws NotSupportedException if the resquest cannot be made.
201: * @throws HeuristicRollbackException if a heuristic decision was made and
202: * some relevant update was rolled back.
203: * @throws RollbackException if the transaction was rolled back instead of
204: * committed.
205: * @throws HeuristicMixedException if a heuristic decision was made and some
206: * relevant update was commited and others rolled back.
207: * @throws TransactionException if a rollback was executed.
208: */
209: public void insertTableWithNestedTrans() throws SQLException,
210: NamingException, SystemException, NotSupportedException,
211: HeuristicRollbackException, RollbackException,
212: HeuristicMixedException, TransactionException {
213: // does a begin in the bean transaction
214: utx.begin();
215: // creates a new transaction
216: UserTransaction utxNested = TransactionHelper
217: .getUserTransaction();
218:
219: // does a begin in other transaction
220: utxNested.begin();
221: // creates the table
222: if (!bolOnlyCreateTrans) {
223: tableManager.insertTable(TABLE);
224: }
225: // does the commit in the nested transaction
226: utxNested.commit();
227: // does the commit in the bean transaction
228: utx.commit();
229:
230: }
231:
232: /**
233: * Creates the table called test.A new transaction is created and it makes
234: * the begin and commit transaction. The bean transaction is not started or
235: * commited in this method.
236: * @throws SQLException if a database error occurs.
237: * @throws NamingException if a lookup error occurs.
238: * @throws SystemException if an unexpected error occurs.
239: * @throws NotSupportedException if the resquest cannot be made.
240: * @throws HeuristicRollbackException if a heuristic decision was made and
241: * some relevant update was rolled back.
242: * @throws RollbackException if the transaction was rolled back instead of
243: * committed.
244: * @throws HeuristicMixedException if a heuristic decision was made and some
245: * relevant update was commited and others rolled back.
246: * @throws TransactionException if a rollback was executed.
247: */
248: public void insertTableWithNewTransaction() throws SQLException,
249: NamingException, SystemException, NotSupportedException,
250: HeuristicRollbackException, RollbackException,
251: HeuristicMixedException, TransactionException {
252: // creates a new transaction
253: UserTransaction utxNested = TransactionHelper
254: .getUserTransaction();
255:
256: // does a begin in other transaction
257: utxNested.begin();
258: try {
259: // creates the table
260: if (!bolOnlyCreateTrans) {
261: tableManager.insertTable(TABLE);
262: }
263: // does the commit in the nested transaction
264: // does the commit in the nested transaction
265: utxNested.commit();
266: } catch (Exception e) {
267: utxNested.rollback();
268: if (this .getTransactionStatus() != Status.STATUS_NO_TRANSACTION) {
269: utx.rollback();
270: }
271: throw new TransactionException("Error during commit.", e);
272: }
273: }
274:
275: /**
276: * Inserts the table called test. The transaction is openned and after
277: * insert the table, an rollback is called.
278: * @throws SQLException if a database error occurs.
279: * @throws NamingException if a lookup error occurs.
280: * @throws SystemException if an unexpected error occurs.
281: * @throws NotSupportedException if the resquest cannot be made.
282: */
283: public void insertTableWithBeginRollback() throws SQLException,
284: NamingException, SystemException, NotSupportedException {
285: utx.begin();
286: if (!bolOnlyCreateTrans) {
287: tableManager.insertTable(TABLE);
288: }
289: utx.rollback();
290: }
291:
292: /**
293: * Makes a rollback in the transaction.
294: * @throws IllegalStateException if a transaction is not associated with a transaction.
295: * @throws SecurityException if the transaction is not allowed to make a rollback.
296: * @throws SystemException if an unexpected error occurs.
297: */
298: public void setRollback() throws IllegalStateException,
299: SecurityException, SystemException {
300: utx.rollback();
301: }
302:
303: /**
304: * Gets the transaction.
305: * @param callOnlyTransaction if only the transactions must be called.
306: * @param dbName the database where the table is inserted.
307: * @throws SQLException if a database error occurs.
308: * @throws NamingException if a lookup error occurs.
309: */
310: public void startup(final boolean callOnlyTransaction,
311: final String dbName) throws NamingException, SQLException {
312: utx = TransactionHelper.getUserTransaction();
313: bolOnlyCreateTrans = callOnlyTransaction;
314: tableManager = new TableManager(dbName);
315:
316: }
317:
318: /**
319: * Obtains the bean transaction status.
320: * @return the bean transaction status.
321: * @throws SystemException if an unexpected error occurs.
322: */
323: public int getTransactionStatus() throws SystemException {
324: return utx.getStatus();
325: }
326:
327: /**
328: * Makes a setRollbackOnly that must to throw en exception.
329: * @throws NamingException if a lookup error occurs.
330: * @throws IllegalStateException if the bean try to call the setRollBackOnly
331: */
332: public void setRollbackOnly() throws NamingException,
333: IllegalStateException {
334: sessionContext.setRollbackOnly();
335: }
336:
337: /**
338: * Makes a getRollbackOnly that must to throw en exception.
339: * @throws NamingException if a lookup error occurs.
340: * @throws IllegalStateException if the bean try to call the getRollBackOnly
341: */
342: public void getRollbackOnly() throws NamingException,
343: IllegalStateException {
344: sessionContext.getRollbackOnly();
345: }
346: }
|