Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Spring, why can H2 transaction rollback work correctly in embedded-database mode, but not in server mode?

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");

}

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

Why can transaction rollback work correctly in H2 embedded-database mode, but not in server mode ?

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 debug log says transaction is rolledback:

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

it seems to me, there is sth nok with h2 standalone mode

like image 925
bastiat Avatar asked Nov 10 '22 05:11

bastiat


1 Answers

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

like image 69
bastiat Avatar answered Nov 15 '22 05:11

bastiat