2012-05-14 18 views
1

我使用spring,oracle和jotm(在tomcat中)使用2PC提交。 下面是彈簧配置。JOTM getTransactionManager()。getTransaction()返回NULL

<aop:config> 
    <aop:pointcut id="defaultOperation" 
     expression="execution(* jatis.avantrade.foundation.model.engine.*.*(..))" /> 
    <aop:advisor advice-ref="defaultTrxAdvice" pointcut-ref="defaultOperation" /> 
</aop:config> 

<tx:advice id="defaultTrxAdvice" transaction-manager="trxManager"> 
    <tx:attributes> 
     <tx:method name="check*" read-only="true" /> 
     <tx:method name="get*" read-only="true" /> 
     <tx:method name="is*" read-only="true" /> 
     <tx:method name="load*" read-only="true" /> 
     <tx:method name="select*" read-only="true" /> 
     <tx:method name="count*" read-only="true" /> 
     <tx:method name="search*" read-only="true" /> 
     <tx:method name="list*" read-only="true" /> 
     <tx:method name="*" rollback-for="Throwable" /> 
    </tx:attributes> 
</tx:advice> 

<bean id="txImpl" class="org.springframework.transaction.jta.JotmFactoryBean" /> 




<bean id="trxManager" 
    class="org.springframework.transaction.jta.JtaTransactionManager"> 
    <property name="transactionManager" ref="txImpl" /> 
    <property name="userTransaction" ref="txImpl" /> 
</bean> 

的問題是,當我使用JOTM對象調用getTransactionManager().getTransaction(),則返回null。

Current cur = (Current) ContextHelper.getApplicationContext() 
        .getBean("txImpl"); 
      try { 
       log.error("cur : " cur.getTransactionManager().getTransaction()); 
     } catch (SystemException e) { 
      log.error(e.getMessage(), e); 
     } 

我該如何解決這個問題?

回答

1

一種方法可能是使用Apache TomEE,它是已集成TransactionManager的Tomcat。編寫一個簡單的Spring工廠bean來處理TransactionManager和UserTransaction。

兩者都可以從JNDI進行查找:

  • java:comp/TransactionManager
  • java:comp/UserTransaction

那家工廠將只需更換從上面的配置之一:

<bean id="txImpl" class="org.foo.MyTransactionFactoryBean" /> 

雖然,很可能Spring已經有了一個因素y bean用於通過這些名稱查找TransactionManager。