/**
* <p>Title: executeInCyouSession</p>
* <p>Description: 将持久化操作放入事务中</p>
*
* @author youyan
* @param action -- 持久化操作(增删改查)
* @return Object -- 结果数据
* @throws CyouBaseException
*/
public Object executeInCyouSession(CyouCallback action) throws CyouBaseException
{
this.session = persistenManager.getCurrentCyouSession();
Object result = null;
try {
log.info("*****************begin transaction*****************");
session.beginTransaction();
result = action.doInTemplate(session);
session.commitTransaction();
log.info("*****************commit transaction*****************");
} catch (CyouBaseException e) {
session.rollbackTransaction();
log.info("!!!!!!!!!!!!!rollback transaction!!!!!!!!!!!!!");
log.error("error happend when executeInCyouSession(): " + e.getMessage());
throw new CyouBaseException
("error happend when executeInCyouSession(): " + e.getMessage() , e);
}
//由于hibernate是延迟加载机制,故如果在此地方关闭session,会导致延迟加载失败,
//报org.hibernate.SessionException: Session is closed! 异常
//另外可以采用lazy=false的配置策略
//事务提交后会自动关闭session,故注释以下session关闭代码,防止异常出现
/*finally
{
if(session != null && session.isConnected())
session.close();
}*/
return result;
}
org.hibernate.SessionException: Session is closed
最新推荐文章于 2021-02-21 10:32:04 发布