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.
I created my DAO class and Service class and defined following declarative transaction control -
<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. The SystemException is a RunTimeException. I pass 2 ids to be deleted, one id exists in the system and other does not. 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.
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? So each itemDAO.delete call is a separate transaction. So if the first id is found it is deleted in one txn. For second one it wont find, the exception is thrown outside the txn - no rollback.
It sounds like you need to setup the txn around deleteItem method instead.
Watch out for Tomcats auto-commit setting for datasources which commits after each statement. It sounds a bit like that to me. I have faced that problem once, not fun...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.