9 Replies Latest reply on Jan 24, 2014 8:40 AM by Frank Cornelis

    WildFly8 - Named query with JPA entitymanager throws exception: java.lang.IllegalStateException: Illegal attempt to set lock mode on a non-SELECT query

    xiang yingbing Master

      Dear all,

       

      My EJB Timer's @Timeout method print Exception: java.lang.IllegalStateException: Illegal attempt to set lock mode on a non-SELECT query

       

       

      @NamedQueries({

          @NamedQuery(name=Session.NamedQuery_name_CountByUserId,query=Session.NamedQuery_jpql_CountByUserId),

          @NamedQuery(name=Session.NamedQuery_name_FindByToken,query=Session.NamedQuery_jpql_FindByToken),

          @NamedQuery(name=Session.NamedQuery_name_DeleteIdleTimeoutRecords,query=Session.NamedQuery_jpql_DeleteIdleTimeoutRecords),

          @NamedQuery(name=Session.NamedQuery_name_DeleteByToken,query=Session.NamedQuery_jpql_DeleteByToken)

      })

      @Entity

      @Table()

      public class Session implements Serializable{

          ...

          public static final String NamedQuery_name_DeleteIdleTimeoutRecords = "Session__NamedQuery_name_DeleteIdleTimeoutRecords";

          public static final String NamedQuery_jpql_DeleteIdleTimeoutRecords = "DELETE FROM Session obj WHERE obj.lastUpdated<:_lastUpdated";

          public static final String NamedQuery_param_DeleteIdleTimeoutRecords_lastUpdated = "_lastUpdated";

          ...

      }

       

       

       

       

      Case.1

       

      @Local(ISessionTimeoutMonitor.class)

      @Singleton

      @Startup

      public class SessionIdleTimeoutMonitor implements ISessionTimeoutMonitor{

          Logger log = Logger.getLogger(SessionIdleTimeoutMonitor.class); 

       

          @SuppressWarnings("unused")

          @Resource

          private SessionContext sessionContext;

       

          @Resource TimerService timerService;

       

          @PersistenceContext

          protected EntityManager em;

       

       

          @EJB

          INmsCoreService nmsCoreService;

       

          @PostConstruct

          public void createTimer() {

              timerService.createIntervalTimer(

                        10000L,

                        nmsCoreService.getSessionIdleCheckInterval(),//intervalDuration

                        new TimerConfig(null,false)

                        );     

          }

       

          @Timeout

          public void timeout( ){

              try{

                  Date expireDate = new Date( System.currentTimeMillis() - nmsCoreService.getSessionIdleTimeout() );

                  int i = em.createNamedQuery(Session.NamedQuery_name_DeleteIdleTimeoutRecords)

                  .setParameter(Session.NamedQuery_param_DeleteIdleTimeoutRecords_lastUpdated,expireDate)

                  .executeUpdate();

              }catch(Exception e){

                  log.error(e);

              }

          }

      }

       

      The Exception is:

      11:12:30,158 ERROR [com.ybxiang.nms.ejb.timer.SessionIdleTimeoutMonitor] (EJB default - 1) java.lang.IllegalStateException: Illegal attempt to set lock mode on a non-SELECT query

      11:12:30,159 ERROR [org.jboss.as.ejb3] (EJB default - 1) JBAS014120: Error invoking timeout for timer: [id=994c9646-81ff-42ce-8322-d5b068eceedd timedObjectId=nms-server-ear.nms-server-ejb.SessionIdleTimeoutMonitor auto-timer?:false persistent?:false timerService=org.jboss.as.ejb3.timerservice.TimerServiceImpl@18873fe initialExpiration=Tue Dec 31 11:12:30 CST 2013 intervalDuration(in milli sec)=30000000 nextExpiration=Tue Dec 31 19:32:30 CST 2013 timerState=IN_TIMEOUT info=null: org.jboss.as.ejb3.tx.TimerTransactionRolledBackException: JBAS014210: Timer invocation failed, transaction rolled back

          at org.jboss.as.ejb3.tx.TimerCMTTxInterceptor.endTransaction(TimerCMTTxInterceptor.java:70) [wildfly-ejb3-8.0.0.CR1.jar:8.0.0.CR1]

          at org.jboss.as.ejb3.tx.CMTTxInterceptor.invokeInOurTx(CMTTxInterceptor.java:277) [wildfly-ejb3-8.0.0.CR1.jar:8.0.0.CR1]

          at org.jboss.as.ejb3.tx.CMTTxInterceptor.required(CMTTxInterceptor.java:340) [wildfly-ejb3-8.0.0.CR1.jar:8.0.0.CR1]

          at org.jboss.as.ejb3.tx.CMTTxInterceptor.processInvocation(CMTTxInterceptor.java:239) [wildfly-ejb3-8.0.0.CR1.jar:8.0.0.CR1]

          at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:309) [jboss-invocation-1.2.0.Beta4.jar:1.2.0.Beta4]

          at org.jboss.as.ejb3.component.interceptors.CurrentInvocationContextInterceptor.processInvocation(CurrentInvocationContextInterceptor.java:41) [wildfly-ejb3-8.0.0.CR1.jar:8.0.0.CR1]

          at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:309) [jboss-invocation-1.2.0.Beta4.jar:1.2.0.Beta4]

          at org.jboss.as.ejb3.security.SecurityContextInterceptor.processInvocation(SecurityContextInterceptor.java:95) [wildfly-ejb3-8.0.0.CR1.jar:8.0.0.CR1]

          at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:309) [jboss-invocation-1.2.0.Beta4.jar:1.2.0.Beta4]

          at org.jboss.as.ejb3.component.interceptors.ShutDownInterceptorFactory$1.processInvocation(ShutDownInterceptorFactory.java:64) [wildfly-ejb3-8.0.0.CR1.jar:8.0.0.CR1]

          at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:309) [jboss-invocation-1.2.0.Beta4.jar:1.2.0.Beta4]

          at org.jboss.as.ee.component.NamespaceContextInterceptor.processInvocation(NamespaceContextInterceptor.java:50) [wildfly-ee-8.0.0.CR1.jar:8.0.0.CR1]

          at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:309) [jboss-invocation-1.2.0.Beta4.jar:1.2.0.Beta4]

          at org.jboss.as.ejb3.component.interceptors.AdditionalSetupInterceptor.processInvocation(AdditionalSetupInterceptor.java:55) [wildfly-ejb3-8.0.0.CR1.jar:8.0.0.CR1]

          at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:309) [jboss-invocation-1.2.0.Beta4.jar:1.2.0.Beta4]

          at org.jboss.invocation.ContextClassLoaderInterceptor.processInvocation(ContextClassLoaderInterceptor.java:64) [jboss-invocation-1.2.0.Beta4.jar:1.2.0.Beta4]

          at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:309) [jboss-invocation-1.2.0.Beta4.jar:1.2.0.Beta4]

          at org.jboss.invocation.InterceptorContext.run(InterceptorContext.java:325) [jboss-invocation-1.2.0.Beta4.jar:1.2.0.Beta4]

          at org.wildfly.security.manager.WildFlySecurityManager.doChecked(WildFlySecurityManager.java:437) [wildfly-security-manager-1.0.0.Beta3.jar:1.0.0.Beta3]

          at org.jboss.invocation.AccessCheckingInterceptor.processInvocation(AccessCheckingInterceptor.java:61) [jboss-invocation-1.2.0.Beta4.jar:1.2.0.Beta4]

          at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:309) [jboss-invocation-1.2.0.Beta4.jar:1.2.0.Beta4]

          at org.jboss.invocation.InterceptorContext.run(InterceptorContext.java:325) [jboss-invocation-1.2.0.Beta4.jar:1.2.0.Beta4]

          at org.jboss.invocation.PrivilegedWithCombinerInterceptor.processInvocation(PrivilegedWithCombinerInterceptor.java:80) [jboss-invocation-1.2.0.Beta4.jar:1.2.0.Beta4]

          at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:309) [jboss-invocation-1.2.0.Beta4.jar:1.2.0.Beta4]

          at org.jboss.invocation.ChainedInterceptor.processInvocation(ChainedInterceptor.java:61) [jboss-invocation-1.2.0.Beta4.jar:1.2.0.Beta4]

          at org.jboss.as.ejb3.timerservice.TimedObjectInvokerImpl.callTimeout(TimedObjectInvokerImpl.java:104) [wildfly-ejb3-8.0.0.CR1.jar:8.0.0.CR1]

          at org.jboss.as.ejb3.timerservice.TimedObjectInvokerImpl.callTimeout(TimedObjectInvokerImpl.java:114) [wildfly-ejb3-8.0.0.CR1.jar:8.0.0.CR1]

          at org.jboss.as.ejb3.timerservice.task.TimerTask.callTimeout(TimerTask.java:196) [wildfly-ejb3-8.0.0.CR1.jar:8.0.0.CR1]

          at org.jboss.as.ejb3.timerservice.task.TimerTask.run(TimerTask.java:168) [wildfly-ejb3-8.0.0.CR1.jar:8.0.0.CR1]

          at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471) [rt.jar:1.7.0_15]

          at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334) [rt.jar:1.7.0_15]

          at java.util.concurrent.FutureTask.run(FutureTask.java:166) [rt.jar:1.7.0_15]

          at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) [rt.jar:1.7.0_15]

          at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) [rt.jar:1.7.0_15]

          at java.lang.Thread.run(Thread.java:722) [rt.jar:1.7.0_15]

          at org.jboss.threads.JBossThread.run(JBossThread.java:122) [jboss-threads-2.1.1.Final.jar:2.1.1.Final]

       

       

       

      Case.2

       

      @Local(ISessionTimeoutMonitor.class)

      @Singleton

      @Startup

      @TransactionManagement(TransactionManagementType.BEAN)

      public class SessionIdleTimeoutMonitor implements ISessionTimeoutMonitor{

          Logger log = Logger.getLogger(SessionIdleTimeoutMonitor.class); 

       

          @SuppressWarnings("unused")

          @Resource

          private SessionContext sessionContext;

       

          @Resource TimerService timerService;

       

          @PersistenceContext

          protected EntityManager em;

       

       

          @EJB

          INmsCoreService nmsCoreService;

       

          @PostConstruct

          public void createTimer() {

              timerService.createIntervalTimer(

                        10000L,

                        nmsCoreService.getSessionIdleCheckInterval(),//intervalDuration

                        new TimerConfig(null,false)

                        );     

          }

       

          @Timeout

          public void timeout( ){

              try{

                  Date expireDate = new Date( System.currentTimeMillis() - nmsCoreService.getSessionIdleTimeout() );

                  int i = em.createNamedQuery(Session.NamedQuery_name_DeleteIdleTimeoutRecords)

                  .setParameter(Session.NamedQuery_param_DeleteIdleTimeoutRecords_lastUpdated,expireDate)

                  .executeUpdate();

              }catch(Exception e){

                  log.error(e);

              }

          }

      }

       

      The Exception is:

      11:04:23,412 ERROR [com.ybxiang.nms.ejb.timer.SessionIdleTimeoutMonitor] (EJB default - 1) java.lang.IllegalStateException: Illegal attempt to set lock mode on a non-SELECT query

       

       

      This works well in jboss 7.1.3 and 7.2.0

       

      Message was edited by: jaikiran pai - Changed the thread subject to be more accurate