KickJava   Java API By Example, From Geeks To Geeks.

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


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: TxSupports.java 1912 2005-06-16 22:29:56Z jlaskowski $
44  */

45 package org.openejb.core.transaction;
46
47 import javax.ejb.EnterpriseBean JavaDoc;
48
49 import org.openejb.ApplicationException;
50
51 /**
52  * 17.6.2.3 Supports
53  *
54  * The Container invokes an enterprise Bean method whose transaction attribute
55  * is set to Supports as follows.
56  *
57  * • If the client calls with a transaction context, the Container performs
58  * the same steps as described in the Required case.
59  *
60  * • If the client calls without a transaction context, the Container performs
61  * the same steps as described in the NotSupported case.
62  *
63  * The Supports transaction attribute must be used with caution. This is
64  * because of the different transactional semantics provided by the two
65  * possible modes of execution. Only the enterprise beans that will
66  * execute correctly in both modes should use the Supports transaction
67  * attribute.
68  *
69  * @author <a HREF="mailto=david.blevins@visi.com">David Blevins</a>
70  * @version $Revision: 1912 $ $Date: 2005-06-16 15:29:56 -0700 (Thu, 16 Jun 2005) $
71  */

72 public class TxSupports extends TransactionPolicy {
73     
74     public TxSupports(TransactionContainer container){
75         this();
76         this.container = container;
77     }
78
79     public TxSupports(){
80         policyType = Supports;
81     }
82     
83     public String JavaDoc policyToString() {
84         return "TX_Supports: ";
85     }
86     
87     public void beforeInvoke(EnterpriseBean JavaDoc instance, TransactionContext context) throws org.openejb.SystemException, org.openejb.ApplicationException{
88         
89         try {
90         
91             context.clientTx = getTxMngr().getTransaction();
92             context.currentTx = context.clientTx;
93         
94         } catch ( javax.transaction.SystemException JavaDoc se ) {
95             throw new org.openejb.SystemException(se);
96         }
97     }
98
99     public void afterInvoke(EnterpriseBean JavaDoc instance, TransactionContext context) throws org.openejb.ApplicationException, org.openejb.SystemException{
100
101     }
102
103     /**
104      * <B>Container's action</B>
105      *
106      * <P>
107      * Re-throw AppException
108      * </P>
109      *
110      * <B>Client's view</B>
111      *
112      * <P>
113      * Client receives AppException. Can attempt to continue computation in the
114      * transaction, and eventually commit the transaction (the commit would fail
115      * if the instance called setRollbackOnly()).
116      * </P>
117      */

118     public void handleApplicationException( Throwable JavaDoc appException, TransactionContext context) throws ApplicationException{
119         // [1] Re-throw AppException
120
throw new ApplicationException( appException );
121     }
122     
123     /**
124      * A system exception is any exception that is not an Application Exception.
125      * <BR>
126      * <B>Container's action</B>
127      *
128      * <P>
129      * <OL>
130      * <LI>
131      * Log the exception or error so that the System Administrator is alerted of
132      * the problem.
133      * </LI>
134      * <LI>
135      * Mark the transaction for rollback.
136      * </LI>
137      * <LI>
138      * Discard instance. The Container must not invoke any business methods or
139      * container callbacks on the instance.
140      * </LI>
141      * <LI>
142      * Throw javax.transaction.TransactionRolledbackException to remote client;
143      * throw javax.ejb.TransactionRolledbackLocalException to local client.
144      * </LI>
145      * </OL>
146      *
147      * </P>
148      *
149      * <B>Client's view</B>
150      *
151      * <P>
152      * Receives javax.transaction.TransactionRolledbackException or
153      * javax.ejb.TransactionRolledbackLocalException.
154      *
155      * Continuing transaction is fruitless.
156      * </P>
157      */

158     public void handleSystemException( Throwable JavaDoc sysException, EnterpriseBean JavaDoc instance, TransactionContext context) throws org.openejb.ApplicationException, org.openejb.SystemException{
159         
160         
161         boolean runningInTransaction = ( context.currentTx != null );
162
163         if (runningInTransaction) {
164             /* [1] Log the system exception or error *********/
165             logSystemException( sysException );
166             
167             /* [2] Mark the transaction for rollback. ********/
168             markTxRollbackOnly( context.currentTx );
169             
170             /* [3] Discard instance. *************************/
171             discardBeanInstance(instance, context.callContext);
172             
173             /* [4] TransactionRolledbackException to client **/
174             throwTxExceptionToServer( sysException );
175         
176         } else {
177             /* [1] Log the system exception or error *********/
178             logSystemException( sysException );
179
180             /* [2] Discard instance. *************************/
181             discardBeanInstance( instance, context.callContext);
182
183             /* [3] Throw RemoteException to client ***********/
184             throwExceptionToServer( sysException );
185         }
186
187     }
188
189 }
190
191
Popular Tags