-->
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: Concurrency issues
PostPosted: Tue Apr 05, 2011 10:53 am 
Newbie

Joined: Mon Apr 04, 2011 1:03 pm
Posts: 2
Hi All,

I have been struggling with a concurency issue for the last couple of days and I have to admit that it is starting to drive me crazy. I am using Spring 3 along with Hibernate.

I have the following controller (in the MVC layer) :

Code:
@RequestMapping("/smppMTDS")
public ModelAndView handleRequest(HttpServletRequest request,
         HttpServletResponse response) throws Exception {
         
   String returnStr = receivedMTManager.getUnsentDSReceivedMT();
      
   response.setContentType("text");
   PrintWriter out = response.getWriter();
   if(null!=returnStr)
   {
      out.println(returnStr);
   }
         
   return null;
}


This method is called by another server several times per second.
"getUnsentDSReceivedMT" is in the service layer, its content is the following :

Code:
@Transactional(isolation=Isolation.SERIALIZABLE)
public String getUnsentDSReceivedMT()
{
   ReceivedMT receivedMT =  receivedMTDao.getUnsentDSReceivedMT();
      
   if(receivedMT!=null)
   {
      receivedMT.setDsSentTime(new Date());
      receivedMTDao.update(receivedMT);
                  
      return("Id="+receivedMT.getMessageId()+";Status="+receivedMT.getDs()+";");
         
   }
   return null;
}



I need "receivedMTDao.getUnsentDSReceivedMT()" to return 2 different objects when 2 requests come in simultaneously. Otherwise the other server gets twice the same data and causes problems. Unfortunately, when 2 requests come in quickly, this happens:"receivedMTDao.getUnsentDSReceivedMT()" returns twice the same data. This is bewildering as I was assuming that a read lock was used when data are read from the DB with this isolation level.

If anyone could help me out with this, I would be really grateful. Any idea ?

Note: I am using MySQL INNODB.

Note : "getUnsentDSReceivedMT" is in the dao layer. Here it is :

Code:
public ReceivedMT getUnsentDSReceivedMT() {
      
   StringBuilder stringQuery = new StringBuilder("");
   stringQuery.append(" FROM ReceivedMT ");
   stringQuery.append(" WHERE dsSentTime is null ");      
   Query query = sessionFactory.getCurrentSession().createQuery(stringQuery.toString()).setMaxResults(1);
         
   return (ReceivedMT) query.uniqueResult();
      
}



Sylvain


Top
 Profile  
 
 Post subject: Re: Concurrency issues
PostPosted: Tue Apr 05, 2011 7:33 pm 
Beginner
Beginner

Joined: Sat Sep 24, 2005 11:04 pm
Posts: 21
Try setting an explicit lock mode for your query:
Code:
public ReceivedMT getUnsentDSReceivedMT() {
   return (ReceivedMT)sessionFactory.getCurrentSession().createQuery(
      "select mt from ReceivedMT mt where dsSentTime is null")
      .setLockMode("mt", LockMode.PESSIMISTIC_WRITE)
      .setMaxResults(1)
      .uniqueResult();
}


Top
 Profile  
 
 Post subject: Re: Concurrency issues
PostPosted: Wed Apr 06, 2011 2:23 am 
Beginner
Beginner

Joined: Mon Apr 04, 2011 3:31 am
Posts: 41
hi Read this ...

http://www.roseindia.net/hibernate/firstexample.shtml

_________________
Thanks
Niki


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.