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