KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openejb > core > stateful > SessionSynchronizationTxPolicy


1 package org.openejb.core.stateful;
2
3 import javax.ejb.EnterpriseBean JavaDoc;
4 import javax.ejb.SessionSynchronization JavaDoc;
5
6 import org.openejb.ApplicationException;
7 import org.openejb.InvalidateReferenceException;
8 import org.openejb.core.transaction.TransactionContext;
9 import org.openejb.core.transaction.TransactionPolicy;
10 /**
11  * Don't wrap the following method transaction types
12  *
13  * TX_NEVER
14  * TX_NOT_SUPPORTED
15  *
16  * They never execute in a transaction and there is nothing to syncronize on.
17  *
18  * @author <a HREF="mailto=david.blevins@visi.com">David Blevins</a>
19  * @version $Revision: 1096 $ $Date: 2004-03-26 13:41:16 -0800 (Fri, 26 Mar 2004) $
20  */

21 public class SessionSynchronizationTxPolicy extends org.openejb.core.transaction.TransactionPolicy {
22     
23     protected TransactionPolicy policy;
24
25     public SessionSynchronizationTxPolicy(TransactionPolicy policy){
26         this.policy = policy;
27         this.container = policy.getContainer();
28         this.policyType = policy.policyType;
29         if(container instanceof org.openejb.Container &&
30            ((org.openejb.Container)container).getContainerType()!=org.openejb.Container.STATEFUL ||
31            policyType==TransactionPolicy.Never ||
32            policyType==TransactionPolicy.NotSupported) {
33             throw new IllegalArgumentException JavaDoc();
34         }
35         
36     }
37
38     public void beforeInvoke(EnterpriseBean JavaDoc instance, TransactionContext context) throws org.openejb.SystemException, org.openejb.ApplicationException{
39         policy.beforeInvoke( instance, context );
40
41         // This can happen with TxSupports policies
42
if( context.currentTx == null ) return;
43             
44         try{
45             SessionSynchronization JavaDoc session = (SessionSynchronization JavaDoc)instance;
46             SessionSynchronizationCoordinator.registerSessionSynchronization( session, context );
47         } catch (javax.transaction.RollbackException JavaDoc e){
48             logger.error("Cannot register the SessionSynchronization bean with the transaction, the transaction has been rolled back");
49             handleSystemException( e, instance, context );
50         } catch (javax.transaction.SystemException JavaDoc e){
51             logger.error("Cannot register the SessionSynchronization bean with the transaction, received an unknown system exception from the transaction manager: "+e.getMessage());
52             handleSystemException( e, instance, context );
53         } catch (Throwable JavaDoc e){
54             logger.error("Cannot register the SessionSynchronization bean with the transaction, received an unknown exception: "+e.getClass().getName()+" "+ e.getMessage());
55             handleSystemException( e, instance, context );
56         }
57     }
58
59     public void afterInvoke(EnterpriseBean JavaDoc instance, TransactionContext context) throws org.openejb.ApplicationException, org.openejb.SystemException{
60         policy.afterInvoke( instance, context );
61     }
62
63     public void handleApplicationException( Throwable JavaDoc appException, TransactionContext context) throws ApplicationException{
64         policy.handleApplicationException( appException, context );
65     }
66     
67     public void handleSystemException( Throwable JavaDoc sysException, EnterpriseBean JavaDoc instance, TransactionContext context) throws org.openejb.ApplicationException, org.openejb.SystemException{
68         try {
69             policy.handleSystemException( sysException, instance, context );
70         } catch ( ApplicationException e ){
71             throw new InvalidateReferenceException( e.getRootCause() );
72         }
73     }
74
75 }
76
77
Popular Tags