title: Nested Transactions Exception although synchronized methods and error handling
body: <p>I am developing a Java-Web Application using Hibernate -> MySQL.</p>
  
  <p>A user sees a JSP page, it calls controllers to get data from DB, also it has a Javascript function which fetches larger data strings via ajax.</p>
  
  <p><strong>The problem:</strong>
  When the user refreshes the page right after loading the page, the servlet responsible for the ajax data throws this exception:
  org.hibernate.TransactionException: nested transactions not supported</p>
  
  <p>I believe this gets caused by a transaction from the first call, which is still active when the user refreshes and the second call gets executed.</p>
  
  <p>I marked every controller method with hibernate interaction as <strong>synchronized</strong>.
  Now the user is able to refresh 1-5 times, but then the exception <strong>still occurs</strong>.</p>
  
  <p>I am using Hibernate 4.3.1.</p>
  
  <p>HibernateUtil.openSession() is just sessionFactory.getCurrentSession();</p>
  
  <p>This is the controller-snippet where the exception occurs:</p>
  
  <pre><code>public static synchronized Widget getWidgetById(long id) {
      Widget result;
      Session session = HibernateUtil.openSession();
      try {
          Transaction tx = session.beginTransaction();
          try {
              result = (Widget) session.get(Widget.class, id);
              tx.commit();
          } catch (Exception ex) {
              tx.rollback();
              throw ex;
          }
      } finally {
          //disabled since we use transaction.auto_close_session
          //session.close();
      }
      return result;
  }
  </code></pre>
  
  <p>Are there any mistakes that I didnt see?
  What could be the problem here?</p>