1 Reply Latest reply on Oct 8, 2011 7:31 AM by itays100

    Update multiple entities in one transaction

    itays100

      Hi All,


      I have an issue and looking for the best practice to solve it. I have a Quartz process to fetch all the entities from the DB and update them in one transaction. See the code below:




      import javax.persistence.EntityManager;
      import org.jboss.seam.ScopeType;
      import org.jboss.seam.annotations.AutoCreate;
      import org.jboss.seam.annotations.In;
      import org.jboss.seam.annotations.Name;
      import org.jboss.seam.annotations.Scope;
      import org.jboss.seam.annotations.Transactional;
      import org.jboss.seam.annotations.async.Asynchronous;
      import org.jboss.seam.annotations.async.Expiration;
      import org.jboss.seam.annotations.async.IntervalCron;
      import org.jboss.seam.async.QuartzTriggerHandle;
      
      @Name("processor")
      @AutoCreate
      @Scope(ScopeType.APPLICATION)
      public class ScheduleProcessor {
          @In private EntityManager entityManager;
           
           @Asynchronous
          @Transactional
          public QuartzTriggerHandle createQuartzUpdateTimer(
                      @Expiration Date when, @IntervalCron String interval) {
              List<Person> persons = entityManager.createQuery("from Person p");
                for (Person  person : persons) {
                     person.setValue(1000);
                }
                return null;
           }
      }
      
      
      Person.java
      
      @Entity
      @Table(name = "person", catalog = "myDb")
      public class Person implements java.io.Serializable {
      
           private BigDecimal value;
           
           @Column(name = "SOME_VALUE", nullable = true, precision = 5, scale=2)
           public BigDecimal getValue() {
                return ytdReturn;
           }
      
           public void setValue(BigDecimal value) {
                this.value = value;
           }
           
      }
      
      




      
      
      When Quartz job start I am getting the following exception, i'm pretty sure it's not related to specific validate.
      
      SEVERE: Exeception thrown whilst executing asynchronous call
      javax.persistence.RollbackException: Error while commiting the transaction
           at org.hibernate.ejb.TransactionImpl.commit(TransactionImpl.java:71)
           at org.jboss.seam.transaction.EntityTransaction.commit(EntityTransaction.java:110)
           at org.jboss.seam.util.Work.workInTransaction(Work.java:58)
           at org.jboss.seam.transaction.TransactionInterceptor.aroundInvoke(TransactionInterceptor.java:91)
           at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68)
           at org.jboss.seam.core.MethodContextInterceptor.aroundInvoke(MethodContextInterceptor.java:44)
           at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68)
           at org.jboss.seam.async.AsynchronousInterceptor.aroundInvoke(AsynchronousInterceptor.java:52)
           at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68)
           at org.jboss.seam.intercept.RootInterceptor.invoke(RootInterceptor.java:107)
           at org.jboss.seam.intercept.JavaBeanInterceptor.interceptInvocation(JavaBeanInterceptor.java:185)
           at org.jboss.seam.intercept.JavaBeanInterceptor.invoke(JavaBeanInterceptor.java:103)
           at com.action.ScheduleProcessor_$$_javassist_seam_2.createQuartzUpdateTimer(ScheduleProcessor_$$_javassist_seam_2.java)
           at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
           at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
           at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
           at java.lang.reflect.Method.invoke(Method.java:597)
           at org.jboss.seam.util.Reflections.invoke(Reflections.java:22)
           at org.jboss.seam.util.Reflections.invokeAndWrap(Reflections.java:144)
           at org.jboss.seam.async.AsynchronousInvocation$1.process(AsynchronousInvocation.java:62)
           at org.jboss.seam.async.Asynchronous$ContextualAsynchronousRequest.run(Asynchronous.java:80)
           at org.jboss.seam.async.AsynchronousInvocation.execute(AsynchronousInvocation.java:44)
           at org.jboss.seam.async.QuartzDispatcher$QuartzJob.execute(QuartzDispatcher.java:243)
           at org.quartz.core.JobRunShell.run(JobRunShell.java:202)
           at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:529)
      Caused by: org.hibernate.validator.InvalidStateException: validation failed for: com.Person
           at org.hibernate.validator.event.ValidateEventListener.validate(ValidateEventListener.java:148)
           at org.hibernate.validator.event.ValidateEventListener.onPreUpdate(ValidateEventListener.java:177)
           at org.hibernate.action.EntityUpdateAction.preUpdate(EntityUpdateAction.java:217)
           at org.hibernate.action.EntityUpdateAction.execute(EntityUpdateAction.java:65)
           at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:250)
           at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:234)
           at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:142)
           at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298)
           at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
           at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
           at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:338)
           at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
           at org.hibernate.ejb.TransactionImpl.commit(TransactionImpl.java:54)
           ... 24 more
      



      Thanks for any comment.