KickJava   Java API By Example, From Geeks To Geeks.

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


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: TxManditory.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 /**
53  * 17.6.2.5 Mandatory
54  *
55  * The Container must invoke an enterprise Bean method whose transaction
56  * attribute is set to Mandatory in a client's transaction context. The client
57  * is required to call with a transaction context.
58  *
59  * • If the client calls with a transaction context, the container invokes the
60  * enterprise Bean's method in the client's transaction context.
61  *
62  * • If the client calls without a transaction context, the Container throws
63  * the javax.transaction.TransactionRequiredException exception if the
64  * client is a remote client, or the
65  * javax.ejb.TransactionRequiredLocalException if the client is a local
66  * client.
67  *
68  * @author <a HREF="mailto=david.blevins@visi.com">David Blevins</a>
69  * @version $Revision: 1912 $ $Date: 2005-06-16 15:29:56 -0700 (Thu, 16 Jun 2005) $
70  */

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

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

164     public void handleSystemException( Throwable JavaDoc sysException, EnterpriseBean JavaDoc instance, TransactionContext context) throws org.openejb.ApplicationException, org.openejb.SystemException{
165         
166         /* [1] Log the system exception or error *********/
167         logSystemException( sysException );
168         
169         /* [2] Mark the transaction for rollback. ********/
170         markTxRollbackOnly( context.currentTx );
171         
172         /* [3] Discard instance. *************************/
173         discardBeanInstance( instance, context.callContext );
174         
175         /* [4] TransactionRolledbackException to client **/
176         throwTxExceptionToServer( sysException );
177     }
178
179
180 }
181
182
Popular Tags