title: Hibernate 5.2.10.Final - javax.persistence.TransactionRequiredException: no transaction is in progress
body: <p>After migration from Hibernate 4.3 to 5.2 I'm getting error <em>javax.persistence.TransactionRequiredException: no transaction is in progress</em>. What can cause this error? Why transaction doesn't exist?</p>
  
  <pre><code>public class HibernateUtil {
  private static StandardServiceRegistry registry;
  private static SessionFactory sessionFactory;
  private static final Logger logger = LogManager.getLogger(HibernateUtil.class);
  private static final ThreadLocal&lt;Interceptor&gt; threadInterceptor = new ThreadLocal&lt;&gt;();
  private static final ThreadLocal&lt;Session&gt; threadSession = new ThreadLocal&lt;&gt;();
  
  /**
   * Retrieves the current Session local to the thread.
   *
   * &lt;p/&gt;
   * If no Session is open, opens a new Session for the running thread.
   *
   * @return Session
   */
  public static Session getSession() throws InfrastructureException {
      Session s = threadSession.get();
      try {
          if (s == null) {
              if (getInterceptor() != null) {
                  if (logger.isDebugEnabled()) {
                      logger.debug("Using interceptor: " + getInterceptor().getClass());
                  }
                  s = getSessionFactory().openSession();
              } else {
                  s = getSessionFactory().openSession();
              }
              threadSession.set(s);
          }
      } catch (HibernateException ex) {
          throw new InfrastructureException(ex);
      }
      return s;
  }
  
  public static SessionFactory getSessionFactory() {
      if (sessionFactory == null) {
          try {
              // Create registry
              registry = new StandardServiceRegistryBuilder()
                      .configure()
                      .build();
  
              // Create MetadataSources
              MetadataSources sources = new MetadataSources(registry);
  
              // Create Metadata
              Metadata metadata = sources.getMetadataBuilder().build();
  
              // Create SessionFactory
              sessionFactory = metadata.getSessionFactoryBuilder().build();
  
          } catch (Exception e) {
              e.printStackTrace();
              if (registry != null) {
                  StandardServiceRegistryBuilder.destroy(registry);
              }
          }
      }
      return sessionFactory;
  }
  
  private static Interceptor getInterceptor() {
      return threadInterceptor.get();
  }
  </code></pre>
  
  <p>General method for storing objects in database (BaseDAO). Error is thrown in session.flush() line.</p>
  
  <pre><code>protected static void storeObject(Object object) throws DAOException {
      try {
          Session session = Helper.getHibernateSession();
          session.saveOrUpdate(object);
          session.flush();
          session.beginTransaction().commit();
      } catch (HibernateException he) {
          rollback();
          throw new DAOException(he);
      }
  }
  </code></pre>
  
  <p>Method in UserDAO class:</p>
  
  <pre><code>public void save(User user) throws DAOException {
      storeObject(user);
  }
  </code></pre>
  
  <p>hibernate.cf.xml</p>
  
  <pre><code>&lt;session-factory&gt;
      &lt;!-- SQL - Settings --&gt;
      &lt;property name="dialect"&gt;org.hibernate.dialect.MySQLDialect&lt;/property&gt;
      &lt;property name="hibernate.connection.driver_class"&gt;com.mysql.jdbc.Driver&lt;/property&gt;
  
      &lt;property name="hibernate.connection.url"&gt;
          jdbc:mysql://localhost/test?autoReconnect=true&amp;amp;autoReconnectForPools=true
      &lt;/property&gt;
      &lt;property name="hibernate.connection.username"&gt;test&lt;/property&gt;
      &lt;property name="hibernate.connection.password"&gt;test&lt;/property&gt;
  
      &lt;!-- connection pool --&gt;
      &lt;property name="hibernate.c3p0.max_size"&gt;5000&lt;/property&gt;
      &lt;property name="hibernate.c3p0.min_size"&gt;10&lt;/property&gt;
      &lt;property name="hibernate.c3p0.timeout"&gt;180&lt;/property&gt;
      &lt;property name="hibernate.c3p0.max_statements"&gt;0&lt;/property&gt;
      &lt;property name="hibernate.c3p0.idle_test_period"&gt;10&lt;/property&gt;
      &lt;property name="hibernate.c3p0.acquire_increment"&gt;1&lt;/property&gt;
      &lt;property name="hibernate.c3p0.validate"&gt;true&lt;/property&gt;
  
      &lt;!-- use the C3P0 connection pool --&gt;
      &lt;property name="connection.provider_class"&gt;
          org.hibernate.service.jdbc.connections.internal.C3P0ConnectionProvider
      &lt;/property&gt;
  
      &lt;!-- Enable Hibernate's automatic session context management --&gt;
      &lt;property name="current_session_context_class"&gt;managed&lt;/property&gt;
  
      &lt;!-- Don't echo all executed SQL to stdout --&gt;
      &lt;property name="show_sql"&gt;false&lt;/property&gt;
      &lt;property name="hibernate.bytecode.use_reflection_optimizer"&gt;false&lt;/property&gt;
  
      &lt;!-- Validate database schema on startup --&gt;
      &lt;property name="hbm2ddl.auto"&gt;validate&lt;/property&gt;
  
      &lt;!-- Mapping --&gt;
      &lt;mapping class="org.test.data.database.beans.User"/&gt;
  
  &lt;/session-factory&gt;
  </code></pre>