public void save(ScClassSort instance) {
log.debug("saving ScClassSort instance");
Session session = null;
Transaction tx = null;
try {
session = getSession();
tx = session.beginTransaction();
if (instance.getParentId()!=0)
{
//这个分支出错!
ScClassSort parent = findById(instance.getParentId());
instance.setChild(0);
instance.setDepth(parent.getDepth()+1);
instance.setParentDir(parent.getParentDir()+parent.getDir()+"/");
instance.setParentPath(parent.getParentPath()+","+parent.getId());
System.out.println(instance);
session.save(instance);
parent.setChild(parent.getChild()+1);
System.out.println(parent);
session.update(parent);
}
else
{
//这个分支不出错!
instance.setChild(0);
instance.setDepth(0);
instance.setParentDir("/");
instance.setParentPath("0");
System.out.println(instance);
session.save(instance);
}
tx.commit();
log.debug("save successful");
} catch (RuntimeException re) {
if(tx!=null)
{
tx.rollback();
}
log.error("save failed", re);
throw re;
}
finally
{
//session.close();
}
}
我把session.close()不注释的时候提示Session is already closed,估计是session.commit()的时候自动关闭了,但是注释了的话还是出错,
不过奇怪的是如果执行else{...}中的分支不出错!