title: How to write service layer stress test with TestNG?
body: <p>I need to write a stress test for my service layer. I started doing that with Unitils, JUnit4 and JUnitPerf, but had no luck. That is why I decided to try TestNG, but I have problem with session closing and transactions when trying to run test in multi-threaded mode.</p>
  
  <p>I am using the following framework stack: Spring, Hibernate, TestNG, Unitils</p>
  
  <p>I need a non-transactional multi-threaded test. The test will call service layer methods. Each call will start new transaction. So there are several threads which simultaneously start several transactions each.</p>
  
  <p>So the test should be something like this:</p>
  
  <pre><code>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.
      }
  } 
  </code></pre>
  
  <p>And the service looks like the following:</p>
  
  <pre><code>...
  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)
      }
  }
  </code></pre>
  
  <p>If I set <em>threadPoolSize</em> to 1 everything works fine. But whenever I change it to be multi-threaded I get exception which says that the session was closed. </p>