Spring JUnit JPA Transaction not rolling back -
i trying test dao uses jpa entitymanager fetch , update entities. have marked unit test transactional , set defaultrollback property false. however, don't see transactions rolling @ end of test when throwing rune time exception. data getting persisted in db. here unit test code along spring configuration. missing havent been able identify what. btw, transaction resource_local in 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 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"/>
your configuration seems fine. there different reasons unexpected commit, maybe datasource autocommit mode or non transaction compliant database (mysql myisam ?)
did check thread why transactions not rolling when using springjunit4classrunner/mysql/spring/hibernate ?
Comments
Post a Comment