KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > easybeans > tests > common > ejbs > base > transaction > ContainerTransaction


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: ContainerTransaction.java 928 2006-07-25 13:11:32Z studzine $
23  * --------------------------------------------------------------------------
24  */

25 package org.objectweb.easybeans.tests.common.ejbs.base.transaction;
26
27 import java.sql.Connection JavaDoc;
28 import java.sql.PreparedStatement JavaDoc;
29
30 import javax.annotation.Resource;
31 import javax.ejb.SessionContext JavaDoc;
32 import javax.sql.DataSource JavaDoc;
33 import javax.transaction.UserTransaction JavaDoc;
34
35 import org.objectweb.easybeans.tests.common.db.TableManager;
36 import org.objectweb.easybeans.tests.common.ejbs.stateless.containermanaged.transaction.ItfTransactionMisc00;
37 import org.objectweb.easybeans.tests.common.ejbs.stateless.containermanaged.transaction.SLSBTransactionMisc00;
38 import org.objectweb.easybeans.tests.common.exception.TransactionException;
39 import org.objectweb.easybeans.tests.common.helper.DBHelper;
40 import org.objectweb.easybeans.tests.common.helper.EJBHelper;
41 import org.objectweb.easybeans.tests.common.helper.TransactionHelper;
42
43 /**
44  * Has all methods needed to do the tests with container-managed transaction.
45  * @author Gisele Pinheiro Souza
46  * @author Eduardo Studzinski Estima de Castro
47  */

48 public class ContainerTransaction implements ItfContainerTransaction {
49
50     /**
51      * The bean session context.
52      */

53     @Resource
54     private SessionContext JavaDoc ctx;
55
56     /**
57      * Inserts the table test the database.
58      * @param dbName is the name of the database in the registry.
59      * @throws Exception if an error occurs.
60      */

61     protected void insertTable(final String JavaDoc dbName) throws Exception JavaDoc {
62         TableManager tableManager = new TableManager(dbName);
63         tableManager.insertTable(TABLE);
64     }
65
66     /**
67      * Inserts the table test in both database.
68      * @param db1 the name of the first database.
69      * @param db2 the name of the second database.
70      * @throws Exception if an error occurs.
71      */

72     public void insertCorrectTableInBothDB(final String JavaDoc db1, final String JavaDoc db2) throws Exception JavaDoc {
73         insertTable(db1);
74         insertTable(db2);
75
76     }
77
78     /**
79      * Makes an incorrect query in the database to force an exception.
80      * @param db1 the database name in the registry.
81      * @throws Exception if an error occurs.
82      */

83     private void makeSQLError(final String JavaDoc db1) throws Exception JavaDoc {
84         Connection JavaDoc connection = null;
85         DataSource JavaDoc ds = DBHelper.getDataSource(db1);
86         try {
87             // gets a connection
88
connection = ds.getConnection();
89             PreparedStatement JavaDoc stmUpdate = null;
90             // connect and insert a query with error
91
try {
92                 stmUpdate = connection.prepareStatement("CREATE TABLE error(");
93                 stmUpdate.executeUpdate();
94             } finally {
95                 if (stmUpdate != null) {
96                     stmUpdate.close();
97                 }
98             }
99         } finally {
100             if (connection != null) {
101                 connection.close();
102             }
103         }
104     }
105
106     /**
107      * Inserts the table test in the first database and makes an incorrect query
108      * in the second database.
109      * @param db1 the name of the first database.
110      * @param db2 the name of the second databse.
111      * @throws Exception if an error occurs.
112      */

113     public void insertCorrectFirstErrorSecond(final String JavaDoc db1, final String JavaDoc db2) throws Exception JavaDoc {
114         // inserts a correct table
115
insertTable(db1);
116         // makes an incorrect sql query
117
makeSQLError(db2);
118
119     }
120
121     /**
122      * Calls the method SessionContext.setRollbackOnly().
123      * @param dbName1 the first database where the table should be inserted.
124      * @param dbName2 the second database where the table should be inserted.
125      * @throws TransactionException if an IllegalStateException occurs.
126      * @throws Exception if an error occurs.
127      */

128     public void setRollbackOnly(final String JavaDoc dbName1, final String JavaDoc dbName2) throws TransactionException, Exception JavaDoc {
129         insertTable(dbName1);
130         insertTable(dbName2);
131         try {
132             ctx.setRollbackOnly();
133         } catch (IllegalStateException JavaDoc e) {
134             throw new TransactionException("There was an exception in the getRollbackOnly", e);
135         }
136     }
137
138     /**
139      * Calls the method SessionContext.getRollbackOnly().
140      * @return true if the rollback only is set, false otherwise.
141      * @throws TransactionException if rollbackonly fails
142      */

143     public boolean getRollbackOnly() throws TransactionException {
144         boolean bolResult;
145         try {
146             bolResult = ctx.getRollbackOnly();
147         } catch (IllegalStateException JavaDoc e) {
148             throw new TransactionException("There was an exception in the getRollbackOnly", e);
149         }
150         return bolResult;
151     }
152
153     /**
154      * Inserts a table in the first database,calls an auxiliary bean to create a
155      * table in the second database and makes an exception to force a rollback
156      * if the transaction attribute supports. The auxiliary bean uses the
157      * transaction attribute REQUIRED.
158      * @param db1 the name of the first database.
159      * @param db2 the name of the second database.
160      * @throws Exception if an error during the execution occurs.
161      */

162     public void insertTablesUsingAuxBeanReq(final String JavaDoc db1, final String JavaDoc db2) throws Exception JavaDoc {
163         // inserts a correct table in the first database
164
insertTable(db1);
165         // creates the auxiliary bean
166
ItfTransactionMisc00 slsbTransactionMisc00 = EJBHelper.getBeanRemoteInstance(SLSBTransactionMisc00.class,
167                 ItfTransactionMisc00.class);
168         // calls insert table for the auxiliary bean in the second database
169
slsbTransactionMisc00.insertTableWithRequired(db2);
170         // makes an illegal insertion to force a rollback.
171
makeSQLError(db2);
172
173     }
174
175     /**
176      * Inserts a table in the first database,calls an auxiliary bean to create a
177      * table in the second database and makes an exception to force a rollback
178      * if the transaction attribute supports. The auxiliary bean uses the
179      * transaction attribute NOT_SUPPORTED.
180      * @param db1 the name of the first database.
181      * @param db2 the name of the second database.
182      * @throws Exception if an error during the execution occurs.
183      */

184     public void insertTablesUsingAuxBeanNotSup(final String JavaDoc db1, final String JavaDoc db2) throws Exception JavaDoc {
185         // inserts a correct table in the first database
186
insertTable(db1);
187         // creates the auxiliary bean
188
ItfTransactionMisc00 slsbTransactionMisc00 = EJBHelper.getBeanRemoteInstance(SLSBTransactionMisc00.class,
189                 ItfTransactionMisc00.class);
190         // calls insert table for the auxiliary bean in the second database
191
slsbTransactionMisc00.insertTableWithNotSupported(db2);
192         // makes an illegal insertion to force a rollback.
193
makeSQLError(db2);
194
195     }
196
197     /**
198      * Makes a lookup in the registry to get an UserTransaction.An exception
199      * must be thrown because the bean with container-managed transaction cannot
200      * call a bean-managed transaction.
201      * @throws Exception if an error occurs.
202      */

203     public void getUserTransactionWithLookup() throws Exception JavaDoc {
204         try {
205             UserTransaction JavaDoc utx = TransactionHelper.getUserTransaction();
206             utx.begin();
207             utx.commit();
208         } catch (Exception JavaDoc e) {
209             throw new TransactionException("The bean cannot get the user transaction with the JNDI", e);
210         }
211     }
212
213     /**
214      * Call the method getUserTransaction() in the EJBContext to get an
215      * UserTransaction.An exception must be thrown because the bean with
216      * container-managed transaction cannot call a bean-managed transaction.
217      * @throws Exception if an error occurs.
218      */

219     public void getUserTransactionWithEJBContext() throws Exception JavaDoc {
220         try {
221             UserTransaction JavaDoc utx = ctx.getUserTransaction();
222             utx.begin();
223             utx.commit();
224         } catch (Exception JavaDoc e) {
225             throw new TransactionException("The bean cannot get the user transaction with the EJBContext", e);
226         }
227     }
228
229 }
230
Popular Tags