2012-10-03 70 views
1

我已經創建了完成後未回滾事務的服務的集成測試。我通過查看數據庫和從第二次運行測試時得到的錯誤中知道這一點。我一直在使用googling這個問題,並感覺我已經正確設置了一切。這是一個寫入SQLServer 2008的hibernate/jpa應用程序。我不確定在哪裏可以看。下面的代碼段。Junit測試不回滾sqlserver上的事務

@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()); 
    } 

目前,我有以下豆定義:

<!-- ******************************************************************** --> 
<!-- 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 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?。這看起來很有希望,但我不知道如何使用它告訴我的。

+1

您是否嘗試過在每個測試方法上使用@ Transactional而不是整個類?你是否期望數據在測試用例之間維護,但在完成課程後會回滾?或者你期望在每種​​方法之後回滾? –

+1

@JesseWebb在測試類級別放置'@Trasactional'註釋是每次測試後回滾事務的標準做法 –

+0

@JesseWebb - 我希望測試在每次測試後回滾。我有和JohnB –

回答

1

答案tunred出在這裏找到: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> 
0

您的加載環境中是否有DataSourceTransactionManager bean?

DataSourceTransactionManager example

見第9.3節

+0

它似乎沒有。我會谷歌如何做到這一點。如果你有參考資料,我將不勝感激。 :-) –

+0

@ user471133更新了鏈接 –