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: SFSBBeanManagedException.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.ejb.Remote;
029: import javax.ejb.Stateful;
030: import javax.ejb.TransactionManagement;
031: import javax.ejb.TransactionManagementType;
032: import javax.naming.NamingException;
033: import javax.transaction.HeuristicMixedException;
034: import javax.transaction.HeuristicRollbackException;
035: import javax.transaction.NotSupportedException;
036: import javax.transaction.RollbackException;
037: import javax.transaction.SystemException;
038: import javax.transaction.UserTransaction;
039:
040: import org.ow2.easybeans.tests.common.db.TableManager;
041: import org.ow2.easybeans.tests.common.exception.RollbackAppRuntimeException;
042: import org.ow2.easybeans.tests.common.exception.TransactionRuntimeException;
043: import org.ow2.easybeans.tests.common.helper.TransactionHelper;
044:
045: /**
046: * Used to test the Exceptions and the bean-managed transaction.
047: * @author Gisele Pinheiro Souza
048: * @author Eduardo Studzinski Estima de Castro
049: *
050: */
051: @Stateful
052: @TransactionManagement(TransactionManagementType.BEAN)
053: @Remote
054: public class SFSBBeanManagedException implements
055: ItfBeanManagedException {
056:
057: /**
058: * Transaction used during the database operations.
059: */
060: private UserTransaction utx = null;
061:
062: /**
063: * Class used to create the tables.
064: */
065: private TableManager tableManager = null;
066:
067: /**
068: * Creates an instance of the tableManager.
069: * @param dbName the database name.
070: * @throws NamingException if a lookup error occurs.
071: */
072: public void startup(final String dbName) throws NamingException {
073: tableManager = new TableManager(dbName);
074: }
075:
076: /**
077: * Begins a transaction, inserts the data in the database, throws an
078: * application exception and tries to commit.
079: * @throws SQLException if a database error occurs.
080: * @throws NamingException if a lookup error occurs.
081: * @throws SystemException if an unexpected error occurs.
082: * @throws NotSupportedException if the resquest cannot be made.
083: * @throws HeuristicRollbackException if a heuristic decision was made and
084: * some relevant update was rolled back.
085: * @throws RollbackException if the transaction was rolled back instead of
086: * committed.
087: * @throws HeuristicMixedException if a heuristic decision was made and some
088: * relevant update was commited and others rolled back.
089: * @throws IllegalStateException if the bean is not associated with a
090: * transaction.
091: * @throws SecurityException if the bean is not allowed to commit.
092: */
093: public void insertTableWithAppException() throws NamingException,
094: SQLException, NotSupportedException, SystemException,
095: IllegalStateException, SecurityException,
096: HeuristicMixedException, HeuristicRollbackException,
097: RollbackException {
098: utx = TransactionHelper.getUserTransaction();
099: try {
100: utx.begin();
101: tableManager.insertTable(TABLE);
102: throw new RollbackAppRuntimeException(new Throwable(
103: "Application exception with rollback = true"));
104: } finally {
105: utx.commit();
106: }
107: }
108:
109: /**
110: * Begins a transaction, inserts the data in the database, throws a
111: * runtime exception and tries to rollback.
112: * @throws SQLException if a database error occurs.
113: * @throws NamingException if a lookup error occurs.
114: * @throws SystemException if an unexpected error occurs.
115: * @throws NotSupportedException if the resquest cannot be made.
116: * @throws HeuristicRollbackException if a heuristic decision was made and
117: * some relevant update was rolled back.
118: * @throws RollbackException if the transaction was rolled back instead of
119: * committed.
120: * @throws HeuristicMixedException if a heuristic decision was made and some
121: * relevant update was commited and others rolled back.
122: * @throws IllegalStateException if the bean is not associated with a
123: * transaction.
124: * @throws SecurityException if the bean is not allowed to commit.
125:
126: */
127: public void insertTableWithRuntimeException()
128: throws NamingException, SQLException,
129: NotSupportedException, SystemException,
130: IllegalStateException, SecurityException,
131: HeuristicMixedException, HeuristicRollbackException,
132: RollbackException {
133: utx = TransactionHelper.getUserTransaction();
134: utx.begin();
135: tableManager.insertTable(TABLE);
136: try {
137: // will throw a runtime exception
138: internallCall();
139: // we wanted to commit the transaction
140: utx.commit();
141: } catch (TransactionRuntimeException e) {
142: utx.rollback();
143: throw e;
144: }
145:
146: }
147:
148: /**
149: * Simulates an internal call that throws a Runtime exception.
150: */
151: private void internallCall() {
152: throw new TransactionRuntimeException(null);
153: }
154:
155: /**
156: * Does nothing.
157: *
158: */
159: public void emptyMethod() {
160:
161: }
162: }
|