1 17 package org.apache.geronimo.tomcat.valve; 18 19 import java.io.IOException ; 20 import javax.servlet.ServletException ; 21 import javax.transaction.HeuristicMixedException ; 22 import javax.transaction.HeuristicRollbackException ; 23 import javax.transaction.RollbackException ; 24 import javax.transaction.SystemException ; 25 26 import org.apache.catalina.connector.Request; 27 import org.apache.catalina.connector.Response; 28 import org.apache.catalina.valves.ValveBase; 29 import org.apache.geronimo.transaction.context.TransactionContext; 30 import org.apache.geronimo.transaction.context.TransactionContextManager; 31 32 35 public class TransactionContextValve extends ValveBase { 36 37 private final TransactionContextManager transactionContextManager; 38 39 public TransactionContextValve(TransactionContextManager transactionContextManager){ 40 this.transactionContextManager = transactionContextManager; 41 } 42 43 public void invoke(Request request, Response response) throws IOException , ServletException { 44 45 TransactionContext oldTransactionContext = transactionContextManager.getContext(); 46 TransactionContext newTransactionContext = null; 47 48 if (oldTransactionContext == null || !oldTransactionContext.isInheritable()) { 49 newTransactionContext = transactionContextManager.newUnspecifiedTransactionContext(); 50 } 51 52 getNext().invoke(request, response); 54 55 try { 56 if (newTransactionContext != null) { 57 if (newTransactionContext != transactionContextManager.getContext()) { 58 transactionContextManager.getContext().rollback(); 59 newTransactionContext.rollback(); 60 throw new RuntimeException ("WRONG EXCEPTION! returned from servlet call with wrong tx context"); 61 } 62 newTransactionContext.commit(); 63 64 } else { 65 if (oldTransactionContext != transactionContextManager.getContext()) { 66 if (transactionContextManager.getContext() != null) { 67 transactionContextManager.getContext().rollback(); 68 } 69 throw new RuntimeException ("WRONG EXCEPTION! returned from servlet call with wrong tx context"); 70 } 71 } 72 } catch (SystemException e) { 73 throw new RuntimeException ("WRONG EXCEPTION!", e); 74 } catch (HeuristicMixedException e) { 75 throw new RuntimeException ("WRONG EXCEPTION!", e); 76 } catch (HeuristicRollbackException e) { 77 throw new RuntimeException ("WRONG EXCEPTION!", e); 78 } catch (RollbackException e) { 79 throw new RuntimeException ("WRONG EXCEPTION!", e); 80 } finally { 81 transactionContextManager.setContext(oldTransactionContext); 83 } 84 } 85 } 86 | Popular Tags |