-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: multi-thread keeps different version of data
PostPosted: Thu Sep 16, 2004 4:47 am 
Newbie

Joined: Mon Jul 12, 2004 2:52 am
Posts: 17
Hibernate version:2.1

My system creates threads that each of them would:
read table Sequence for nextId, increment the id and save back to Sequence
save the new record into Orders with the read id

shortened code:
Code:
   public synchronized void syncOrder(Orders o) {
      try {
         Collection c;
         beginTransaction();
            try {
               Sequence s;
               List l = session.createCriteria(Sequence.class)
                  .add(Expression.eq("type", "ORD"))
                  .list();
                  // read old values
         
                  // update this rec
                  Sequence s = (Sequence) l.iterator().next();
                  int newId = s.getCurId().intValue();
                  s.setCurId(new Integer(newId + 1));
                  session.flush();
               }
               o.setId(s.getId());
            } finally {
            }
            session.save(o);
            logger.info(id + " saved Order " + o.getId() + " at " + o.getModifiedOn() + " as " + o.getStatus());
            endTransaction(true);
      } catch (Exception e) {
         logger.error(id + ":" + o.getId() + "(" + o.getReferenceNo() + ") caused " + e);
      }
   }


The method containing the critical section has been synchronized and it seems only 1 thread enters at any time.

However, the log shows that threads got old values of Sequence, which has been updated by other theads, thus generating duplicated id for Orders...

I've tried flushing the session at various points, re-beginning the transaction, passing 1 session to all threads... but no luck

how can I deal with it?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.