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: Junit test for method with row locking - any advice?
PostPosted: Mon Oct 27, 2008 6:27 am 
Newbie

Joined: Tue Aug 26, 2008 8:57 am
Posts: 3
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version: 3.1.3, Java: 1.4

I've added row locking to a method to prevent two users updating the same row at the same time by adding UPGRADE_NOWAIT to the get before the save.

Code:
public void saveBatch(final Batch batch) throws FunctionalException, SystemException {
   if (batch.getBatchId() != null) {
      Batch checkUpdate = null;
      try {
         checkUpdate = (Batch) getHibernateTemplate().get(Batch.class, batch.getBatchId(), LockMode.UPGRADE_NOWAIT);
      } catch (CannotAcquireLockException e) {
         throw new FunctionalException(LOCATION, Constants.Errors.CONFLICT_UPDATE, Constants.Errors.Errors_Code.CONFLICT_UPDATE);
      }

      if (checkUpdate == null) {
         throw new FunctionalException(LOCATION, Constants.Errors.OBJECT_HAS_ALREADY_BEEN_DELETED,
                           Constants.Errors.Errors_Code.OBJECT_HAS_ALREADY_BEEN_DELETED);
      }

      if (!(checkUpdate.getLastModifiedOn().equals(batch.getLastModifiedOn()))) {
         throw new FunctionalException(LOCATION, Constants.Errors.CONFLICT_UPDATE, Constants.Errors.Errors_Code.CONFLICT_UPDATE);
      }
   }

   getHibernateTemplate().saveOrUpdate(batch);
}


I'd like to write a unit test for this, but can't work out the best way to mimic two users trying to save at the same time.

Has anyone tried this and can give me any pointers please?


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.