title: How to use Hibernate in a multi threaded application?
body: <p>I am trying to use Hibernate for a multi threaded application wherein each thread retrieves an object and tries to insert it into a table. My code looks like below. 
  I have local hibernate Session objects per thread and in each InsertData I do beginTransaction and commit. </p>
  
  <p>The problem I am facing is that many times I get "org.hibernate.TransactionException: nested transactions not supported"</p>
  
  <p>Since I am new to hibernate I don't know if what I am doing is correct or not? Please let me know what is the correct way to use hibernate in multi threaded app and how to avoid the above mentioned exception.</p>
  
  <p>Thanks </p>
  
  <pre><code>public class Worker extends Thread {
  private Session session = null;
  
  Worker() {
      SessionFactory sf = HibernateUtil.getSessionFactory(); // Singleton
      session = sf.openSession();
      session.setFlushMode(FlushMode.ALWAYS);
  }
  
  public void run() {
      // Some loop which will run thousand of times 
      for (....)
      {
          InsertData(b);
      }
      session.close();
  }
  
  // BlogPost Table has (pk = id AutoGenerated), dateTime, blogdescription etc. 
  private void InsertData(BlogPost b) {
      session.beginTransaction();
      Long id = (Long) session.save(b);
      b.setId(id);
      session.getTransaction().commit();
  }
  }
  </code></pre>
  
  <p>My hibernate config file has <code>c3p0.min_size=10</code> and <code>c3p0.max_size=20</code></p>