2010-09-14 100 views
0

我正在使用spring 3.0.3.RELEASE以及在JDK 1.6.0_21中運行的Apache Tomcat 6.0.29中的mybatis-3.0.2和mybatis-spring-1.0.0。春季聲明式事務管理不起作用

創建我DAO類和服務類和定義下面的聲明式事務控制 -

<tx:advice id="txAdvice" transaction-manager="transactionManager"> 
     <tx:attributes> 
      <tx:method name="*" propagation="REQUIRED" /> 
     </tx:attributes> 
    </tx:advice> 
    <aop:config> 
     <aop:pointcut id="dtxops" 
      expression="execution(* com.project.service.*.*(..))" /> 
     <aop:advisor advice-ref="txAdvice" pointcut-ref="dtxops" /> 
    </aop:config> 
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 
     <property name="dataSource"> 
      <ref bean="dataSource" /> 
     </property> 
    </bean> 

這種方法是在使用ItemDAO類com.project.service.ItemDAOServiceImpl。 SystemException是一個RunTimeException。我通過2個ID來刪除,系統中存在一個ID,其他ID不存在。由於一個id不存在,我得到SystemException,但是當我檢查數據庫時,其他id被刪除而不是回滾。

public void deleteItem(List<Integer> itemIds) { 
     for (int itemId : itemIds) { 
      try { 
       int result = itemDAO.delete(itemId); 
       if (result != 1) { 
        throw new SystemException(
          "Failed to delete item"); 
       } 
      } catch (DataAccessException dae) { 
       log.error("Failed to delete item", dae); 
       throw new SystemException("Failed to delete items"); 
      } 
     } 
    } 

回答

2

事務配置是圍繞itemDao嗎?所以每個itemDAO.delete調用是一個單獨的事務。所以如果找到第一個id,它會在一個txn中被刪除。對於第二個它不會找到,異常是拋出txn - 沒有回滾。

這聽起來像你需要設置tXn圍繞deleteItem方法。

+0

此外,我會推薦Spring註釋這種事情。很容易明白交易邊界的位置,而不是推斷切入點在哪裏應用和不應用。 – 2012-05-11 13:42:55

0

注意在每個語句後提交的數據源的Tomcats自動提交設置。這聽起來有點像我。我曾經遇到過這個問題,沒有樂趣...