KickJava   Java API By Example, From Geeks To Geeks.

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


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: TxRequiresNew.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.4 RequiresNew
54  *
55  * The Container must invoke an enterprise Bean method whose transaction
56  * attribute is set to RequiresNew with a new transaction context.
57  *
58  * If the client invokes the enterprise Bean’s method while the client is not
59  * associated with a transaction context, the container automatically starts a
60  * new transaction before delegating a method call to the enterprise Bean
61  * business method. The Container automatically enlists all the resource
62  * managers accessed by the business method with the transaction. If the
63  * business method invokes other enterprise beans, the Container passes the
64  * transaction context with the invocation. The Container attempts to commit
65  * the transaction when the business method has completed. The container
66  * performs the commit protocol before the method result is sent to the client.
67  *
68  * If a client calls with a transaction context, the container suspends the
69  * association of the transaction context with the current thread before
70  * starting the new transaction and invoking the business method. The container
71  * resumes the suspended transaction association after the business method and
72  * the new transaction have been completed.
73  *
74  * @author <a HREF="mailto=david.blevins@visi.com">David Blevins</a>
75  * @version $Revision: 1912 $ $Date: 2005-06-16 15:29:56 -0700 (Thu, 16 Jun 2005) $
76  */

77 public class TxRequiresNew extends TransactionPolicy {
78     
79     public TxRequiresNew(TransactionContainer container){
80         this();
81         this.container = container;
82     }
83
84     public TxRequiresNew(){
85         policyType = RequiresNew;
86     }
87     
88     public String JavaDoc policyToString() {
89         return "TX_RequiresNew: ";
90     }
91     
92     public void beforeInvoke(EnterpriseBean JavaDoc instance, TransactionContext context) throws org.openejb.SystemException, org.openejb.ApplicationException{
93         
94         try {
95         
96             // if no transaction ---> suspend returns null
97
context.clientTx = suspendTransaction();
98             beginTransaction();
99             context.currentTx = getTxMngr().getTransaction();
100         
101         } catch ( javax.transaction.SystemException JavaDoc se ) {
102             throw new org.openejb.SystemException(se);
103         }
104     
105     }
106
107     public void afterInvoke(EnterpriseBean JavaDoc instance, TransactionContext context) throws org.openejb.ApplicationException, org.openejb.SystemException{
108
109         try {
110             // Commit or rollback
111
if ( context.currentTx.getStatus() == Status.STATUS_ACTIVE ) {
112                 commitTransaction( context.currentTx );
113             } else {
114                 rollbackTransaction( context.currentTx );
115             }
116         
117         } catch ( javax.transaction.SystemException JavaDoc se ) {
118             throw new org.openejb.SystemException(se);
119         } finally {
120             if ( context.clientTx != null ) {
121                 resumeTransaction( context.clientTx );
122             } else if(txLogger.isInfoEnabled()) {
123                 txLogger.info(policyToString()+"No transaction to resume");
124             }
125         }
126     }
127
128     /**
129      * <B>Container's action</B>
130      *
131      * <P>
132      * If the instance called setRollbackOnly(), then rollback the transaction,
133      * and re-throw AppException.
134      *
135      * Otherwise, attempt to commit the transaction, and then re-throw
136      * AppException.
137      * </P>
138      *
139      * <B>Client's view</B>
140      *
141      * <P>
142      * Receives AppException.
143      *
144      * If the client executes in a transaction, the client's transaction is not
145      * marked for rollback, and client can continue its work.
146      * </P>
147      */

148     public void handleApplicationException( Throwable JavaDoc appException, TransactionContext context) throws ApplicationException{
149         throw new ApplicationException( appException );
150     }
151     
152     /**
153      * A system exception is any exception that is not an Application Exception.
154      * <BR>
155      * <B>Container's action</B>
156      *
157      * <P>
158      * <OL>
159      * <LI>
160      * Log the exception or error so that the System Administrator is alerted of
161      * the problem.
162      * </LI>
163      * <LI>
164      * Rollback the container-started transaction.
165      * </LI>
166      * <LI>
167      * Discard instance. The Container must not invoke any business methods or
168      * container callbacks on the instance.
169      * </LI>
170      * <LI>
171      * Throw RemoteException to remote client;
172      * throw EJBException to local client.
173      * </LI>
174      * </OL>
175      *
176      * </P>
177      *
178      * <B>Client's view</B>
179      *
180      * <P>
181      * Receives RemoteException or EJBException
182      *
183      * If the client executes in a transaction, the client's transaction may or
184      * may not be marked for rollback.
185      * </P>
186      */

187     public void handleSystemException( Throwable JavaDoc sysException, EnterpriseBean JavaDoc instance, TransactionContext context) throws org.openejb.ApplicationException, org.openejb.SystemException{
188         
189         /* [1] Log the system exception or error **********/
190         logSystemException( sysException );
191
192         /* [2] afterInvoke will roll back the tx */
193         markTxRollbackOnly( context.currentTx );
194
195         /* [3] Discard instance. **************************/
196         discardBeanInstance( instance, context.callContext);
197
198         /* [4] Throw RemoteException to client ************/
199         throwExceptionToServer( sysException );
200
201     }
202 }
203
204
Popular Tags