-->
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.  [ 3 posts ] 
Author Message
 Post subject: Something is wrong with my transactions
PostPosted: Wed Nov 25, 2009 9:15 am 
Newbie

Joined: Fri Nov 21, 2008 8:56 pm
Posts: 15
I'm doing something really simple with my BD, and it appears to be failing. The code is below. Basically, what myMethod() does is check if certain entry on my DB matches a certain criteria, and if it doesn't, it inserts an object that matches it.
Well, I've detected that when in my application two threads go trough that point at once, it sometimes saves the object twice. Am I doing something wrong with the transaction?

Code:
public void myMethod() {
   try{
      sessionFactory.getCurrentSession().beginTransaction();

      MyObject myObject = getSomeObjectFromDB("1234");
      if (myObject == null) {
         myObject = new MyObject();
         myObject.setSomeParam("1234");
   
         sessionFactory.getCurrentSession().saveOrUpdate(myObject);
      }

      sessionFactory.getCurrentSession().getTransaction().commit();

   } catch (Throwable d) {
      sessionFactory.getCurrentSession().getTransaction().rollback();
   }
}


public MyObject getSomeObjectFromDB(String param){
   Criteria criteria = sessionFactory.getCurrentSession().createCriteria(MyObject.class);
   criteria.add(Expression.eq("someParam", param));
   return criteria.list().isEmpty() ? null : (MyObject)criteria.list().get(0);
}


Top
 Profile  
 
 Post subject: Re: Something is wrong with my transactions
PostPosted: Wed Nov 25, 2009 9:32 am 
Expert
Expert

Joined: Thu Jan 08, 2009 6:16 am
Posts: 661
Location: Germany
Thats just a thread concurrency problem. One thread could execute the query being interrupted after getting the result. Now the other threads does the query also getting no result. So both threads will execute the persist. The easiest thing of hadling this issue is making the whole method synchronized.

_________________
-----------------
Need advanced help? http://www.viada.eu


Top
 Profile  
 
 Post subject: Re: Something is wrong with my transactions
PostPosted: Wed Nov 25, 2009 9:35 am 
Newbie

Joined: Fri Nov 21, 2008 8:56 pm
Posts: 15
Yeah, I came to that conclusion after posting. I thought that transactions blocked...


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.