1 16 package org.mortbay.j2ee.session; 17 18 20 import javax.naming.Context ; 21 import javax.naming.InitialContext ; 22 import javax.naming.NamingException ; 23 import javax.transaction.SystemException ; 24 import javax.transaction.Transaction ; 25 import javax.transaction.TransactionManager ; 26 27 import org.jfox.ioc.logger.Logger; 28 29 31 36 public class TransactionInterceptor 37 extends AroundInterceptor 38 { 39 protected static final Logger _log=Logger.getLogger(TransactionInterceptor.class); 40 protected final ThreadLocal _theirTransaction =new ThreadLocal (); 41 protected Context _ctx; 42 43 public 44 TransactionInterceptor() 45 { 46 super(); 47 48 try 49 { 50 _ctx=new InitialContext (); 51 } 52 catch (Exception e) 53 { 54 _log.error("could not create InitialContext", e); 55 } 56 } 57 58 protected TransactionManager 59 getTransactionManager() 60 { 61 try 62 { 63 return (TransactionManager )_ctx.lookup("java:/TransactionManager"); 64 } 65 catch (NamingException e) 66 { 67 _log.error("could not find TransactionManager", e); 68 } 69 70 return null; 71 } 72 73 protected void 75 before() 76 { 77 try 78 { 79 Transaction tx=getTransactionManager().suspend(); 80 _theirTransaction.set(tx); 81 } 82 catch (SystemException e) 83 { 84 _log.error("could not disassociate UserTransaction from current thread", e); 85 } 86 } 87 88 protected void 89 after() 90 { 91 try 92 { 93 Transaction tx=(Transaction )_theirTransaction.get(); 94 getTransactionManager().resume(tx); 95 } 96 catch (Exception e) 97 { 98 _log.error("could not re-associate UserTransaction with current thread", e); 99 } 100 finally 101 { 102 _theirTransaction.set(null); 103 } 104 } 105 106 } 108 | Popular Tags |