首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用Spring @Transactional的TestNG多线程测试

使用Spring @Transactional的TestNG多线程测试
EN

Stack Overflow用户
提问于 2010-02-04 18:11:27
回答 2查看 10K关注 0票数 3

我使用TestNG测试持久性Spring模块(JPA+Hibernate),使用AbstractTransactionalTestNGSpringContextTests作为基类。所有重要的部分--“自动头发”、“TransactionConfiguration”、“TransactionConfiguration”--事务工作都很好。

当我试图使用threadPoolSize=x,invocationCount=y TestNG注释在并行线程中运行测试时,问题就出现了。

代码语言:javascript
运行
复制
WARNING: Caught exception while allowing TestExecutionListener [org.springframework.test.context.transaction.TransactionalTestExecutionListener@174202a] 
to process 'before' execution of test method [testCreate()] for test instance [DaoTest] java.lang.IllegalStateException:
Cannot start new transaction without ending existing transaction: Invoke endTransaction() before startNewTransaction().
at org.springframework.test.context.transaction.TransactionalTestExecutionListener.beforeTestMethod(TransactionalTestExecutionListener.java:123)
at org.springframework.test.context.TestContextManager.beforeTestMethod(TestContextManager.java:374)
at org.springframework.test.context.testng.AbstractTestNGSpringContextTests.springTestContextBeforeTestMethod(AbstractTestNGSpringContextTests.java:146)

..。有人面临过这个问题吗?

以下是代码:

代码语言:javascript
运行
复制
@TransactionConfiguration(defaultRollback = false)
@ContextConfiguration(locations = { "/META-INF/app.xml" })
public class DaoTest extends AbstractTransactionalTestNGSpringContextTests {

@Autowired
private DaoMgr dm;

@Test(threadPoolSize=5, invocationCount=10)
public void testCreate() {
    ...
    dao.persist(o);
    ...
}
...

更新:当所有其他测试线程都没有得到自己的事务实例时,似乎只为主线程维护事务。解决这一问题的唯一方法是以编程方式扩展AbstractTestNGSpringContextTests并以编程方式维护事务(而不是@ transaction注释),每个方法(即使用TransactionTemplate):

代码语言:javascript
运行
复制
@Test(threadPoolSize=5, invocationCount=10)
public void testMethod() {
    new TransactionTemplate(txManager).execute(new TransactionCallbackWithoutResult() {
        @Override
        protected void doInTransactionWithoutResult(TransactionStatus status) {
            // transactional test logic goes here
        }
    }
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2010-02-05 04:10:08

事务需要在同一个线程中启动,下面是更多的详细信息:

Spring3/Hibernate3/TestNG: some tests give LazyInitializationException, some don't

票数 2
EN

Stack Overflow用户

发布于 2012-01-05 11:07:03

您不认为这是因为org.springframework.test.context.TestContextManager不安全(参见https://jira.springsource.org/browse/SPR-5863)吗?

当我想并行地启动事务处理TestNG测试时,我也遇到了同样的问题,您可以看到Spring实际上试图将事务绑定到正确的线程。

然而,这种错误是随机失败的。我用以下方式扩展了AbstractTransactionalTestNGSpringContextTests:

代码语言:javascript
运行
复制
@Override
@BeforeMethod(alwaysRun = true)
protected synchronized void springTestContextBeforeTestMethod(
        Method testMethod) throws Exception {
    super.springTestContextBeforeTestMethod(testMethod);
}

@Override
@AfterMethod(alwaysRun = true)
protected synchronized void springTestContextAfterTestMethod(
        Method testMethod) throws Exception {
    super.springTestContextAfterTestMethod(testMethod);
}

(关键是同步的.)

现在它的作用就像一种魅力。我不能等待春天3.2,尽管如此,它可以完全平分!

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2202045

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档