[英]Spring declarative transaction management not working
I am using spring 3.0.3.RELEASE along with mybatis-3.0.2 and mybatis-spring-1.0.0 running in Apache Tomcat 6.0.29 with JDK 1.6.0_21. 我正在使用Spring 3.0.3.RELEASE以及mybatis-3.0.2和mybatis-spring-1.0.0,它们在带有JDK 1.6.0_21的Apache Tomcat 6.0.29中运行。
I created my DAO class and Service class and defined following declarative transaction control - 我创建了我的DAO类和Service类,并按照声明式事务控制进行了定义-
<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>
This method is in the class com.project.service.ItemDAOServiceImpl which uses ItemDAO. 此方法在使用ItemDAO的com.project.service.ItemDAOServiceImpl类中。 The SystemException is a RunTimeException.
SystemException是RunTimeException。 I pass 2 ids to be deleted, one id exists in the system and other does not.
我传递了2个要删除的ID,系统中存在一个ID,其他ID不存在。 Since one id does not exists I get the SystemException but when I check the database the other id is deleted instead of a rollback.
由于一个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");
}
}
}
The transaction config is around the itemDao right? 事务配置在itemDao周围吗? So each itemDAO.delete call is a separate transaction.
因此,每个itemDAO.delete调用都是一个单独的事务。 So if the first id is found it is deleted in one txn.
因此,如果找到第一个ID,则会在一个txn中将其删除。 For second one it wont find, the exception is thrown outside the txn - no rollback.
对于第二个它不会发现,该异常被抛出到txn之外-没有回滚。
It sounds like you need to setup the txn around deleteItem method instead. 听起来您需要在deleteItem方法周围设置txn。
Watch out for Tomcats auto-commit setting for datasources which commits after each statement. 注意在每个语句之后提交的数据源的Tomcats自动提交设置。 It sounds a bit like that to me.
对我来说听起来有点像。 I have faced that problem once, not fun...
我曾经遇到过这个问题,不好玩...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.