title: Java: Running transaction in multithreaded environment
body: <p>We are launching a website that will have a very heavy volume for a short period of time. It is basically giving tickets. The code is written in Java, Spring &amp; Hibernate. I want to mimic the high volume by spawning multiple threads and trying to get the ticket using JUnit test case. The problem is that in my DAO class the code just simply dies after I begin transaction. I mean there is no error trace in the log file or anything like that. Let me give some idea about the way my code is.</p>
  
  <p>DAO code:</p>
  
  <pre><code>@Repository("customerTicketDAO")
  public class CustomerTicketDAO extends BaseDAOImpl {// BaseDAOImpl extends HibernateDaoSupport
  
  public void saveCustomerTicketUsingJDBC(String customerId) {
    try{
        getSession().getTransaction().begin(); //NOTHING HAPPENS AFTER THIS LINE OF CODE
        // A select query
        Query query1 = getSession().createSQLQuery("my query omitted on purpose");
        .
        .
        // An update query
        Query query2 = getSession().createSQLQuery("my query omitted on purpose");
        getSession().getTransaction().commite();
     } catch (Exception e) {
     }
  }
  </code></pre>
  
  <p>Runnable code:</p>
  
  <pre><code>public class InsertCustomerTicketRunnable implements Runnable {
  
  @Autowired
  private CustomerTicketDAO customerTicketDAO;    
  
   public InsertCustomerTicketRunnable(String customerId) {
      this.customerId = customerId;
  }     
  
   @Override
  public void run() {
      if (customerTicketDAO != null) {
          customerTicketDAO.saveCustomerTicketUsingJDBC(customerId); 
      }
  }
  }
  </code></pre>
  
  <p>JUnit method:</p>
  
  <pre><code>@RunWith(SpringJUnit4ClassRunner.class)
  @ContextConfiguration(locations={"file:src/test/resources/applicationContext-test.xml"})
  public class DatabaseTest {
    @Before
  public void init() {
      sessionFactory = (SessionFactory)applicationContext.getBean("sessionFactory");
      Session session = SessionFactoryUtils.getSession(sessionFactory, true);
      TransactionSynchronizationManager.bindResource(sessionFactory, new SessionHolder(session));
  
      customerTicketDAO = (CustomerTicketDAO)applicationContext.getBean("customerTicketDAO");
  }
  
  @After
  public void end() throws Exception {
      SessionHolder sessionHolder = (SessionHolder) TransactionSynchronizationManager.unbindResource(sessionFactory);
      SessionFactoryUtils.closeSession(session);
    }
  
    @Test
  public void saveCustomerTicketInMultipleThreads () throws Exception  {
      ExecutorService executor = Executors.newFixedThreadPool(NTHREDS);
  
      for (int i=0; i&lt;1000; i++) {
          executor.submit(new InsertCustomerTicketRunnable(i));
      }
  
      // This will make the executor accept no new threads
      // and finish all existing threads in the queue
      executor.shutdown();
      // Wait until all threads are finish
      executor.awaitTermination(1, TimeUnit.SECONDS);
  }
  </code></pre>
  
  <p>I see no data being inserted into the database. Can someone please point me as to where I am going wrong?</p>
  
  <p>Thanks
  Raj</p>