user320587
user320587

Reputation: 1367

Spring JUnit JPA Transaction not rolling back

I am trying to test my DAO that uses JPA EntityManager to fetch and update entities. I have marked my unit test as Transactional and set the defaultRollback property to false. However, I don't see my transactions rolling back at the end of the test when throwing a rune time exception. The data is getting persisted in the DB. Here is my unit test code along with spring configuration. I am clearly missing something but havent been able to identify what. Btw, the transaction is RESOURCE_LOCAL in the persistence.xml

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:spring/test-jpa.xml"})
@TestExecutionListeners(
{   DependencyInjectionTestExecutionListener.class,
TransactionalTestExecutionListener.class,
DbUnitTestExecutionListener.class
})
@TransactionConfiguration(defaultRollback=false)
@Transactional
public class JpaTests {
    @PersistenceContext
    EntityManage em;

    @Test
    public void testTransactionQueueManager() {
        Object entity = em.find(1);
        //code to update entity omitted.
    entity = em.merge(entity);
    em.flush();
        throw new RuntimeException
    }
}

Spring Configuration

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
    destroy-method="close">
    <property name="driverClassName" value="${jpa.driverclassname}" />
    <property name="url" value="${jpa.url}" />
    <property name="username" value="${jpa.username}" />
    <property name="password" value="${jpa.password}" />
</bean>

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="persistenceUnitName" value="${jpa.persistenceunitname}"/>
    <property name="dataSource" ref="dataSource" />
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.OpenJpaVendorAdapter">
            <property name="databasePlatform" value="org.apache.openjpa.jdbc.sql.DBDictionary"/>
        </bean>
    </property>
</bean> 

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>

<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/>

Upvotes: 4

Views: 4886

Answers (3)

okin2014
okin2014

Reputation: 323

Adding rollbackFor may help, it's a common pitfall.

@Transactional(rollbackFor=Exception.class)

Upvotes: 0

Radha
Radha

Reputation: 1

@TransactionConfiguration(defaultRollback=false)

might be the culprit. Try defaultRollback=true, that should rollback the transaction.

Upvotes: 0

Gab
Gab

Reputation: 8323

Your configuration seems fine. There could be different reasons for the unexpected commit, maybe a datasource with autocommit mode or a non transaction compliant database (mysql with MyISAM ?)

Did you check this thread Why are transactions not rolling back when using SpringJUnit4ClassRunner/MySQL/Spring/Hibernate ?

Upvotes: 1

Related Questions