我需要为我的服务层编写一个压力测试。我开始在Unitils、JUnit4和JUnitPerf上做这件事,但是没有运气。这就是我决定尝试TestNG的原因,但是当我尝试在多线程模式下运行测试时,我在会话关闭和事务方面有问题。
我使用以下框架堆栈: Spring、Hibernate、TestNG、Unitils
我需要一个非事务性的多线程测试。测试将调用服务层方法。每个呼叫将启动新的事务。因此,有几个线程同时启动多个事务。
所以测试应该是这样的:
public class ServiceStressTest extends UnitilsTestNG
@SpringBeanByType
private MyService myService;
@DataSet
@Test
public void createTestData() {
}
@Test(invocationCount = 100, threadPoolSize = 50, dependsOnMethods = "createTestData")
public void test1() throws Exception {
myService.method1(); // Starts new transaction and commit it.
myService.method2(); // Starts new transaction and commit it.
}
} 该服务如下所示:
...
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
...
@Transactional
@Service
public class MyServiceImpl implements MyService {
public void method1(){
sessionFactory.getCurrentSession().save(smth); (update/delete)
}
public void method2(){
sessionFactory.getCurrentSession().save(smth); (update/delete)
}
}如果我将threadPoolSize设置为1,那么一切都很好。但是,每当我将它更改为多线程时,我就会得到异常,即会话已关闭。
发布于 2011-04-05 17:41:48
这就是我们需要知道的所有代码吗?听起来,您可能有一个@BeforeMethod或@AfterMethod来关闭会话。
或者,这是由我不熟悉的其他注释之一完成的(@Transactional?)。
https://stackoverflow.com/questions/5549010
复制相似问题