KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openejb > core > stateless > StatelessBeanManagedTxPolicy


1 package org.openejb.core.stateless;
2
3 import java.rmi.RemoteException JavaDoc;
4
5 import javax.ejb.EnterpriseBean JavaDoc;
6 import javax.transaction.Status JavaDoc;
7
8 import org.openejb.ApplicationException;
9 import org.openejb.core.transaction.TransactionContainer;
10 import org.openejb.core.transaction.TransactionContext;
11 import org.openejb.core.transaction.TransactionPolicy;
12
13 /**
14  * Transaction policy for Stateless Session beans with
15  * bean-managed transaction demarcation.
16  *
17  * @author <a HREF="mailto=david.blevins@visi.com">David Blevins</a>
18  * @version $Revision: 1096 $ $Date: 2004-03-26 13:41:16 -0800 (Fri, 26 Mar 2004) $
19  */

20 public class StatelessBeanManagedTxPolicy extends TransactionPolicy {
21     
22     public StatelessBeanManagedTxPolicy(TransactionContainer container){
23         this();
24         if(container instanceof org.openejb.Container &&
25            ((org.openejb.Container)container).getContainerType()!=org.openejb.Container.STATELESS) {
26             throw new IllegalArgumentException JavaDoc();
27         }
28            
29         this.container = container;
30     }
31
32     public StatelessBeanManagedTxPolicy(){
33         policyType = BeanManaged;
34     }
35     
36     public String JavaDoc policyToString() {
37         return "TX_BeanManaged: ";
38     }
39     /**
40      * When a client invokes a business method via the enterprise bean’s home
41      * or component interface, the Container suspends any transaction that may be
42      * associated with the client request.
43      *
44      * @param instance
45      * @param context
46      * @exception org.openejb.SystemException
47      * @exception org.openejb.ApplicationException
48      */

49     public void beforeInvoke(EnterpriseBean JavaDoc instance, TransactionContext context) throws org.openejb.SystemException, org.openejb.ApplicationException{
50             // if no transaction ---> suspend returns null
51
context.clientTx = suspendTransaction();
52     }
53
54     /**
55      * If a stateless session bean instance starts a transaction in a business
56      * method, it must commit the transaction before the business method returns.
57      *
58      * The Container must detect the case in which a transaction was started, but
59      * not completed, in the business method, and handle it as follows:
60      *
61      * • Log this as an application error to alert the system administrator.
62      * • Roll back the started transaction.
63      * • Discard the instance of the session bean.
64      * • Throw the java.rmi.RemoteException to the client if the client is a
65      * remote client, or throw the javax.ejb.EJBException if the client is a
66      * local client.
67      *
68      * @param instance
69      * @param context
70      * @exception org.openejb.ApplicationException
71      * @exception org.openejb.SystemException
72      */

73     public void afterInvoke(EnterpriseBean JavaDoc instance, TransactionContext context) throws org.openejb.ApplicationException, org.openejb.SystemException{
74         try {
75             /*
76             * The Container must detect the case in which a transaction was started, but
77             * not completed, in the business method, and handle it as follows:
78             */

79             context.currentTx = getTxMngr().getTransaction();
80
81             if (context.currentTx == null) return;
82             
83             if (context.currentTx.getStatus() != Status.STATUS_ROLLEDBACK && context.currentTx.getStatus() != Status.STATUS_COMMITTED ) {
84                 String JavaDoc message = "The stateless session bean started a transaction but did not complete it.";
85
86                 /* [1] Log this as an application error ********/
87                 logger.error( message );
88                 
89                 /* [2] Roll back the started transaction *******/
90                 try {
91                     rollbackTransaction( context.currentTx );
92                 } catch (Throwable JavaDoc t){
93                     // This exception was logged before leaving the method
94
}
95                 
96                 /* [3] Throw the RemoteException to the client */
97                 throwAppExceptionToServer( new RemoteException JavaDoc( message ));
98             }
99
100         } catch (javax.transaction.SystemException JavaDoc e){
101             throw new org.openejb.SystemException( e );
102         } finally {
103             resumeTransaction( context.clientTx );
104         }
105     }
106
107     /**
108      * <B>Container's action</B>
109      *
110      * <P>
111      * Re-throw AppException
112      * </P>
113      *
114      * <B>Client's view</B>
115      *
116      * <P>
117      * Client receives AppException.
118      * </P>
119      */

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

156     public void handleSystemException( Throwable JavaDoc sysException, EnterpriseBean JavaDoc instance, TransactionContext context) throws org.openejb.ApplicationException, org.openejb.SystemException{
157         try {
158             context.currentTx = getTxMngr().getTransaction();
159         } catch (javax.transaction.SystemException JavaDoc e ){
160             context.currentTx = null;
161         }
162
163         // Log the system exception or error
164
logSystemException( sysException );
165         
166         // Mark for rollback the instance's transaction if it is not completed.
167
if ( context.currentTx != null ) markTxRollbackOnly( context.currentTx );
168
169         // Discard instance.
170
discardBeanInstance( instance, context.callContext);
171
172         // Throw RemoteException to remote client; throw EJBException to local client.
173
throwExceptionToServer( sysException );
174         
175     }
176
177 }
178
179
Popular Tags