-->
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: Handling StaleStateException
PostPosted: Fri Dec 25, 2015 1:19 pm 
Newbie

Joined: Fri Dec 25, 2015 9:49 am
Posts: 1
I am using Hibernate in my Project.
I have two methods which get called concurrently and they try to update the same rows. In the following code the two methods are updateSessionStatus() and updatePayloadTableAggregateTxtFileName().
To handle concurrent updates I am using version tag in the mapping file.
The problem is that the changes are not getting reflected in the database, whichever method last commit it's changes are getting reflected to solve this issue I am making use of version tag which is long in this case, and if there is version mismatch during update I am handling StaleStateException to merge the changes.
But it is not working and I am getting the exception as HHH000346: Error during managed flush [Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1]. Please help

public void updateSessionStatus(int jobId, String crcFileName, String crc) {
Session session = HibernateSessionFactory.getSessionFactory()
.openSession();
Transaction tx = null;
PayloadTableDao payloadTableDao = null;
try {
tx = session.beginTransaction();
Criteria crit = session.createCriteria(PayloadTableDao.class);
crit.add(Restrictions.eq("jobId", jobId));
ScrollableResults items = crit.scroll();
while (items.next()) {
payloadTableDao = (PayloadTableDao) items.get(0);
if (payloadTableDao.getError() == null) {
payloadTableDao.setCrcFileName(crcFileName);
payloadTableDao.setCrc(crc);
}
session.saveOrUpdate(payloadTableDao);

session.update(payloadTableDao);

}
tx.commit();
} catch (HibernateException asd) {
if (asd instanceof StaleStateException) {
// session.merge(payloadTableDao);
handleStale(payloadTableDao);
}
if (tx != null) {
tx.rollback();
}
LOGGER.debug(asd.getMessage());
} finally {
session.close();

}
}

public void updatePayloadTableAggregateTxtFileName(int jobId,
String txtFileName) {
Session session = HibernateSessionFactory.getSessionFactory()
.openSession();
Transaction tx = null;
PayloadTableDao payloadTableDao = null;
try {
tx = session.beginTransaction();
Criteria crit = session.createCriteria(PayloadTableDao.class);
crit.add(Restrictions.eq("jobId", jobId));
ScrollableResults items = crit.scroll();

while (items.next()) {
payloadTableDao = (PayloadTableDao) items.get(0);
if (payloadTableDao.getError() == null) {
payloadTableDao.setTxtFileName(txtFileName);
}

session.update(payloadTableDao);

}
tx.commit();
} catch (HibernateException asd) {
if (tx != null) {
tx.rollback();
}
if (asd instanceof StaleStateException) {
// session.merge(payloadTableDao);
handleStale(payloadTableDao);
}
LOGGER.debug(asd.getMessage());
} finally {
session.close();
}
}



public synchronized void handleStale(PayloadTableDao payloadTableDao) {
Session session = HibernateSessionFactory.getSessionFactory()
.openSession();
System.out
.println("***************************************************************");
Transaction tx = null;
try {
tx = session.beginTransaction();
session.merge(payloadTableDao);
tx.commit();
} catch (HibernateException e) {
LOGGER.error(e.getMessage(), e);
if (tx != null)
tx.rollback();
throw e;
} finally {
session.close();
}
}


Top
 Profile  
 
 Post subject: Re: Handling StaleStateException
PostPosted: Sat Dec 26, 2015 3:58 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
You can use a version-less optimistic locking mechanism.

This way you can reduce the chance of getting stale state exception false positives.


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.