title: @Transaction is not working in spring boot 2.x and i am getting "Executing an update/delete query" while updating table
body: <p>When I am using <code>session.beginTransaction()</code> then table getting updated but unable to rollback when anywhere stuck. By removing <code>session.beginTransaction()</code> I found the table is not updating and I am getting "Executing an update/delete query" exception at the console. Please find below full stack track.</p>
  
  <pre><code>javax.persistence.TransactionRequiredException: Executing an update/delete query
      at org.hibernate.internal.AbstractSharedSessionContract.checkTransactionNeededForUpdateOperation(AbstractSharedSessionContract.java:398)
      at org.hibernate.query.internal.AbstractProducedQuery.executeUpdate(AbstractProducedQuery.java:1580)
      at com.skilrock.jackpot.dao.impl.JackpotDaoImpl.updateCurrentValue(JackpotDaoImpl.java:159)
      at com.skilrock.jackpot.dao.impl.JackpotDaoImpl.getSetDetail(JackpotDaoImpl.java:91)
      at com.skilrock.jackpot.dao.impl.JackpotDaoImpl.getJackpot(JackpotDaoImpl.java:47)
      at com.skilrock.jackpot.dao.impl.JackpotDaoImpl$$FastClassBySpringCGLIB$$bdef8e95.invoke(&lt;generated&gt;)
      at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218)
      at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:749)
      at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)
      at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:139)
      at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
      at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:688)
      at com.skilrock.jackpot.dao.impl.JackpotDaoImpl$$EnhancerBySpringCGLIB$$1280fb7b.getJackpot(&lt;generated&gt;)
      at com.skilrock.jackpot.service.impl.JackpotServiceImpl.getJackpot(JackpotServiceImpl.java:21)
      at com.skilrock.jackpot.service.impl.JackpotServiceImpl$$FastClassBySpringCGLIB$$6bee6d5b.invoke(&lt;generated&gt;)
      at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218)
      at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:749)
      at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)
      at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:295)
      at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:98)
      at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
      at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:688)
      at com.skilrock.jackpot.service.impl.JackpotServiceImpl$$EnhancerBySpringCGLIB$$47a38826.getJackpot(&lt;generated&gt;)
      at com.skilrock.jackpot.controller.JackpotController.contribution(JackpotController.java:20)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:498)
      at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:190)
      at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138)
      at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
      at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:892)
      at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:797)
      at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
      at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1039)
      at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:942)
      at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1005)
      at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:908)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:660)
      at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:882)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
      at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
      at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99)
      at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:109)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
      at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:92)
      at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:109)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
      at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:93)
      at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:109)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
      at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:200)
      at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:109)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
      at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202)
      at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
      at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:490)
      at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)
      at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
      at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)
      at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
      at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:408)
      at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
      at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:853)
      at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1587)
      at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
      at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
      at java.lang.Thread.run(Thread.java:748)
  </code></pre>
  
  <hr>
  
  <pre><code> import org.springframework.beans.factory.annotation.Autowired;
      import org.springframework.stereotype.Service;
  
      import com.company.jackpot.beans.RequestBean;
      import com.company.jackpot.beans.ResponseBean;
      import com.company.jackpot.dao.JackpotDao;
      import com.company.jackpot.service.JackpotService;
      @Service
      public class JackpotServiceImpl implements JackpotService {
  
          @Autowired
          private JackpotDao jackpotDao;
          @Override
          @Transactional
          public ResponseBean getJackpot(RequestBean requestBean) {
  
              ResponseBean responseBean = jackpotDao.getJackpot(requestBean);
              return responseBean;
          }
  
      }
  </code></pre>
  
  <hr>
  
  <pre><code>package com.company.jackpot.util;
  
  import java.util.Properties;
  
  import javax.sql.DataSource;
  
  import org.springframework.beans.factory.annotation.Value;
  import org.springframework.context.annotation.Bean;
  import org.springframework.context.annotation.Configuration;
  import org.springframework.jdbc.datasource.DriverManagerDataSource;
  import org.springframework.orm.hibernate5.HibernateTransactionManager;
  import org.springframework.orm.hibernate5.LocalSessionFactoryBean;
  import org.springframework.transaction.annotation.EnableTransactionManagement;
  
  @Configuration
  @EnableTransactionManagement
  public class HibernateUtilConfig {
      /*@Value("${db.driver}")
      private String DRIVER;*/
  
      @Value("${db.password}")
      private String PASSWORD;
  
      @Value("${db.url}")
      private String URL;
  
      @Value("${db.username}")
      private String USERNAME;
  
      @Value("${hibernate.dialect}")
      private String DIALECT;
  
      @Value("${hibernate.show_sql}")
      private String SHOW_SQL;
  
      @Value("${hibernate.hbm2ddl.auto}")
      private String HBM2DDL_AUTO;
  
      @Value("${entitymanager.packagesToScan}")
      private String PACKAGES_TO_SCAN;
  
      @Bean
      public DataSource dataSource() {
          DriverManagerDataSource dataSource = new DriverManagerDataSource();
         // dataSource.setDriverClassName(DRIVER);
          dataSource.setUrl(URL);
          dataSource.setUsername(USERNAME);
          dataSource.setPassword(PASSWORD);
          return dataSource;
      }
  
      @Bean
      public LocalSessionFactoryBean sessionFactory() {
          LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
          sessionFactory.setDataSource(dataSource());
          sessionFactory.setPackagesToScan(PACKAGES_TO_SCAN);
          Properties hibernateProperties = new Properties();
          hibernateProperties.put("hibernate.dialect", DIALECT);
          hibernateProperties.put("hibernate.show_sql", SHOW_SQL);
          hibernateProperties.put("hibernate.hbm2ddl.auto", HBM2DDL_AUTO);
          sessionFactory.setHibernateProperties(hibernateProperties);
  
          return sessionFactory;
      }
  
      @Bean
      public HibernateTransactionManager transactionManager() {
          HibernateTransactionManager transactionManager = new HibernateTransactionManager();
          transactionManager.setSessionFactory(sessionFactory().getObject());
          return transactionManager;
      }
  }
  
  
  
  
  package com.company.jackpot.dao.impl;
  
  import java.util.Date;
  import java.util.List;
  
  import org.hibernate.Query;
  import org.hibernate.Session;
  import org.hibernate.SessionFactory;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.stereotype.Repository;
  
  import com.company.jackpot.beans.RequestBean;
  import com.company.jackpot.beans.ResponseBean;
  import com.company.jackpot.dao.JackpotDao;
  import com.company.jackpot.model.CurrentValue;
  import com.company.jackpot.model.TransactionMaster;
  import com.company.jackpot.model.TransactionWin;
  import com.company.jackpot.model.enummodel.Level;
  import com.company.jackpot.util.CommonOperation;
  
  @Repository
  //@Transactional(rollbackOn = {ArrayIndexOutOfBoundsException.class})
  public class JackpotDaoImpl implements JackpotDao {
  
      @Autowired
      private SessionFactory sessionFactory;
      ResponseBean responseBean = new ResponseBean();
  
      @Override
      public ResponseBean getJackpot(RequestBean requestBean) {
  
          Session session = null;
          //Transaction tx = null;
          try {
              //session = sessionFactory.openSession();
              //tx = session.beginTransaction();
  
              String hql = "select setId from SetReference where serviceCode = '" + requestBean.getServiceCode()
                      + "' and gameCode = " + requestBean.getGameCode() + "";
              Query query = session.createQuery(hql);
              List&lt;Integer&gt; queryResultList = query.list();
              if (!queryResultList.isEmpty()) {
  
                  int setId = queryResultList.get(0);
                  getSetDetail(setId, requestBean, session);
                  //int i = 2;
  
                  //session.getTransaction().commit();
                  //tx.commit();
  
                  return responseBean;
              } else {
                  responseBean.setErrorCode(401);
                  responseBean.setErrorMsg("You are not eligible for jackpot");
  //              session.getTransaction().commit();
                  return responseBean;
              }
          } catch (RuntimeException e) {
              e.printStackTrace();
          }
          return null;
      }
  
      public void getSetDetail(int setId, RequestBean requestBean, Session session) {
  
          try {
              String setDetailQuery = "select setId, level, type, contributionPercentage, startRange, endRange from SetDetail where setId = "
                      + setId + "";
              Query query = session.createQuery(setDetailQuery);
              List&lt;Object[]&gt; setDetailQueryList = query.list();
              if (!setDetailQueryList.isEmpty()) {
                  for (Object[] setDetail : setDetailQueryList) {
                      // SetDetail setDetail = (SetDetail)set;
                      Double start = (double) setDetail[4];
                      Double end = (double) setDetail[5];
                      int randomNumber = CommonOperation.getRandomNumber(start.intValue(), end.intValue());
                      Double contributionPercentage = (double) setDetail[3];
                      Double contributionValue = CommonOperation.getContributionValue(requestBean.getBetAmount(),
                              contributionPercentage.intValue());
                      if (setDetail[1].toString().equalsIgnoreCase("MACHINE")) {
                          /*
                           * from here take random and contribution and then update current_value table
                           * txn_master, txn_win
                           */
                          System.out.println(setDetail[1]);
                          updateCurrentValue((int) setDetail[0], setDetail[1].toString(), setDetail[2].toString(),
                                  randomNumber, contributionValue, requestBean.getMachineId(), requestBean, session);
  
                      }
  
                  }
              }
  
          } catch (RuntimeException e) {
              e.printStackTrace();
          }
      }
  
      public void updateCurrentValue(int setId, String level, String type, int randomNumber, double contributionValue,
              String levelId, RequestBean requestBean, Session session) {
              long transactionId;
  
          try {
              String hql = "from CurrentValue where setId = " + setId + " and level = '" + level + "' and levelId = '"
                      + levelId + "'";
              Query query = session.createQuery(hql);
              List&lt;CurrentValue&gt; currentValueList = query.list();
              for (CurrentValue currentValue : currentValueList) {
                  System.out.println(currentValue);
  
                  if (currentValue.getEligiblePrice() == null) {
                      responseBean.setJackpotType(null);
                      responseBean.setWinningAmount(null);
                      String updateHQl = "update CurrentValue set eligiblePrice = :price, totalContributionValue = :value where setId = "
                              + setId + " and level = '" + level + "' and levelId = '" + levelId + "'";
  
                      Query updateQuery = session.createQuery(updateHQl);
                      updateQuery.setParameter("price", (double) randomNumber);
                      updateQuery.setParameter("value", (currentValue.getTotalContributionValue() + contributionValue));
                      int rowsAffected = updateQuery.executeUpdate();
                      if (rowsAffected &gt; 0) {
                          System.out.println(rowsAffected + " Affected");
                          // now update txn_master, txn_win
                          transactionId = updateTransactionMaster(setId, requestBean, session);
                          System.out.println(5/0);
  
  
  
                  }   }
              }
          } catch (RuntimeException e) {
              e.printStackTrace();
          }
  
      }
  
  }
  </code></pre>
  
  <p>I am expecting tables get updated and if any exception comes all the entries must be rollback by using <code>@Transaction</code>.</p>