gpt4 book ai didi

java - 使用Spring,为什么H2事务回滚在嵌入式数据库模式下可以正常工作,而在服务器模式下却不能?

转载 作者:行者123 更新时间:2023-12-01 13:50:19 26 4
gpt4 key购买 nike

一个简单的 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 调试日志说事务已回滚:

现在抛出运行时异常
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 独立模式有 sth nok

最佳答案

您的 dataSource 需要事务管理器 bean 定义

对于本地资源事务管理器;

<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>

对于 JTA 事务管理器,请参阅更多 spring 文档 here

关于java - 使用Spring,为什么H2事务回滚在嵌入式数据库模式下可以正常工作,而在服务器模式下却不能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24484180/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com