Using Spring, why does the removal of the H2 transaction work correctly in embedded database mode, but not in server mode?

advertisements

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 [[email protected]]
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 [[email protected]] for key [org[email protected]3be9bb55] from thread [main]
2014-07-03 20:02:05,966 TRACE [org.springframework.transaction.support.TransactionSynchronizationManager] - Removed value [[email protected]] for key [[email protected]] from thread [main]
2014-07-03 20:02:05,966 DEBUG [org.springframework.orm.jpa.JpaTransactionManager] - Closing JPA EntityManager [[email protected]] 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


Need transaction manager bean definition for your dataSource

For local resource transaction manager;

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

For JTA transaction manager, see more spring doc here