在 webservice + hibernate 中陷入僵局......不知道为什么。
我有以下webservice代码(函数的核心代码)
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;
}
}
现在时间线线程 (TimelineProcessor) 执行以下操作(在收到消息后):
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();
}
}
日志中的异常:原因:com.mysql.jdbc.exceptions.jdbc4.MySQLTransactionRollbackException:尝试获取锁时发现死锁;尝试在 sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) 处重新启动事务
org.hibernate.HibernateException:非法尝试将集合与 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