我已经创建了一个服务的集成测试,该服务在完成后没有回滚事务。我从查看db和我第二次运行测试时得到的错误就知道了这一点。我整个上午一直在谷歌上搜索这个问题,觉得我已经把一切都设置好了。这是一个写入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 integration test doesn't rollback?。这看起来很有希望,但我不知道如何使用它告诉我的内容。
答案 0 :(得分:1)
答案原来在这里找到:Spring/JTA/JPA DAO integration test doesn't rollback?。我将数据源更改为以下内容。
<bean name="hostDataSource" class="com.atomikos.jdbc.nonxa.AtomikosNonXADataSourceBean" destroy-method="close" >
<property name="driverClassName" value="${My_JDTs_to_AgHost_Host_scheme.connection.driver_class}" />
<property name="user" 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="maxPoolSize" value="20" />
<property name="reapTimeout" value="300" />
<property name="uniqueResourceName" value="myappDatabase" />
</bean>
答案 1 :(得分:0)