Spring集成测试不回滚
Posted
技术标签:
【中文标题】Spring集成测试不回滚【英文标题】:Spring integration test does not roll back 【发布时间】:2011-11-28 22:54:29 【问题描述】:我正在使用 Spring + Hibernate + H2。我在集成测试中执行数据库操作(通过调用服务类)。我希望 Spring 在每个测试方法之后回滚更改,但我无法让它工作。起初我使用 mysql(使用不支持事务的 MyISAM),但在更改为 H2 后问题仍然存在。我尝试了几个 DataSource 定义(在阅读它必须支持 XA 之后),但似乎没有任何帮助。
我将http://code.google.com/p/generic-dao/ 用于我的 DAO 类,并在我的服务层中使用 @Transactional-annotations(如果我删除它们,我会收到以下错误:javax.persistence.TransactionRequiredException: no transaction is in progress)。
我的测试类如下所示:
@ContextConfiguration("classpath:...spring.xml")
@Transactional
@TransactionConfiguration(transactionManager="transactionManager", defaultRollback=true)
public class DemoServiceTest extends AbstractTestNGSpringContextTests
@Autowired
private DemoService demoService;
@Test
public void addTest()
int size = demoService.findAll().size();
Test coach = new Test();
test.setName("Test");
testService.save(test);
Assert.assertEquals(size+1,testService.findAll().size());
这是我的弹簧配置:
<!-- <bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource"> -->
<!-- <property name="driverClassName" value="org.h2.Driver" /> -->
<!-- <property name="url" value="jdbc:h2:~/test" /> -->
<!-- <property name="username" value="sa" /> -->
<!-- <property name="password" value="" /> -->
<!-- </bean> -->
<!-- <bean id="myDataSource" class=" com.mchange.v2.c3p0.ComboPooledDataSource"> -->
<!-- <property name="driverClass" value="org.h2.Driver" /> -->
<!-- <property name="jdbcUrl" value="jdbc:h2:~/test" /> -->
<!-- <property name="user" value="sa" /> -->
<!-- <property name="password" value="" /> -->
<!-- </bean> -->
<bean id="myDataSource" class="com.atomikos.jdbc.nonxa.AtomikosNonXADataSourceBean">
<property name="uniqueResourceName" value="test" />
<property name="driverClassName" value="org.h2.Driver" />
<property name="url" value="jdbc:h2:~/test" />
<property name="user" value="sa" />
<property name="password" value="" />
<property name="maxPoolSize" value="20" />
<property name="reapTimeout" value="300" />
</bean>
<bean id="demoDAO" class="...DemoDAOImpl" />
<bean id="demoService" class="...DemoServiceImpl" />
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitName" value="test" />
<property name="persistenceXmlLocation" value="classpath:persistence.xml" />
<property name="dataSource" ref="myDataSource" />
<property name="loadTimeWeaver">
<bean
class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver" />
</property>
<property name="jpaVendorAdapter" ref="vendorAdapter" />
</bean>
<bean id="vendorAdapter"
class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="database" value="H2" />
<property name="showSql" value="true" />
<property name="generateDdl" value="true" />
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<bean id="searchProcessor" class="com.googlecode.genericdao.search.jpa.JPASearchProcessor">
<constructor-arg ref="metadataUtil" />
</bean>
<bean id="metadataUtil"
class="com.googlecode.genericdao.search.jpa.hibernate.HibernateMetadataUtil"
factory-method="getInstanceForEntityManagerFactory">
<constructor-arg ref="entityManagerFactory" />
</bean>
<bean
class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
<tx:annotation-driven />
这就是我的服务实现:
@Transactional
public class TestServiceImpl implements TestService
private TestDAO<Test,Long> dao;
@Autowired
public void setDao(TestDAO<Test,Long> dao)
this.dao = dao;
public void save(Test test)
dao.persist(test);
dao.flush();
public List<Test> findAll()
return dao.findAll();
public Test findByName(String name)
if (name == null)
return null;
return dao.searchUnique(new Search().addFilterEqual("name", name));
编辑:dao.persist() 方法基本上封装了从 genericdao 对以下方法的调用(em 返回当前的 EntityManager):
/**
* <p>
* Make a transient instance persistent and add it to the datastore. This
* operation cascades to associated instances if the association is mapped
* with cascade="persist". Throws an error if the entity already exists.
*
* <p>
* Does not guarantee that the object will be assigned an identifier
* immediately. With <code>persist</code> a datastore-generated id may not
* be pulled until flush time.
*/
protected void _persist(Object... entities)
for (Object entity : entities)
if (entity != null)
em().persist(entity);
一切正常,但更改仍保留在数据库中。有什么想法吗?
【问题讨论】:
你有 tx:annotation-driven 吗? H2 是否默认启用了自动提交?如果启用,可能想尝试将其设置为 false。 1. dao.persist(...) 只是从 genericdao 项目中调用 JPABaseDAO。我认为这里不会发生任何交易。我将代码添加到问题中。 2. 我的 spring.xml 中有 。 3. 我尝试了我认为您需要从 AbstractTransactionalTestNGSpringContextTests 而不是 AbstractTestNGSpringContextTests 扩展。
【讨论】:
以上是关于Spring集成测试不回滚的主要内容,如果未能解决你的问题,请参考以下文章
在集成测试中每个方法不回滚后 Spring Data JPA 数据库更改
Spring @Transactional 和回滚不起作用,弹簧集成测试