KickJava   Java API By Example, From Geeks To Geeks.

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


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

45 package org.openejb.core.transaction;
46
47 import javax.ejb.EnterpriseBean JavaDoc;
48 import javax.transaction.Status JavaDoc;
49
50 import org.openejb.ApplicationException;
51
52 /**
53  * 17.6.2.2 Required
54  *
55  * The Container must invoke an enterprise Bean method whose transaction
56  * attribute is set to Required with a valid transaction context.
57  *
58  * If a client invokes the enterprise Bean's method while the client is
59  * associated with a transaction context, the container invokes the enterprise
60  * Bean's method in the client's transaction context.
61  *
62  * If the client invokes the enterprise Bean's method while the client is not
63  * associated with a transaction context, the container automatically starts a
64  * new transaction before delegating a method call to the enterprise Bean
65  * business method. The Container automatically enlists all the resource
66  * managers accessed by the business method with the transaction. If the
67  * business method invokes other enterprise beans, the Container passes the
68  * transaction context with the invocation. The Container attempts to commit
69  * the transaction when the business method has completed. The container
70  * performs the commit protocol before the method result is sent to the client.
71  *
72  * @author <a HREF="mailto=david.blevins@visi.com">David Blevins</a>
73  * @version $Revision: 1912 $ $Date: 2005-06-16 15:29:56 -0700 (Thu, 16 Jun 2005) $
74  */

75 public class TxRequired extends TransactionPolicy {
76     
77     public TxRequired(TransactionContainer container){
78         this();
79         this.container = container;
80     }
81
82     public TxRequired(){
83         policyType = Required;
84     }
85     
86     public String JavaDoc policyToString() {
87         return "TX_Required: ";
88     }
89     
90     public void beforeInvoke(EnterpriseBean JavaDoc instance, TransactionContext context) throws org.openejb.SystemException, org.openejb.ApplicationException{
91         
92         try {
93         
94             context.clientTx = getTxMngr().getTransaction();
95
96             if ( context.clientTx == null ) {
97                 beginTransaction();
98             }
99
100             context.currentTx = getTxMngr().getTransaction();
101
102         } catch ( javax.transaction.SystemException JavaDoc se ) {
103             logger.error("Exception during getTransaction()", se);
104             throw new org.openejb.SystemException(se);
105         }
106     }
107
108     public void afterInvoke(EnterpriseBean JavaDoc instance, TransactionContext context) throws org.openejb.ApplicationException, org.openejb.SystemException{
109
110         try {
111             if ( context.clientTx != null ) return;
112             // we created a new transaction in beforeInvoke, which must be ended.
113
if ( context.currentTx.getStatus() == Status.STATUS_ACTIVE ) {
114                 commitTransaction( context.currentTx );
115             } else {
116                 rollbackTransaction( context.currentTx );
117             }
118
119         } catch ( javax.transaction.SystemException JavaDoc se ) {
120             logger.error("Exception during getTransaction()", se);
121             throw new org.openejb.SystemException(se);
122         }
123     }
124
125     /**
126      * <B>Container's action</B>
127      *
128      * <P>
129      * Re-throw AppException
130      * </P>
131      *
132      * <B>Client's view</B>
133      *
134      * <P>
135      * Client receives AppException. Can attempt to continue computation in the
136      * transaction, and eventually commit the transaction (the commit would fail
137      * if the instance called setRollbackOnly()).
138      * </P>
139      */

140     public void handleApplicationException( Throwable JavaDoc appException, TransactionContext context) throws ApplicationException{
141         // Re-throw AppException
142
throw new ApplicationException( appException );
143     }
144     
145     /**
146      * A system exception is any exception that is not an Application Exception.
147      * <BR>
148      * <B>Container's action</B>
149      *
150      * <P>
151      * <OL>
152      * <LI>
153      * Log the exception or error so that the System Administrator is alerted of
154      * the problem.
155      * </LI>
156      * <LI>
157      * Mark the transaction for rollback.
158      * </LI>
159      * <LI>
160      * Discard instance. The Container must not invoke any business methods or
161      * container callbacks on the instance.
162      * </LI>
163      * <LI>
164      * Throw javax.transaction.TransactionRolledbackException to remote client;
165      * throw javax.ejb.TransactionRolledbackLocalException to local client.
166      * </LI>
167      * </OL>
168      *
169      * </P>
170      *
171      * <B>Client's view</B>
172      *
173      * <P>
174      * Receives javax.transaction.TransactionRolledbackException or
175      * javax.ejb.TransactionRolledbackLocalException.
176      *
177      * Continuing transaction is fruitless.
178      * </P>
179      */

180     public void handleSystemException( Throwable JavaDoc sysException, EnterpriseBean JavaDoc instance, TransactionContext context) throws org.openejb.ApplicationException, org.openejb.SystemException{
181         
182             /* [1] Log the system exception or error **********/
183             logSystemException( sysException );
184
185         boolean runningInContainerTransaction = (!context.currentTx.equals( context.clientTx ));
186         if (runningInContainerTransaction) {
187             /* [2] Mark the transaction for rollback. afterInvoke() will roll it back */
188             markTxRollbackOnly( context.currentTx );
189
190             /* [3] Discard instance. **************************/
191             discardBeanInstance( instance, context.callContext);
192
193             /* [4] Throw RemoteException to client ************/
194             throwExceptionToServer( sysException );
195         } else {
196             /* [2] Mark the transaction for rollback. *********/
197             markTxRollbackOnly( context.clientTx );
198             
199             /* [3] Discard instance. **************************/
200             discardBeanInstance( instance, context.callContext);
201             
202             /* [4] Throw TransactionRolledbackException to client ************/
203             throwTxExceptionToServer( sysException );
204         }
205     }
206 }
207
Popular Tags