一个简单的spring服务,其方法是在插入后执行插入和抛出运行时异常。 运行时异常应该导致回滚。
@Transactional()
public void insertAndThrowRuntimeEx() {
Order order = new Order();
entityManager.persist(order);
throw new RuntimeException("Unexpected runtime exception");
}
仅当我使用以下命令配置dataSource时,才能正确显示回滚:
<jdbc:embedded-database id="dataSource" type="H2" /> <!-- with this configuration there is correct rollback -->
但是当我在独立模式下使用数据库时,没有回滚,或者回滚无效:
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" <!-- here inserted record remains in database -->
p:driverClassName="org.h2.Driver" p:url="jdbc:h2:tcp://localhost/databases/test1"
p:username="sa" p:password="" />
为什么事务回滚在H2嵌入式数据库模式下可以正常工作,但在服务器模式下却不能正常工作?
PS, 还配置了事务管理器
@Bean
public PlatformTransactionManager transactionManager() {
JpaTransactionManager manager = new JpaTransactionManager( localContainerEntityManagerFactoryBean().getObject() );
return manager;
}
<tx:annotation-driven transaction-manager="transactionManager" />
spring debug log表示事务已回退:
现在抛出运行时异常
2014-07-03 20:02:05,965 TRACE [org.springframework.transaction.interceptor.TransactionInterceptor] - Completing transaction for [com.javatech.training.OrderServiceImpl.insertAndThrowRuntimeEx] after exception: java.lang.RuntimeException: Unexpected runtime exception
2014-07-03 20:02:05,965 TRACE [org.springframework.transaction.interceptor.RuleBasedTransactionAttribute] - Applying rules to determine whether transaction should rollback on java.lang.RuntimeException: Unexpected runtime exception
2014-07-03 20:02:05,965 TRACE [org.springframework.transaction.interceptor.RuleBasedTransactionAttribute] - <Winning rollback rule is: null>
2014-07-03 20:02:05,965 TRACE [org.springframework.transaction.interceptor.RuleBasedTransactionAttribute] - No relevant rollback rule found: applying default rules
2014-07-03 20:02:05,965 TRACE [org.springframework.orm.jpa.JpaTransactionManager] - Triggering beforeCompletion synchronization
2014-07-03 20:02:05,965 DEBUG [org.springframework.orm.jpa.JpaTransactionManager] - Initiating transaction rollback
2014-07-03 20:02:05,965 DEBUG [org.springframework.orm.jpa.JpaTransactionManager] - Rolling back JPA transaction on EntityManager [org.hibernate.ejb.EntityManagerImpl@1affcbf9]
2014-07-03 20:02:05,966 TRACE [org.springframework.orm.jpa.JpaTransactionManager] - Triggering afterCompletion synchronization
2014-07-03 20:02:05,966 TRACE [org.springframework.transaction.support.TransactionSynchronizationManager] - Clearing transaction synchronization
2014-07-03 20:02:05,966 TRACE [org.springframework.transaction.support.TransactionSynchronizationManager] - Removed value [org.springframework.orm.jpa.EntityManagerHolder@2f216eaf] for key [org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean@3be9bb55] from thread [main]
2014-07-03 20:02:05,966 TRACE [org.springframework.transaction.support.TransactionSynchronizationManager] - Removed value [org.springframework.jdbc.datasource.ConnectionHolder@412eb15f] for key [org.apache.commons.dbcp.BasicDataSource@24915432] from thread [main]
2014-07-03 20:02:05,966 DEBUG [org.springframework.orm.jpa.JpaTransactionManager] - Closing JPA EntityManager [org.hibernate.ejb.EntityManagerImpl@1affcbf9] after transaction
2014-07-03 20:02:05,966 DEBUG [org.springframework.orm.jpa.EntityManagerFactoryUtils] - Closing JPA EntityManager
CATCH Exception: Unexpected runtime exception
在我看来,h2独立模式还有......
答案 0 :(得分:0)
需要dataSource
对于本地资源事务管理器;
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
对于JTA事务管理器,请参阅更多spring doc here
答案 1 :(得分:0)
在独立模式下H2存在问题。 使用mysql的相同代码和配置按预期工作(事务上,使用回滚)。