我创建了一个服务的集成测试,它在完成后不回滚事务。我通过查看数据库和第二次运行测试时遇到的错误知道这一点。我整个早上都在谷歌上搜索这个问题,感觉一切都设置妥当了。这是一个写入 SQLServer 2008 的 hibernate/jpa 应用程序。我不知道还能去哪里看。片段如下。
@RunWith(SpringJUnit4ClassRunner.class)
@TestExecutionListeners({
DependencyInjectionTestExecutionListener.class,
DirtiesContextTestExecutionListener.class,
TransactionalTestExecutionListener.class })
@TransactionConfiguration(defaultRollback=true)
@Transactional // extend transactional boundary to test class so that automatic rollback works properly
@ContextConfiguration(locations = {
"file:./src/main/resources/AghostMobile.Service-business.service-context.xml",
"file:./src/main/resources/AghostMobile.Service-service-context.xml",
"file:./src/main/resources/AghostMobile.Service-dao-context.xml"})
public class ColorSchemeMigrationServiceIntTest {
/**
* The service being tested, injected by Spring
*
*/
@Autowired
ColorSchemeMigrationService service;
/**
* The helper services, injected by Spring.
*
*/
@Autowired
protected WebsitecolorpaletteuserdefinedService userPaletteService;
@Test
public void testSaveColorPalette() {
Integer mobileWebsiteId = Integer.valueOf(386);
Integer custId = Integer.valueOf(15);
Integer siteId = Integer.valueOf(2);
String user = "Test";
Websitecolorpaletteuserdefined palette = service.translateColorScheme(mobileWebsiteId, custId, siteId, user);
service.saveColorPalette(palette);
Websitecolorpaletteuserdefined response = userPaletteService.findWebsitecolorpaletteuserdefinedByCustIdAndSiteId(custId, siteId);
assertNotNull("User palette not found.", response);
assertEquals("CustId is not the expected value.", custId, response.getCustId());
assertEquals("SiteId is not the expected value.", siteId, response.getSiteId());
}
目前,我定义了以下 bean:
<!-- ******************************************************************** -->
<!-- Setup the transaction manager -->
<!-- ******************************************************************** -->
<!-- Using Atomikos Transaction Manager -->
<bean id="atomikosTransactionManager" class="com.atomikos.icatch.jta.UserTransactionManager" init-method="init"
destroy-method="close">
<property name="forceShutdown" value="true" />
<property name="startupTransactionService" value="true" />
<property name="transactionTimeout" value="60" />
</bean>
<bean id="atomikosUserTransaction" class="com.atomikos.icatch.jta.UserTransactionImp" />
<!-- Configure the Spring framework to use JTA transactions from Atomikos -->
<bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager">
<property name="transactionManager" ref="atomikosTransactionManager" />
<property name="userTransaction" ref="atomikosUserTransaction" />
<property name="transactionSynchronizationName" value="SYNCHRONIZATION_ON_ACTUAL_TRANSACTION" />
</bean>
<!-- ******************************************************************** -->
<!-- Setup a data source -->
<!-- ******************************************************************** -->
<!-- Using Apache DBCP Data Sources -->
<bean name="hostDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" >
<property name="driverClassName" value="${My_JDTs_to_AgHost_Host_scheme.connection.driver_class}" />
<property name="username" value="${My_JDTs_to_AgHost_Host_scheme.connection.username}" />
<property name="password" value="${My_JDTs_to_AgHost_Host_scheme.connection.password}" />
<property name="url" value="${My_JDTs_to_AgHost_Host_scheme.connection.url}" />
<property name="maxIdle" value="${My_JDTs_to_AgHost_Host_scheme.minPoolSize}" />
<property name="maxActive" value="${My_JDTs_to_AgHost_Host_scheme.maxPoolSize}" />
</bean>
<!-- ******************************************************************** -->
<!-- Setup each persistence unit -->
<!-- ******************************************************************** -->
<!-- Configure a JPA vendor adapter -->
<bean id="My_JDTs_to_AgHost_Host_schemeJPAVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="showSql" value="${My_JDTs_to_AgHost_Host_scheme.show_sql}" />
<property name="generateDdl" value="${My_JDTs_to_AgHost_Host_scheme.generateDdl}" />
<property name="databasePlatform" value="${My_JDTs_to_AgHost_Host_scheme.dialect}" />
</bean>
<!-- EntityManager Factory that brings together the persistence unit, datasource, and JPA Vendor -->
<bean id="My_JDTs_to_AgHost_Host_scheme" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="hostDataSource" />
<property name="persistenceUnitName" value="My_JDTs_to_AgHost_Host_scheme" />
<property name="jpaVendorAdapter" ref="My_JDTs_to_AgHost_Host_schemeJPAVendorAdapter" />
<property name="jpaPropertyMap">
<map>
<entry key="hibernate.transaction.manager_lookup_class" value="com.atomikos.icatch.jta.hibernate3.TransactionManagerLookup" />
<!-- <entry key="hibernate.transaction.factory_class" value="org.hibernate.transaction.JTATransactionFactory" /> -->
<entry key="hibernate.connection.release_mode" value="on_close" />
</map>
</property>
</bean>
这确实允许我更新我的数据,但不会回滚我的事务。所以,我正在使用事务管理器 org.springframework.jdbc.datasource.DataSourceTransactionManager。我将“设置事务管理器”块中的三个 bean 替换为:
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="hostDataSource" />
</bean>
这让我的测试失败并出现IllegalStateException:此方法需要调用线程的事务,并且不存在。例外还指出:
Possible causes: either you didn't start a transaction,
it rolledback due to timeout, or it was committed already.
ACTIONS: You can try one of the following:
1. Make sure you started a transaction for the thread.
2. Make sure you didn't terminate it yet.
3. Increase the transaction timeout to avoid automatic rollback of long transactions;
check [http://www.atomikos.com/Documentation/JtaProperties][1] for how to do this.
我承认尚未阅读该文档,并将在发布这些更新后这样做。我还发现了这个看起来很有希望的线程:persistence-unit, different hibernate.transaction.manager_lookup_class property。您可以看到在 bean My_JDTs_to_AgHost_Host_scheme 中注释掉了。这失败得很惨。但是,也许我没有正确使用它。
我还发现了这个线程:Spring/JTA/JPA DAO 集成测试不回滚?. 这看起来很有希望,但我再次不确定如何使用它告诉我的内容。