使用Spring,为什么H2事务回滚在嵌入式数据库模式下可以正常工作,而在服务器模式下却不能正常工作? [英] Using Spring, why can H2 transaction rollback work correctly in embedded-database mode, but not in server mode?

查看:330
本文介绍了使用Spring,为什么H2事务回滚在嵌入式数据库模式下可以正常工作,而在服务器模式下却不能正常工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个简单的spring服务,具有执行插入操作并在插入后引发运行时异常的方法. 运行时异常应导致回滚.

A simple spring service with method doing insert and throwing Runtime exception after insert. Runtime exception should cause rollback.

@Transactional()
public void insertAndThrowRuntimeEx()  {

    Order order = new Order();

    entityManager.persist(order);

    throw new RuntimeException("Unexpected runtime exception");

}

仅当我使用以下方式配置dataSource时,回滚才能正确显示:

Rollback appears correctly only when I configure dataSource with:

<jdbc:embedded-database id="dataSource" type="H2" /> <!-- with this configuration there is correct rollback -->

但是当我在独立模式下使用数据库时,没有回滚,或者回滚无效:

But when I use database in standalone mode, there is no rollback, or rollback is not effective:

<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, 还有配置的交易管理器

ps, there is also transaction manager configured

    @Bean
public PlatformTransactionManager transactionManager() {
    JpaTransactionManager manager = new JpaTransactionManager( localContainerEntityManagerFactoryBean().getObject() );
    return manager;
}

    <tx:annotation-driven transaction-manager="transactionManager" />

spring调试日志显示事务已回滚:

now thrownig运行时异常

now thrownig runtime exception

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独立模式下还存在其他问题

推荐答案

问题在于独立模式下的H2. 使用mysql的相同代码和配置按预期方式工作(通过回滚进行事务处理).

Problem is with H2 in standalone mode. The same code and configuration using mysql works as expected (transactionally, with rollback).

这篇关于使用Spring,为什么H2事务回滚在嵌入式数据库模式下可以正常工作,而在服务器模式下却不能正常工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆