0 Replies Latest reply on Jul 24, 2007 5:41 AM by serj f

    how to safe update

    serj f Newbie

      hello

      I want to know what settings should have for safe updates using stateless beans (if it is possible), when have concurrency. I try to test using this code:

      //this code is from Runnable objects (I have many objects of this type):

       for(int i=0;i<UPDATE_NO;i++){
       int amount = ran.nextInt(1000);
       amountSum +=amount;
       testSessionC.adjust2Balance(balance.getId(),amount);
       }
      


      //stateless bean:
      @Stateless
      @Remote(TestSessionC.class)
      @Local(TestSessionC.class)
      public class TestSessionCBean implements TestSessionC{
      
       @PersistenceContext(unitName = "...")
       EntityManager em;
      
       public void adjust2Balance(long id, int amount) {
       Balance b = em.find(Balance.class, id);
       em.lock(b, LockModeType.WRITE);
       float old = b.getAmount();
       b.setAmount(old+amount);
       em.merge(b);
       }
      
      }
      


      my Balance entity use version control:
       @Version
       @Column(name = "OPTLOCK")
       public int getVersion() {
       return version;
       }
      
      


      thanks for any ideea