我正在使用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