Eri*_* B. 52 java spring jpa spring-transactions spring-data
我正在使用Spring/Spring-data-JPA,并发现自己需要在单元测试中手动强制提交.我的用例是我正在进行多线程测试,其中我必须使用在生成线程之前持久化的数据.
不幸的是,鉴于测试在一个@Transactional事务中运行,即使是一个flush也没有让它可以被生成的线程访问.
@Transactional
public void testAddAttachment() throws Exception{
final Contract c1 = contractDOD.getNewTransientContract(15);
contractRepository.save(c1);
// Need to commit the saveContract here, but don't know how!
em.getTransaction().commit();
List<Thread> threads = new ArrayList<>();
for( int i = 0; i < 5; i++){
final int threadNumber = i;
Thread t = new Thread( new Runnable() {
@Override
@Transactional
public void run() {
try {
// do stuff here with c1
// sleep to ensure that the thread is not finished before another thread catches up
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
threads.add(t);
t.start();
}
// have to wait for all threads to complete
for( Thread t : threads )
t.join();
// Need to validate test results. Need to be within a transaction here
Contract c2 = contractRepository.findOne(c1.getId());
}
Run Code Online (Sandbox Code Playgroud)
我尝试过使用实体管理器,但是当我这样做时会收到错误消息:
org.springframework.dao.InvalidDataAccessApiUsageException: Not allowed to create transaction on shared EntityManager - use Spring transactions or EJB CMT instead; nested exception is java.lang.IllegalStateException: Not allowed to create transaction on shared EntityManager - use Spring transactions or EJB CMT instead
at org.springframework.orm.jpa.EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(EntityManagerFactoryUtils.java:293)
at org.springframework.orm.jpa.aspectj.JpaExceptionTranslatorAspect.ajc$afterThrowing$org_springframework_orm_jpa_aspectj_JpaExceptionTranslatorAspect$1$18a1ac9(JpaExceptionTranslatorAspect.aj:33)
Run Code Online (Sandbox Code Playgroud)
有没有办法提交交易并继续下去?我一直无法找到任何允许我拨打电话的方法commit().
Mar*_*rey 54
在测试hibernate事件监听器时我有一个类似的用例,它只在commit上调用.
解决方案是将代码保存为另一个带注释的方法REQUIRES_NEW.(在另一个类中)这样生成一个新事务,并在方法返回后发出flush/commit.

请记住,这可能会影响所有其他测试!因此,相应地编写它们或者您需要确保在测试运行后可以清理它们.
Pie*_*ter 25
为什么不使用spring TransactionTemplate来以编程方式控制事务?您还可以重新@Transactional构建代码,以便每个"事务块"都有自己的方法,但鉴于它是一个测试,我会选择对您的事务进行编程控制.
另请注意,@Transactionalrunnable上的注释将不起作用(除非您使用的是aspectj),因为runnables不是由spring管理的!
@RunWith(SpringJUnit4ClassRunner.class)
//other spring-test annotations; as your database context is dirty due to the committed transaction you might want to consider using @DirtiesContext
public class TransactionTemplateTest {
@Autowired
PlatformTransactionManager platformTransactionManager;
TransactionTemplate transactionTemplate;
@Before
public void setUp() throws Exception {
transactionTemplate = new TransactionTemplate(platformTransactionManager);
}
@Test //note that there is no @Transactional configured for the method
public void test() throws InterruptedException {
final Contract c1 = transactionTemplate.execute(new TransactionCallback<Contract>() {
@Override
public Contract doInTransaction(TransactionStatus status) {
Contract c = contractDOD.getNewTransientContract(15);
contractRepository.save(c);
return c;
}
});
ExecutorService executorService = Executors.newFixedThreadPool(5);
for (int i = 0; i < 5; ++i) {
executorService.execute(new Runnable() {
@Override //note that there is no @Transactional configured for the method
public void run() {
transactionTemplate.execute(new TransactionCallback<Object>() {
@Override
public Object doInTransaction(TransactionStatus status) {
// do whatever you want to do with c1
return null;
}
});
}
});
}
executorService.shutdown();
executorService.awaitTermination(10, TimeUnit.SECONDS);
transactionTemplate.execute(new TransactionCallback<Object>() {
@Override
public Object doInTransaction(TransactionStatus status) {
// validate test results in transaction
return null;
}
});
}
Run Code Online (Sandbox Code Playgroud)
}
我知道由于这种丑陋的匿名内部类使用TransactionTemplate看起来不太好,但是当出于某种原因我们想要一个测试方法事务性恕我直言时,它是最灵活的选择。
在某些情况下(取决于应用程序类型),在 Spring 测试中使用事务的最佳方式是关闭@Transactional测试方法。为什么?因为@Transactional可能会导致许多假阳性测试。您可以查看此示例文章以了解详细信息。在这种情况下TransactionTemplate,当我们需要控制时,可以完美地控制事务边界。
| 归档时间: |
|
| 查看次数: |
85381 次 |
| 最近记录: |