Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用Spring,为什么H2事务回滚可以在嵌入式数据库模式下正常工作,而不能在服务器模式下正常工作?_Java_Spring_Transactions_H2_Spring Transactions - Fatal编程技术网

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

Java 使用Spring,为什么H2事务回滚可以在嵌入式数据库模式下正常工作,而不能在服务器模式下正常工作?,java,spring,transactions,h2,spring-transactions,Java,Spring,Transactions,H2,Spring Transactions,一个简单的spring服务,方法是执行插入并在插入后引发运行时异常。 运行时异常应导致回滚 @Transactional() public void insertAndThrowRuntimeEx() { Order order = new Order(); entityManager.persist(order); throw new RuntimeException("Unexpected runtime exception"); } 仅当我使用以下配置数

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

@Transactional()
public void insertAndThrowRuntimeEx()  {

    Order order = new Order();

    entityManager.persist(order);

    throw new RuntimeException("Unexpected runtime exception");

}
仅当我使用以下配置数据源时,回滚才会正确显示:

<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="" />

需要为您的
数据源定义事务管理器bean

用于本地资源事务管理器

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


有关JTA事务管理器,请参阅更多spring文档,问题在于H2处于独立模式。
使用mysql的相同代码和配置与预期的一样工作(事务性,带回滚)。

thx对于回复,很抱歉我没有写之前的内容,但是配置了tx管理器,它工作正常,但只有当数据库处于内存模式时,您才尝试使用datasource属性
defaultAutoCommit
false
?否则,您可能需要确保事务管理器配置。是的,我已尝试使用defaultAutoCommit=false,它没有影响。在我看来,h2有点不好,在日志中我看到spring事务回滚,我添加了完整的日志来发布描述。在日志中你看到事务回滚已经启动了吗?是的,我看到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
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
   <property name="dataSource" ref="dataSource"/>
</bean>