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

Answers


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


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


Need Your Help

From reference element, get count of preceding element occurrences within ancestor

xml xslt xpath xslt-1.0

I am trying to determine the figure number contained to the current chapter from the reference to the figure.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.