title: Hibernate + Jersey + Jackson getting “org.hibernate.TransactionException: nested transactions not supported” randomly
body: <p>I have buit a Webservice using these technologies + c3p0 for database handling. It works ok most of the time but I have a 3-5% ratio (sometimes even a 10%) of failed acces due to this error.</p>
  
  <p>I am using Hibernate this way:</p>
  
  <p>-Session Factory</p>
  
  <pre><code>private static SessionFactory buildSessionFactory() {
      try {           
          Configuration configuration = new Configuration();
          configuration.configure();
          serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();        
  
          // Create the SessionFactory from hibernate.cfg.xml
          return configuration
          .buildSessionFactory(serviceRegistry);
      } catch (Throwable ex) {
          System.err.println("Initial SessionFactory creation failed." + ex);
          throw new ExceptionInInitializerError(ex);
      }
  }
  
  public static SessionFactory getSessionFactory() {
      //reabrimos la sesion si esta cerrada al liberar los recursos
      if(sessionFactory.isClosed())
      {
          System.out.println("Reopen session");
          sessionFactory.openSession();
      }
  
      return sessionFactory;
  }
  </code></pre>
  
  <p>Then in my <code>hibernate.cfg.xml</code> I have the following line:</p>
  
  <pre><code>&lt;property name="current_session_context_class"&gt;thread&lt;/property&gt;
  </code></pre>
  
  <p>Finally in my endpoints I have defined a <code>hibernate_session</code> class which I use as follows:</p>
  
  <pre><code>@Path("/projects")
  public class ProjectServiceImpl {
  
  @Context
  SecurityContext security;
  Session hibernate_session = null;
  
  @POST
  @Path("sync.json")
  @Produces(value = {"application/json",
          "application/vnd.myapp-v1+json",
          "application/vnd.myapp-v2+json"})
  public Response syncProjects(
          @DefaultValue("") @FormParam("projects") String in_projects_str,
          @DefaultValue("0") @FormParam("last_sync") long last_sync,
          @Context Request request) {
  
     //...
  
     hibernate_session = HibernateUtil.getSessionFactory()
              .getCurrentSession();
  
    if (hibernate_session == null) {
      ResponseMessage rm = new ResponseMessage();
          rm.setCode(Status.INTERNAL_SERVER_ERROR.getStatusCode());
          rm.setMessage("Hibernate Session is Null");
          rm.setType("ERROR");
          return Response.status(Status.INTERNAL_SERVER_ERROR).entity(rm)
                  .type("application/json").build();
      }
  
      try {
  
          hibernate_session.beginTransaction();
  
          //Database work...
  
          hibernate_session.flush();
  
          hibernate_session.getTransaction().commit();
  
          }catch (RuntimeException | IllegalAccessException
                  | InvocationTargetException e) {
              try {
                  if (hibernate_session.getTransaction() != null) {
                      hibernate_session.getTransaction().rollback();
                  }
              } catch (RuntimeException rbe) {
                  System.err.println("Couldn’t roll back transaction");
              }
  
              e.printStackTrace();
              ResponseMessage rm = new ResponseMessage();
              rm.setCode(Status.INTERNAL_SERVER_ERROR.getStatusCode());
              rm.setMessage(e.getMessage());
              rm.setType("ERROR");
              return Response.status(Status.INTERNAL_SERVER_ERROR).entity(rm)
                      .type("application/json").build();
  
          }
      }
  
      return Response.ok().entity(result_entity)
              .type("application/json").build();
  }
  </code></pre>
  
  <p>My <code>hibernate_session</code> is a class attribute, do I have to change it to a local variable?. As far as I know the end points will be executed in different threads so I have assumed that I am working with different instances of my endpoint container class and these class attributes will not get overriden by multiple request.</p>
  
  <p>Any light you can shed on this topic will be appreciated,</p>
  
  <p>Thanks in advance</p>