KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > easybeans > tests > common > ejbs > stateful > containermanaged > transaction > SFSBSessionSync


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

25 package org.objectweb.easybeans.tests.common.ejbs.stateful.containermanaged.transaction;
26
27 import java.rmi.RemoteException JavaDoc;
28 import java.sql.SQLException JavaDoc;
29
30 import javax.annotation.Resource;
31 import javax.ejb.EJBException JavaDoc;
32 import javax.ejb.Remote JavaDoc;
33 import javax.ejb.SessionContext JavaDoc;
34 import javax.ejb.SessionSynchronization JavaDoc;
35 import javax.ejb.Stateful JavaDoc;
36 import javax.ejb.TransactionAttribute JavaDoc;
37 import javax.ejb.TransactionAttributeType JavaDoc;
38 import javax.ejb.TransactionManagement JavaDoc;
39 import javax.ejb.TransactionManagementType JavaDoc;
40 import javax.naming.NamingException JavaDoc;
41
42 import org.objectweb.easybeans.tests.common.db.TableManager;
43
44 /**
45  * Inserts tables in two database using the different types of transastion
46  * attribute for container-managed transaction. Uses the interface
47  * SesionSynchronization, so calls the callback methods afterBegin,
48  * beforeCompletion and afterCompletation.
49  * @author Gisele Pinheiro Souza
50  * @author Eduardo Studzinski Estima de Castro
51  */

52 @Stateful JavaDoc
53 @Remote JavaDoc(ItfSessionSync.class)
54 @TransactionManagement JavaDoc(value = TransactionManagementType.CONTAINER)
55 public class SFSBSessionSync implements ItfSessionSync, SessionSynchronization JavaDoc {
56
57
58     /**
59      * The database1 name in the registry.
60      */

61     private String JavaDoc strDB1;
62
63     /**
64      * The database2 name in the registry.
65      */

66     private String JavaDoc strDB2;
67
68     /**
69      * Indicates if the transaction rolled back.
70      */

71     private boolean bolRollback;
72
73     /**
74      * The SessionContext.
75      */

76     @Resource
77     private SessionContext JavaDoc sessionContext;
78
79     /**
80      * Inserts the table test the database.
81      * @param dbName is the name of the database in the registry.
82      * @throws SQLException if a databse error occurs.
83      * @throws NamingException if a lookup error occurs.
84      */

85     private void insertTable(final String JavaDoc dbName) throws SQLException JavaDoc, NamingException JavaDoc {
86         TableManager tableManager = new TableManager(dbName);
87         tableManager.insertTable(TABLE);
88     }
89
90     /**
91      * Inserts the table test in the second database with the transaction
92      * attribute MANDATORY.
93      * @throws SQLException if a databse error occurs.
94      * @throws NamingException if a lookup error occurs.
95      */

96     @TransactionAttribute JavaDoc(value = TransactionAttributeType.MANDATORY)
97     public void insertTableMandatory() throws SQLException JavaDoc, NamingException JavaDoc {
98         insertTable(strDB2);
99
100     }
101
102     /**
103      * Inserts the table test in the second database with the transaction
104      * attribute NEVER.
105      * @throws SQLException if a databse error occurs.
106      * @throws NamingException if a lookup error occurs.
107      */

108     @TransactionAttribute JavaDoc(value = TransactionAttributeType.NEVER)
109     public void insertTableNever() throws SQLException JavaDoc, NamingException JavaDoc {
110         insertTable(strDB2);
111     }
112
113     /**
114      * Inserts the table test in the second database with the transaction
115      * attribute NOT_SUPPORTED.
116      * @throws SQLException if a databse error occurs.
117      * @throws NamingException if a lookup error occurs.
118      */

119     @TransactionAttribute JavaDoc(value = TransactionAttributeType.NOT_SUPPORTED)
120     public void insertTableNotSupported() throws SQLException JavaDoc, NamingException JavaDoc {
121         insertTable(strDB2);
122     }
123
124     /**
125      * Inserts the table test in the second database with the transaction
126      * attribute REQUIRED.
127      * @throws SQLException if a databse error occurs.
128      * @throws NamingException if a lookup error occurs.
129      */

130     @TransactionAttribute JavaDoc(value = TransactionAttributeType.REQUIRED)
131     public void insertTableRequired() throws SQLException JavaDoc, NamingException JavaDoc {
132         insertTable(strDB2);
133     }
134
135     /**
136      * Inserts the table test in the second database with the transaction
137      * attribute REQUIRED_NEW.
138      * @throws SQLException if a databse error occurs.
139      * @throws NamingException if a lookup error occurs.
140      */

141     @TransactionAttribute JavaDoc(value = TransactionAttributeType.REQUIRES_NEW)
142     public void insertTableRequiredNew() throws SQLException JavaDoc, NamingException JavaDoc {
143         insertTable(strDB2);
144     }
145
146     /**
147      * Inserts the table test in the second database with the transaction
148      * attribute SUPPORTS.
149      * @throws SQLException if a databse error occurs.
150      * @throws NamingException if a lookup error occurs.
151      */

152     @TransactionAttribute JavaDoc(value = TransactionAttributeType.SUPPORTS)
153     public void insertTableSupports() throws SQLException JavaDoc, NamingException JavaDoc {
154         insertTable(strDB2);
155     }
156
157     /**
158      * Initializes the bean with the database name in the registry.
159      * @param dbName1 database1 name in the registry.
160      * @param dbName2 database2 name in the registry.
161      */

162     @TransactionAttribute JavaDoc(value = TransactionAttributeType.SUPPORTS)
163     public void startup(final String JavaDoc dbName1, final String JavaDoc dbName2) {
164         strDB1 = dbName1;
165         strDB2 = dbName2;
166         bolRollback = false;
167     }
168
169     /**
170      * Inserts the table test in the first database. This callback method mustbe
171      * called before the method with transaction.
172      * @throws EJBException if a system error occurs.
173      * @throws RemoteException used to maintain the compatibility with EJB 1.1.
174      */

175     public void afterBegin() throws EJBException JavaDoc, RemoteException JavaDoc {
176         try {
177             insertTable(strDB1);
178         } catch (SQLException JavaDoc e) {
179             throw new EJBException JavaDoc("SQLError " + e.getMessage());
180         } catch (NamingException JavaDoc e) {
181             throw new EJBException JavaDoc("LookupError " + e.getMessage());
182         }
183         // sets the rollback as false
184
bolRollback = false;
185
186     }
187
188     /**
189      * Verifies if the transaction was commited. If the transaction was
190      * committed, the method throws an error. The beforeCompletion method
191      * always call the setRolbackOnly, so the transaction never can be commited.
192      * @param commited says if the transaction was commited.
193      * @throws EJBException if a system error occurs.
194      * @throws RemoteException used to maintain the compatibility with EJB 1.1.
195      */

196     public void afterCompletion(final boolean commited) throws EJBException JavaDoc, RemoteException JavaDoc {
197         // if the container made a commit, it is an error, because the set
198
// rollback only is active.
199
if (commited) {
200             throw new EJBException JavaDoc("The bean cannot make the commit.");
201         }
202         // says that the setRollbackOnly was called.
203
bolRollback = true;
204     }
205
206     /**
207      * Calls the setRollbackOnly.
208      * @throws EJBException if a system error occurs.
209      * @throws RemoteException used to maintain the compatibility with EJB 1.1.
210      */

211     public void beforeCompletion() throws EJBException JavaDoc, RemoteException JavaDoc {
212         sessionContext.setRollbackOnly();
213     }
214
215     /**
216      * Verifies if the transaction was rolled back.
217      * @return true if the transaction was rolled back, false otherwise.
218      */

219     public boolean isRolledback() {
220         return bolRollback;
221     }
222
223 }
224
Popular Tags