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: ContainerTransaction.java 1970 2007-10-16 11:49:25Z benoitf $
023: * --------------------------------------------------------------------------
024: */package org.ow2.easybeans.tests.common.ejbs.base.transaction;
025:
026: import java.sql.Connection;
027: import java.sql.PreparedStatement;
028:
029: import javax.annotation.Resource;
030: import javax.ejb.SessionContext;
031: import javax.sql.DataSource;
032: import javax.transaction.UserTransaction;
033:
034: import org.ow2.easybeans.tests.common.db.TableManager;
035: import org.ow2.easybeans.tests.common.ejbs.stateless.containermanaged.transaction.ItfTransactionMisc00;
036: import org.ow2.easybeans.tests.common.ejbs.stateless.containermanaged.transaction.SLSBTransactionMisc00;
037: import org.ow2.easybeans.tests.common.exception.TransactionException;
038: import org.ow2.easybeans.tests.common.helper.DBHelper;
039: import org.ow2.easybeans.tests.common.helper.EJBHelper;
040: import org.ow2.easybeans.tests.common.helper.TransactionHelper;
041:
042: /**
043: * Has all methods needed to do the tests with container-managed transaction.
044: * @author Gisele Pinheiro Souza
045: * @author Eduardo Studzinski Estima de Castro
046: */
047: public class ContainerTransaction implements ItfContainerTransaction {
048:
049: /**
050: * The bean session context.
051: */
052: @Resource
053: private SessionContext ctx;
054:
055: /**
056: * Inserts the table test the database.
057: * @param dbName is the name of the database in the registry.
058: * @throws Exception if an error occurs.
059: */
060: protected void insertTable(final String dbName) throws Exception {
061: TableManager tableManager = new TableManager(dbName);
062: tableManager.insertTable(TABLE);
063: }
064:
065: /**
066: * Inserts the table test in both database.
067: * @param db1 the name of the first database.
068: * @param db2 the name of the second database.
069: * @throws Exception if an error occurs.
070: */
071: public void insertCorrectTableInBothDB(final String db1,
072: final String db2) throws Exception {
073: insertTable(db1);
074: insertTable(db2);
075:
076: }
077:
078: /**
079: * Makes an incorrect query in the database to force an exception.
080: * @param db1 the database name in the registry.
081: * @throws Exception if an error occurs.
082: */
083: private void makeSQLError(final String db1) throws Exception {
084: Connection connection = null;
085: DataSource ds = DBHelper.getDataSource(db1);
086: try {
087: // gets a connection
088: connection = ds.getConnection();
089: PreparedStatement stmUpdate = null;
090: // connect and insert a query with error
091: try {
092: stmUpdate = connection
093: .prepareStatement("CREATE TABLE error(");
094: stmUpdate.executeUpdate();
095: } finally {
096: if (stmUpdate != null) {
097: stmUpdate.close();
098: }
099: }
100: } finally {
101: if (connection != null) {
102: connection.close();
103: }
104: }
105: }
106:
107: /**
108: * Inserts the table test in the first database and makes an incorrect query
109: * in the second database.
110: * @param db1 the name of the first database.
111: * @param db2 the name of the second databse.
112: * @throws Exception if an error occurs.
113: */
114: public void insertCorrectFirstErrorSecond(final String db1,
115: final String db2) throws Exception {
116: // inserts a correct table
117: insertTable(db1);
118: // makes an incorrect sql query
119: makeSQLError(db2);
120:
121: }
122:
123: /**
124: * Calls the method SessionContext.setRollbackOnly().
125: * @param dbName1 the first database where the table should be inserted.
126: * @param dbName2 the second database where the table should be inserted.
127: * @throws TransactionException if an IllegalStateException occurs.
128: * @throws Exception if an error occurs.
129: */
130: public void setRollbackOnly(final String dbName1,
131: final String dbName2) throws TransactionException,
132: Exception {
133: insertTable(dbName1);
134: insertTable(dbName2);
135: try {
136: ctx.setRollbackOnly();
137: } catch (IllegalStateException e) {
138: throw new TransactionException(
139: "There was an exception in the getRollbackOnly", e);
140: }
141: }
142:
143: /**
144: * Calls the method SessionContext.getRollbackOnly().
145: * @return true if the rollback only is set, false otherwise.
146: * @throws TransactionException if rollbackonly fails
147: */
148: public boolean getRollbackOnly() throws TransactionException {
149: boolean bolResult;
150: try {
151: bolResult = ctx.getRollbackOnly();
152: } catch (IllegalStateException e) {
153: throw new TransactionException(
154: "There was an exception in the getRollbackOnly", e);
155: }
156: return bolResult;
157: }
158:
159: /**
160: * Inserts a table in the first database,calls an auxiliary bean to create a
161: * table in the second database and makes an exception to force a rollback
162: * if the transaction attribute supports. The auxiliary bean uses the
163: * transaction attribute REQUIRED.
164: * @param db1 the name of the first database.
165: * @param db2 the name of the second database.
166: * @throws Exception if an error during the execution occurs.
167: */
168: public void insertTablesUsingAuxBeanReq(final String db1,
169: final String db2) throws Exception {
170: // inserts a correct table in the first database
171: insertTable(db1);
172: // creates the auxiliary bean
173: ItfTransactionMisc00 slsbTransactionMisc00 = EJBHelper
174: .getBeanRemoteInstance(SLSBTransactionMisc00.class,
175: ItfTransactionMisc00.class);
176: // calls insert table for the auxiliary bean in the second database
177: slsbTransactionMisc00.insertTableWithRequired(db2);
178: // makes an illegal insertion to force a rollback.
179: makeSQLError(db2);
180:
181: }
182:
183: /**
184: * Inserts a table in the first database,calls an auxiliary bean to create a
185: * table in the second database and makes an exception to force a rollback
186: * if the transaction attribute supports. The auxiliary bean uses the
187: * transaction attribute NOT_SUPPORTED.
188: * @param db1 the name of the first database.
189: * @param db2 the name of the second database.
190: * @throws Exception if an error during the execution occurs.
191: */
192: public void insertTablesUsingAuxBeanNotSup(final String db1,
193: final String db2) throws Exception {
194: // inserts a correct table in the first database
195: insertTable(db1);
196: // creates the auxiliary bean
197: ItfTransactionMisc00 slsbTransactionMisc00 = EJBHelper
198: .getBeanRemoteInstance(SLSBTransactionMisc00.class,
199: ItfTransactionMisc00.class);
200: // calls insert table for the auxiliary bean in the second database
201: slsbTransactionMisc00.insertTableWithNotSupported(db2);
202: // makes an illegal insertion to force a rollback.
203: makeSQLError(db2);
204:
205: }
206:
207: /**
208: * Makes a lookup in the registry to get an UserTransaction.An exception
209: * must be thrown because the bean with container-managed transaction cannot
210: * call a bean-managed transaction.
211: * @throws Exception if an error occurs.
212: */
213: public void getUserTransactionWithLookup() throws Exception {
214: try {
215: UserTransaction utx = TransactionHelper
216: .getUserTransaction();
217: utx.begin();
218: utx.commit();
219: } catch (Exception e) {
220: throw new TransactionException(
221: "The bean cannot get the user transaction with the JNDI",
222: e);
223: }
224: }
225:
226: /**
227: * Call the method getUserTransaction() in the EJBContext to get an
228: * UserTransaction.An exception must be thrown because the bean with
229: * container-managed transaction cannot call a bean-managed transaction.
230: * @throws Exception if an error occurs.
231: */
232: public void getUserTransactionWithEJBContext() throws Exception {
233: try {
234: UserTransaction utx = ctx.getUserTransaction();
235: utx.begin();
236: utx.commit();
237: } catch (Exception e) {
238: throw new TransactionException(
239: "The bean cannot get the user transaction with the EJBContext",
240: e);
241: }
242: }
243:
244: }
|