I have a simple app that use org.springframework.jdbc.datasource.DataSourceTransactionManager
to manage the transactions.
My spring config is as follow:
<tx:annotation-driven/>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
I have annotated the method with @Transactional
and print out the TransactionSynchronizationManager.isActualTransactionActive()
But the out put is false
. What have i done wrong ?
Edit: i forgot to say that i test that with SpringJUnit4ClassRunner.class
. I included the TransactionalTestExecutionL开发者_如何转开发istener.class
and this will not work. Now it worked after i extend the class with AbstractTransactionalJUnit4SpringContextTests
I think you forgot to add the below to your cfg file. this is required when you are using annotations. Have you added this?
<tx:annotation-driven/>
Here is the namespace
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
Did you use the required annotations on your test class?
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:/spring-....xml")
@TestExecutionListeners({TransactionalTestExecutionListener.class, DependencyInjectionTestExecutionListener.class})
@TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = true)
@Transactional(readOnly = false)
public class MyTest {
...
}
I'm not sure whether the last two are really necessary, I want my test cases to have an active transaction that's why I need those. The first three should be enough in order to get transactional proxies for your annotated beans.
I had the same problem, you should use this instead:
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
精彩评论