title: Data gets stored in database when i click register but then it gives "javax.persistence.TransactionRequiredException: no transaction is in progress"
body: <p>When I run my application on server the user registration page open and after entering all the details and and clicking on submit i get this</p>
  <pre><code>javax.persistence.TransactionRequiredException: no transaction is in progress
      org.hibernate.internal.SessionImpl.checkTransactionNeeded(SessionImpl.java:3467)
      org.hibernate.internal.SessionImpl.doFlush(SessionImpl.java:1427)
      org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1423)
      main.java.com.package.web.Register.doPost(Register.java:49)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:652)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:733)
      org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
  </code></pre>
  <p>My Register.java is</p>
  <pre><code>    package main.java.com.package.web;
  import javax.servlet.annotation.WebServlet;
  import org.hibernate.Transaction;
  
  import org.hibernate.Session;
  
  import main.java.com.package.entity.UsersEntity;
  import main.java.com.package.util.HibernateUtil;
  
  import javax.servlet.ServletException;
  import javax.servlet.http.Cookie;
  import javax.servlet.http.HttpServlet;
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpServletResponse;
  import javax.transaction.Transactional;
  
  import java.io.IOException;
  
  
  @Transactional
  @WebServlet(&quot;/Register&quot;)
  public class Register extends HttpServlet {
      private static final long serialVersionUID = 1L;
      
      @Override
      protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
          System.out.println(&quot;check1&quot;);
          String firstName = request.getParameter(&quot;firstName&quot;);
          String lastName = request.getParameter(&quot;lastName&quot;);
          String username = request.getParameter(&quot;username&quot;);
          String password = request.getParameter(&quot;password&quot;);
  
          UsersEntity user = new UsersEntity();
          user.setFirstName(firstName);
          user.setLastName(lastName);
          user.setUsername(username);
          user.setPassword(password);
          Transaction tx = null;
          Session session = HibernateUtil.getSessionFactory().openSession();
          
          try{
              tx = session.beginTransaction();
              session.persist(user);
              session.flush();
              tx.commit();
  //            session.getTransaction().commit();
          }catch (Exception e){
              response.getWriter().println(&quot;User could not be saved&quot;);
              response.getWriter().println(e);
              session.getTransaction().rollback();
              session.close();
              return;
          }
          session.flush();
          session.close();
          response.getWriter().print(&quot;Registered Successfully&quot;);
          response.addCookie(new Cookie(&quot;username&quot;,username));
          response.addCookie(new Cookie(&quot;password&quot;,password));
          response.sendRedirect(&quot;login&quot;);
  
      }
      
      @Override
      protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
          response.sendRedirect(&quot;register.jsp&quot;);
      }
  }
  </code></pre>
  <p>and my hibernate.cfg.xml is</p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
  &lt;!DOCTYPE hibernate-configuration PUBLIC
          &quot;-//Hibernate/Hibernate Configuration DTD 3.0//EN&quot;
          &quot;http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd&quot;&gt;
  &lt;hibernate-configuration&gt;
      &lt;session-factory name=&quot;sessionFactory&quot;&gt;
          &lt;property name=&quot;hibernate.connection.driver_class&quot;&gt;com.mysql.jdbc.Driver&lt;/property&gt;
          &lt;property name=&quot;hibernate.connection.password&quot;&gt;password&lt;/property&gt;
          &lt;property name=&quot;hibernate.connection.url&quot;&gt;jdbc:mysql://localhost/demo&lt;/property&gt;
          &lt;property name=&quot;hibernate.connection.username&quot;&gt;root&lt;/property&gt;
          &lt;property name=&quot;hibernate.dialect&quot;&gt;org.hibernate.dialect.MySQLDialect&lt;/property&gt;
          &lt;property name=&quot;connection.pool_size&quot;&gt;1&lt;/property&gt;
           &lt;property name=&quot;connection.autocommit&quot;&gt;true&lt;/property&gt;
          &lt;property name=&quot;show_sql&quot;&gt;true&lt;/property&gt;
          &lt;property name=&quot;hbm2ddl.auto&quot;&gt;update&lt;/property&gt;
          &lt;mapping class=&quot;main.java.com.package.entity.UsersEntity&quot;/&gt;
      &lt;/session-factory&gt;
  &lt;/hibernate-configuration&gt;
  </code></pre>
  <p><strong>The data I entered gets stroed in mysql table but still this exception occurs any suggestions would be really helpful</strong></p>