title: How to avoid nested transactions not supported error?
body: <p>I need to make sure many concurrent users be able to access the database. Although after each commit I close the session but sometimes my code runs into following error, but when I do the same operation for few times it surpass the error and works.</p>
  
  <p>My hibernate is 4.2.1.Final</p>
  
  <pre><code>Messages:   
  nested transactions not supported
  File:   org/hibernate/engine/transaction/spi/AbstractTransactionImpl.java
  Line number:    152
  </code></pre>
  
  <p><strong>My Code</strong></p>
  
  <pre><code>session = HibernateUtil.getSession();
  session.getTransaction().begin();       OR session.beginTransaction();
         ...   to do ....
  session.getTransaction().commit();
  session.close();                        OR HibernateUtil.closeSession();
  </code></pre>
  
  <p><strong>HibernateUtil</strong></p>
  
  <pre><code>import org.hibernate.HibernateException;
  import org.hibernate.Session;
  import org.hibernate.SessionFactory;
  import org.hibernate.cfg.Configuration;
  import org.hibernate.service.ServiceRegistry;
  import org.hibernate.service.ServiceRegistryBuilder;
  
  public class HibernateUtil {
  
     private static ServiceRegistry serviceRegistry;
     private static final ThreadLocal&lt;Session&gt; threadLocal = new ThreadLocal();
     private static SessionFactory sessionFactory;
      private static SessionFactory configureSessionFactory() {
          try {
  
              Configuration configuration = new Configuration();
              configuration.configure();
              serviceRegistry = new ServiceRegistryBuilder()
                                   .applySettings(configuration.getProperties())
                                   .buildServiceRegistry();
              sessionFactory = configuration.buildSessionFactory(serviceRegistry);
  
              return sessionFactory;
          } catch (HibernateException e) {
              System.out.append("** Exception in SessionFactory **");
              e.printStackTrace();
          }
         return sessionFactory;
    }
  
  
    static {
      try {
        sessionFactory = configureSessionFactory();
      } catch (Exception e) {
        System.err.println("%%%% Error Creating SessionFactory %%%%");
        e.printStackTrace();
      }
    }
  
    private HibernateUtil() {
    }
  
    public static SessionFactory getSessionFactory() {
      return sessionFactory;
    }
  
    public static Session getSession() throws HibernateException {
      Session session = threadLocal.get();
  
      if (session == null || !session.isOpen()) {
        if (sessionFactory == null) {
          rebuildSessionFactory();
        }
        session = (sessionFactory != null) ? sessionFactory.openSession() : null;
        threadLocal.set(session);
      }
  
      return session;
    }
  
    public static void rebuildSessionFactory() {
      try {
        sessionFactory = configureSessionFactory();
      } catch (Exception e) {
        System.err.println("%%%% Error Creating SessionFactory %%%%");
        e.printStackTrace();
      }
    }
  
    public static void closeSession() throws HibernateException {
      Session session = (Session) threadLocal.get();
      threadLocal.set(null);
  
      if (session != null) {
        session.close();
      }
    }
  }
  </code></pre>
  
  <p><strong>Configuration</strong></p>
  
  <pre><code>&lt;?xml version='1.0' encoding='utf-8'?&gt;
  &lt;!DOCTYPE hibernate-configuration PUBLIC
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
          "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"&gt;
  
  &lt;hibernate-configuration&gt;
  
      &lt;session-factory&gt;
  
          &lt;!-- Database connection settings --&gt;
          &lt;property name="connection.driver_class"&gt;
              com.mysql.jdbc.Driver
          &lt;/property&gt;
          &lt;property name="connection.url"&gt;
              jdbc:mysql://localhost:3306/MyProject
          &lt;/property&gt;
          &lt;property name="connection.username"&gt;root&lt;/property&gt;
          &lt;property name="connection.password"&gt;&lt;/property&gt;
  
          &lt;!-- JDBC connection pool (use the built-in) --&gt;
          &lt;property name="connection.pool_size"&gt;12&lt;/property&gt;
  
          &lt;!-- SQL dialect --&gt;
          &lt;property name="dialect"&gt;
              org.hibernate.dialect.MySQLDialect
          &lt;/property&gt;
  
          &lt;!-- Enable Hibernate's automatic session context management --&gt;
          &lt;property name="current_session_context_class"&gt;thread&lt;/property&gt;
  
          &lt;!-- Disable the second-level cache  --&gt;
          &lt;property name="cache.provider_class"&gt;
              org.hibernate.cache.NoCacheProvider
          &lt;/property&gt;
  
          &lt;!-- Echo all executed SQL to stdout --&gt;
          &lt;property name="show_sql"&gt;true&lt;/property&gt;
  
                  &lt;mapping class="com.project.common.Project" /&gt;
                  &lt;mapping class="com.project.common.School" /&gt;
                  &lt;mapping class="com.project.common.Address" /&gt;
                  &lt;mapping class="com.project.common.Female" /&gt;
                  &lt;mapping class="com.project.common.Male" /&gt;
                  &lt;mapping class="com.project.common.Credential" /&gt;
                  &lt;mapping class="com.project.common.Users" /&gt;
  
      &lt;/session-factory&gt;
  
  &lt;/hibernate-configuration&gt;
  </code></pre>