[英]Spring @transactional does not start a transaction while testing with JUnit4
我有以下配置。
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<bean id="dataSource" class="org.springframework.jdbc.datasource.IsolationLevelDataSourceAdapter">
<property name="targetDataSource">
<bean class="com.microsoft.sqlserver.jdbc.SQLServerConnectionPoolDataSource">
<property name="user" value="user"/>
<property name="password" value="password"/>
<property name="serverName" value="someserver"/>
<property name="databaseName" value="someDBName"/>
<property name="portNumber" value="somePortNumber"/>
</bean>
</property>
</bean>
<!-- this is bean is only used for data extraction module only -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" lazy-init="true">
<property name="persistenceXmlLocation" value="classpath:persistence.xml" />
<property name="dataSource" ref="dataSource"/>
<property name="jpaDialect">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect"/>
</property>
<property name="jpaProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
<prop key="hibernate.format_sql">false</prop>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.cache.use_second_level_cache">true</prop>
<prop key="hibernate.cache.use_query_cache">true</prop>
</props>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>
<!--
Instruct Spring to perform declarative transaction management automatically
on annotated classes. transaction-manager="transactionManager"
-->
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
然后,當我運行具有insert語句的測試時,它們會產生錯誤消息:
javax.persistence.TransactionRequiredException: Executing an update/delete query
at org.hibernate.ejb.QueryImpl.executeUpdate(QueryImpl.java:47)
經過深思熟慮后,我嘗試了這個:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:services.xml" })
@Transactional(propagation = Propagation.REQUIRED)
@TransactionConfiguration(defaultRollback = true)
@TestExecutionListeners(value = { DependencyInjectionTestExecutionListener.class, TransactionalTestExecutionListener.class})
public class SimpleUnitTest {
@Test
public void simpleTest() throws Exception {
System.out.println(entityManager.getTransaction().isActive());
assertTrue(entityManager.getTransaction().isActive());
}
}
它失敗了。 entityManager.getTransaction()。isActive()實際上是假的。
為什么Transactional測試不會開始交易?
你需要添加
@TestExecutionListeners(TransactionalTestExecutionListener.class)
或延伸
AbstractTransactionalJUnit4SpringContextTests
獲得交易行為。 (記住,您的測試類不是bean,因此常規事務配置不適用)
如果使用SpringJUnit4ClassRunner
,則默認情況下啟用TransactionalTestExecutionListener
。
您需要確保在Test上下文配置中包含事務管理配置:
@ContextConfiguration(locations = { "classpath:services.xml" })
所以你可以通過注入TM bean來檢查它:
@Autowired
private PlatformTransactionManager transactionManager;
如果未解析依賴關系,則事務配置未正確定位。
在調試測試期間,檢查堆棧跟蹤中的TransactionInterceptor
。
因為您有兩個與配置相關的答案,我認為問題不是配置,而是問題如何檢查事務是否處於活動狀態,以及如何獲取該EntityManager
實例。
可能的問題可能是:使用EntityManagerFactory.createEntityManager()
方法而不是獲取注入的EntityManager
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.