Having a deadlock in webservice + hibernate... No idea why.
I have the following webservice code (core code of the function)
public String createImageRecipe(...)
{
Session session = ICDBHibernateUtil.getSessionFactory().getCurrentSession();
try
{
session.beginTransaction();
User user = (User) session.load(User.class, userid);
// Create the recipe
Recipe recipe = new Recipe();
...
...
...
session.save(recipe);
session.save(user);
...
...
...
session.update( recipe );
// Add new entry to activity log
Activitylog activityLog = new Activitylog( user,
(byte) ActivityTypeEnum.USER_SHARED_RECIPE.ordinal(),
new Date() );
activityLog.setRecipe(recipe);
TimelineProcessor.saveTimeline( activityLog, null, true );
session.getTransaction().commit();
endMeasurement();
return recipe.getRecipeid() + "," + recipe.getImageRecipeUrl();
}
catch (RuntimeException e)
{
ICDBHibernateUtil.getSessionFactory().getCurrentSession().getTransaction().rollback();
throw e;
}
}
Now the timeline thread (TimelineProcessor) does the following (after getting the message):
Session session = ICDBHibernateUtil.getTimelineSessionFactory().openSession();
try {
session.getTransaction().begin();
...
...
...
TimelineId tlId = new TimelineId();
tlId.setUserId(userId);
User u = new User();
u.setUserid(userId);
Timeline tl = new Timeline(tlId, timelineData.getActivitylog(), u);
session.save(tl);
session.getTransaction().commit();
session.close();
} catch (Exception e) {
logger.error("Error while processing timeline :: ", e);
session.getTransaction().rollback();
}
}
Exceptions from log: Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLTransactionRollbackException: Deadlock found when trying to get lock; try restarting transaction at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
org.hibernate.HibernateException: Illegal attempt to associate a collection with two open sessions at org.hibernate.collection.AbstractPersistentCollection.setCurrentSession(AbstractPersistentCollection.java:435)
ERROR com.icdb.TimelineProcessor - processTimeline - Error while processing timeline :: org.hibernate.exception.LockAcquisitionException: could not insert: [com.icdb.data.Activitylog]
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLTransactionRollbackException: Deadlock found when trying to get lock; try restarting transaction at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
ERROR com.icdb.TimelineProcessor - processTimeline - Error while processing timeline :: org.hibernate.HibernateException: Illegal attempt to associate a collection with two open sessions
Will really appriciate help... Thanks a lot