小编典典

如何手动强制@Transactional方法中的提交?

spring

我正在使用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());
    }

我尝试使用实体管理器来执行操作,但是在执行此操作时收到错误消息:

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)

有什么方法可以提交事务并继续进行吗?我一直找不到任何允许我调用的方法commit()


阅读 4565

收藏
2020-04-15

共1个答案

小编典典

在测试仅在提交时调用的hibernate事件侦听器期间,我有一个类似的用例。

解决方案是将要持久保存的代码包装到另一个用注释的方法中REQUIRES_NEW。(在另一个类中)这样,一旦方法返回,就产生一个新的事务并发出刷新/提交。

请记住,这可能会影响所有其他测试!因此,请相应地编写它们,或者你需要确保在测试运行后可以进行清理。

2020-04-15