我正在使用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");
}
}
}
此外,我會推薦Spring註釋這種事情。很容易明白交易邊界的位置,而不是推斷切入點在哪裏應用和不應用。 – 2012-05-11 13:42:55