KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openejb > core > transaction > TransactionPolicy


1 /**
2  * Redistribution and use of this software and associated documentation
3  * ("Software"), with or without modification, are permitted provided
4  * that the following conditions are met:
5  *
6  * 1. Redistributions of source code must retain copyright
7  * statements and notices. Redistributions must also contain a
8  * copy of this document.
9  *
10  * 2. Redistributions in binary form must reproduce the
11  * above copyright notice, this list of conditions and the
12  * following disclaimer in the documentation and/or other
13  * materials provided with the distribution.
14  *
15  * 3. The name "OpenEJB" must not be used to endorse or promote
16  * products derived from this Software without prior written
17  * permission of The OpenEJB Group. For written permission,
18  * please contact dev@openejb.org.
19  *
20  * 4. Products derived from this Software may not be called "OpenEJB"
21  * nor may "OpenEJB" appear in their names without prior written
22  * permission of The OpenEJB Group. OpenEJB is a registered
23  * trademark of The OpenEJB Group.
24  *
25  * 5. Due credit should be given to the OpenEJB Project
26  * (http://www.openejb.org/).
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE OPENEJB GROUP AND CONTRIBUTORS
29  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
32  * THE OPENEJB GROUP OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39  * OF THE POSSIBILITY OF SUCH DAMAGE.
40  *
41  * Copyright 2001 (C) The OpenEJB Group. All Rights Reserved.
42  *
43  * $Id: TransactionPolicy.java 1912 2005-06-16 22:29:56Z jlaskowski $
44  */

45 package org.openejb.core.transaction;
46
47 import java.rmi.RemoteException JavaDoc;
48
49 import javax.ejb.EnterpriseBean JavaDoc;
50 import javax.transaction.Transaction JavaDoc;
51 import javax.transaction.TransactionManager JavaDoc;
52
53 import org.openejb.ApplicationException;
54 import org.openejb.InvalidateReferenceException;
55 import org.openejb.OpenEJB;
56 import org.openejb.SystemException;
57 import org.openejb.core.ThreadContext;
58 import org.openejb.util.Logger;
59
60 /**
61  * Use container callbacks so containers can implement any special behavior
62  *
63  * Callbacks are similar to sessionsynchronization with the addition of
64  * discardInstace
65  * createSystemException
66  *
67  * @author <a HREF="mailto=david.blevins@visi.com">David Blevins</a>
68  * @version $Revision: 1912 $ $Date: 2005-06-16 15:29:56 -0700 (Thu, 16 Jun 2005) $
69  */

70 public abstract class TransactionPolicy {
71     
72     public static final int Mandatory = 0;
73     public static final int Never = 1;
74     public static final int NotSupported = 2;
75     public static final int Required = 3;
76     public static final int RequiresNew = 4;
77     public static final int Supports = 5;
78     public static final int BeanManaged = 6;
79     
80     public int policyType;
81     private TransactionManager JavaDoc manager;
82     protected TransactionContainer container;
83
84     protected final static Logger logger = Logger.getInstance( "OpenEJB", "org.openejb.util.resources" );
85     protected final static Logger txLogger = Logger.getInstance( "Transaction", "org.openejb.util.resources" );
86
87     protected TransactionManager JavaDoc getTxMngr( ) {
88         if(manager==null) {
89             manager = OpenEJB.getTransactionManager();
90         }
91         return manager;
92     }
93     
94     public TransactionContainer getContainer(){
95         return container;
96     }
97     
98     public String JavaDoc policyToString() {
99         return "Internal Error: no such policy";
100     }
101     
102     public abstract void handleApplicationException( Throwable JavaDoc appException, TransactionContext context ) throws org.openejb.ApplicationException;
103     public abstract void handleSystemException( Throwable JavaDoc sysException, EnterpriseBean JavaDoc instance, TransactionContext context ) throws org.openejb.ApplicationException, org.openejb.SystemException;
104     public abstract void beforeInvoke( EnterpriseBean JavaDoc bean, TransactionContext context ) throws org.openejb.SystemException, org.openejb.ApplicationException;
105     public abstract void afterInvoke(EnterpriseBean JavaDoc bean, TransactionContext context ) throws org.openejb.ApplicationException, org.openejb.SystemException;
106
107     protected void markTxRollbackOnly( Transaction JavaDoc tx ) throws SystemException{
108         try {
109             if ( tx != null ) {
110                 tx.setRollbackOnly();
111                 if(txLogger.isInfoEnabled()) {
112                     txLogger.info(policyToString()+"setRollbackOnly() on transaction "+tx);
113                 }
114             }
115         } catch ( javax.transaction.SystemException JavaDoc se ) {
116             logger.error("Exception during setRollbackOnly()", se);
117             throw new org.openejb.SystemException(se);
118         }
119     }
120
121     protected Transaction JavaDoc suspendTransaction() throws SystemException{
122         try {
123             Transaction JavaDoc tx = getTxMngr( ).suspend();
124             if(txLogger.isInfoEnabled()) {
125                 txLogger.info(policyToString()+"Suspended transaction "+tx);
126             }
127             return tx;
128         } catch ( javax.transaction.SystemException JavaDoc se ) {
129             logger.error("Exception during suspend()", se);
130             throw new org.openejb.SystemException(se);
131         }
132     }
133     
134     protected void resumeTransaction(Transaction JavaDoc tx) throws SystemException{
135         try {
136             if ( tx == null) {
137                 if(txLogger.isInfoEnabled()) {
138                     txLogger.info(policyToString()+"No transaction to resume");
139                 }
140             } else {
141                 if(txLogger.isInfoEnabled()) {
142                     txLogger.info(policyToString()+"Resuming transaction "+tx);
143                 }
144                 getTxMngr( ).resume(tx);
145             }
146         }catch(javax.transaction.InvalidTransactionException JavaDoc ite){
147             // TODO:3: Localize the message; add to Messages.java
148
txLogger.error("Could not resume the client's transaction, the transaction is no longer valid: "+ite.getMessage());
149             throw new org.openejb.SystemException(ite);
150         }catch(IllegalStateException JavaDoc e){
151             // TODO:3: Localize the message; add to Messages.java
152
txLogger.error("Could not resume the client's transaction: "+e.getMessage());
153             throw new org.openejb.SystemException(e);
154         }catch(javax.transaction.SystemException JavaDoc e){
155             // TODO:3: Localize the message; add to Messages.java
156
txLogger.error("Could not resume the client's transaction: The transaction reported a system exception: "+e.getMessage());
157             throw new org.openejb.SystemException(e);
158         }
159     }
160     
161     protected void commitTransaction( Transaction JavaDoc tx ) throws SystemException{
162         try {
163             if(txLogger.isInfoEnabled()) {
164                 txLogger.info(policyToString()+"Committing transaction "+tx);
165             }
166             if(tx.equals(getTxMngr().getTransaction())) {
167                 // this solves the problem that rolling back the transaction does not
168
// remove the association between the transaction and the current thread
169
// e.g. a subsequent resume() will fail
170
getTxMngr().commit();
171             } else {
172                 tx.commit();
173             }
174         } catch ( javax.transaction.RollbackException JavaDoc e ) {
175             // TODO:3: Localize the message; add to Messages.java
176
txLogger.info("The transaction has been rolled back rather than commited: "+e.getMessage());
177         
178         } catch ( javax.transaction.HeuristicMixedException JavaDoc e ) {
179             // TODO:3: Localize the message; add to Messages.java
180
txLogger.info("A heuristic decision was made, some relevant updates have been committed while others have been rolled back: "+e.getMessage());
181         
182         } catch ( javax.transaction.HeuristicRollbackException JavaDoc e ) {
183             // TODO:3: Localize the message; add to Messages.java
184
txLogger.info("A heuristic decision was made while commiting the transaction, some relevant updates have been rolled back: "+e.getMessage());
185
186         } catch (SecurityException JavaDoc e){
187             // TODO:3: Localize the message; add to Messages.java
188
txLogger.error("The current thread is not allowed to commit the transaction: "+e.getMessage());
189             throw new org.openejb.SystemException( e );
190         
191         } catch (IllegalStateException JavaDoc e){
192             // TODO:3: Localize the message; add to Messages.java
193
txLogger.error("The current thread is not associated with a transaction: "+e.getMessage());
194             throw new org.openejb.SystemException( e );
195
196         } catch (javax.transaction.SystemException JavaDoc e){
197             txLogger.error("The Transaction Manager has encountered an unexpected error condition while attempting to commit the transaction: "+e.getMessage());
198             // TODO:3: Localize the message; add to Messages.java
199
throw new org.openejb.SystemException( e );
200         }
201     }
202     
203     protected void rollbackTransaction( Transaction JavaDoc tx ) throws SystemException{
204         try {
205             if(txLogger.isInfoEnabled()) {
206                 txLogger.info(policyToString()+"Rolling back transaction "+tx);
207             }
208             if(tx.equals(getTxMngr().getTransaction())) {
209                 // this solves the problem that rolling back the transaction does not
210
// remove the association between the transaction and the current thread
211
// e.g. a subsequent resume() will fail
212
getTxMngr().rollback();
213             } else {
214             tx.rollback();
215             }
216         } catch (IllegalStateException JavaDoc e){
217             // TODO:3: Localize the message; add to Messages.java
218
logger.error("The TransactionManager reported an exception while attempting to rollback the transaction: "+e.getMessage());
219             throw new org.openejb.SystemException( e );
220
221         } catch (javax.transaction.SystemException JavaDoc e){
222             // TODO:3: Localize the message; add to Messages.java
223
logger.error("The TransactionManager reported an exception while attempting to rollback the transaction: "+e.getMessage());
224             throw new org.openejb.SystemException( e );
225         }
226     }
227     
228     protected void throwAppExceptionToServer( Throwable JavaDoc appException ) throws ApplicationException{
229         throw new ApplicationException( appException );
230     }
231
232     protected void throwTxExceptionToServer( Throwable JavaDoc sysException ) throws ApplicationException{
233         /* Throw javax.transaction.TransactionRolledbackException to remote client */
234         // TODO:3: Localize the message; add to Messages.java
235
String JavaDoc message = "The transaction was rolled back because the bean encountered a non-application exception :" + sysException.getClass().getName() + " : "+sysException.getMessage();
236         javax.transaction.TransactionRolledbackException JavaDoc txException = new javax.transaction.TransactionRolledbackException JavaDoc(message);
237
238         // See section 2.1.3.7 of the OpenEJB Specification
239
throw new InvalidateReferenceException( txException );
240
241         // TODO:3: throw javax.ejb.TransactionRolledbackLocalException to local client.
242
}
243
244     /**
245      * Throw RemoteException to remote client; throw EJBException to local client.
246      *
247      * @param sysException
248      * @exception ApplicationException
249      */

250     protected void throwExceptionToServer( Throwable JavaDoc sysException ) throws ApplicationException{
251         // Throw RemoteException to remote client.
252
// TODO:3: Localize the message; add to Messages.java
253
RemoteException JavaDoc re = new RemoteException JavaDoc("The bean encountered a non-application exception.", sysException);
254         
255         // See section 2.1.3.7 of the OpenEJB Specification
256
throw new InvalidateReferenceException( re );
257
258         // TODO:3: throw EJBException to local client.
259

260     }
261     
262     protected void logSystemException(Throwable JavaDoc sysException){
263         // TODO:2: Put information about the instance and deployment in the log message.
264
// TODO:3: Localize the message; add to Messages.java
265
logger.error( "The bean instances business method encountered a system exception:"+sysException.getMessage(), sysException);
266     }
267     
268     protected void discardBeanInstance(EnterpriseBean JavaDoc instance, ThreadContext callContext){
269         container.discardInstance( instance, callContext );
270     }
271     
272     protected void beginTransaction() throws javax.transaction.SystemException JavaDoc{
273         try {
274             getTxMngr( ).begin();
275             if(txLogger.isInfoEnabled()) {
276                 txLogger.info(policyToString()+"Started transaction "+getTxMngr( ).getTransaction());
277             }
278         } catch ( javax.transaction.NotSupportedException JavaDoc nse ) {
279             logger.error("", nse);
280         }
281     }
282
283
284     /**
285      * <B>18.3.3 Exceptions from container-invoked callbacks</B>
286      *
287      * <P>
288      * Handles the exceptions thrown from the from the following container-invoked
289      * callback methods of the enterprise bean.
290      *
291      * EntityBean:
292      * • ejbActivate()
293      * • ejbLoad()
294      * • ejbPassivate()
295      * • ejbStore()
296      * • setEntityContext(EntityContext)
297      * • unsetEntityContext()
298      *
299      * SessionBean:
300      * • ejbActivate()
301      * • ejbPassivate()
302      * • setSessionContext(SessionContext)
303      *
304      * MessageDrivenBean:
305      * • setMessageDrivenContext(MessageDrivenContext)
306      *
307      * SessionSynchronization interface:
308      * • afterBegin()
309      * • beforeCompletion()
310      * • and afterCompletion(boolean)
311      * </P>
312      * <P>
313      * The Container must handle all exceptions or errors from these methods
314      * as follows:
315      *
316      * • Log the exception or error to bring the problem to the attention of the
317      * System Administrator.
318      *
319      * • If the instance is in a transaction, mark the transaction for rollback.
320      *
321      * • Discard the instance. The Container must not invoke any business methods
322      * or container callbacks on the instance.
323      *
324      * • If the exception or error happened during the processing of a client
325      * invoked method, throw the java.rmi.RemoteException to the client if the
326      * client is a remote client or throw the javax.ejb.EJBException to the
327      * client if the client is a local client.
328      *
329      * If the instance executed in the client's transaction, the Container
330      * should throw the javax.transaction.TransactionRolledbackException to a
331      * remote client or the javax.ejb.TransactionRolledbackLocalException to a
332      * local client, because it provides more information to the client.
333      * (The client knows that it is fruitless to continue the transaction.)
334      */

335     protected void handleCallbackException(){
336     }
337 }
338
339
340
Popular Tags