当前位置: 首页 > 知识库问答 >
问题:

如何在@Transactional方法中手动强制提交?[副本]

梁丘翔
2023-03-14

我正在使用Spring/Spring数据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)

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

共有3个答案

林富
2023-03-14

我知道,由于这种丑陋的匿名内部类,TransactionTemplate的使用看起来不太好,但是出于某种原因,当我们想要一个测试方法transactional IMHO时,它是最灵活的选择。

在某些情况下(取决于应用程序类型),在Spring测试中使用事务的最佳方式是关闭测试方法上的事务。为什么?因为事务性可能会导致许多假阳性测试。您可以查看这篇示例文章以了解详细信息。在这种情况下,当我们需要控制事务边界时,TransactionTemplate可以完美地控制事务边界。

金正阳
2023-03-14

您为什么不使用Spring的TransactionTemplate以编程方式控制事务?您还可以重组您的代码,以便每个“事务块”都有自己的@Transactional方法,但是考虑到这是一个测试,我会选择对您的事务进行编程控制

另请注意,您的runnable上的@Transactional注释将不起作用(除非您使用aspectj),因为runnable不是由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;
        }
    });
}

}

邬博涉
2023-03-14

在测试hibernate事件监听器时,我有一个类似的用例,它只在提交时调用。

解决方案是将要持久化的代码封装到另一个方法中,该方法用“REQUIRES\u NEW”注释。(在另一个类中)通过这种方式生成新事务,并在方法返回后发出刷新/提交。

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

 类似资料:
  • 问题内容: 我正在使用Spring / Spring-data-JPA,发现自己需要在单元测试中手动强制提交。我的用例是我正在做一个多线程测试,其中我必须使用在生成线程之前保留的数据。 不幸的是,鉴于测试正在事务中运行,即使不能使它也不能被衍生线程访问。 我尝试使用实体管理器来执行操作,但是在执行此操作时收到错误消息: 有什么方法可以提交事务并继续进行吗?我一直找不到任何允许我调用的方法。 问题答

  • 我正在使用Hibernate Envers来持久化对象历史。在某些情况下,我们希望捕获对象图状态的快照—我们可以通过了解相应的Envers修订版来实现这一点,然后将其存储在审计记录中。 然而,我们有一个问题。父对象在我们创建和存储其子审核记录的同一事务中更新,并使用Envers revision完成。我们可以获得最新版本: 或创建新修订: 并且使用其中一个,但是父级的提交总是在这之后发生。这就是E

  • 问题内容: 我必须创建许多非常相似的类,它们之间只有一种方法不同。因此,我认为创建抽象类将是实现此目标的好方法。但是我要覆盖的方法(例如,方法foo())没有默认行为。我不想保留任何默认实现,而是强制所有扩展类都实现此方法。我该怎么做呢? 问题答案: 您需要在基类上使用 抽象 方法: 这样,您无需指定默认行为 , 而是可以强制从继承的非抽象类指定的实现。

  • 我使用的是camel kafka组件,我不清楚在提交补偿时引擎盖下发生了什么。如下所示,我正在聚合记录,我认为对于我的用例来说,只有在记录保存到SFTP后提交偏移量才有意义。 是否可以手动控制何时可以执行提交?

  • 在C++中,我们可以强制函数内联。 这在C#中也是可能的吗?有时,当方法很小时,它会自动内联。但是在C#/.NET中强制内联函数是否可能?