-->
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.  [ 2 posts ] 
Author Message
 Post subject: A subtle (dangerous) bug?
PostPosted: Fri Jun 25, 2010 10:10 am 
Newbie

Joined: Tue Dec 18, 2007 2:06 pm
Posts: 9
I think I've stumbled on a pretty dangerous bug. Assume the following case:

Code:
static Affiliate affiliate;

   @Transactional
   @Test      
   @Rollback(false)
   public void testAffiliateCreate() {
      affiliate = dataPoolFactory.getAffiliate(); // get an object full of random-filled fields
      affiliate.setId(null);
      dataLayerWms.saveOrUpdate(affiliate);

                // assume DB fails at this point i.e. before the rollback
   }

   @Test      
   public void testAffiliateView() {
      System.out.println(affiliate.getId());
   }


Now assume that the save goes thru in the first method, but then the database performs a rollback. At this point the affiliate object has already been given an ID by hibernate (Affiliate table has an auto-inc key).

As you can see, in the second method, the id has been assigned even though it's not valid anymore. If you just attempt to save it again you can potentially clobber an existing record. The problem is that Hibernate should only set the ID when the transaction has been committed and not assume all will be well upon insert.

Comments?


Top
 Profile  
 
 Post subject: Re: A subtle (dangerous) bug?
PostPosted: Fri Jun 25, 2010 12:07 pm 
Newbie

Joined: Tue Dec 18, 2007 2:06 pm
Posts: 9
(I realise some DBs may actually not recycle auto-inc nos anyway -- wonder if hibernate will then attempt to use an INSERT for next time rather than UPDATE)


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

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.