当前位置:编程学习 > JAVA >>

多线程hibernate session is closed!问题

大家好,我在一个类里面执行多线程,会报如下错误:
Exception in thread "池中线程-20" Exception in thread "池中线程-34" org.hibernate.SessionException: Session is closed!
at org.hibernate.impl.AbstractSessionImpl.errorIfClosed(AbstractSessionImpl.java:72)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1138)
at org.hibernate.impl.QueryImpl.list(QueryImpl.java:102)
at com.ffcs.upc.thread.modle_thread.ModleThread.run(ModleThread.java:45)
at com.ffcs.upc.util.ThreadPoolUtil$PooledThread.run(ThreadPoolUtil.java:160)

线程类:
public class ModleThread  implements Runnable {

public ModleThread(String mdn,String mainId) {
super();
this.mdn = mdn;
this.mainId = mainId;
service = SpringContextHolder.getBean("updateModleService");
}
public void run() {

TUpcRemainMobile remainMobile = new TUpcRemainMobile();
List<TUpcRemainMobile> list = service.createQuery("from TUpcRemainMobile where mainId=? and mdn=?", mainId,mdn).list();
}
}

主类里面就是启动多个ModleThread。

多线程 hibernate session --------------------编程问答-------------------- session 关闭了,看看哪里关了 --------------------编程问答-------------------- 看看session的生存周期 --------------------编程问答-------------------- debug每一句执行,看着session状态,找到在哪里关闭即可。 --------------------编程问答-------------------- service = SpringContextHolder.getBean("updateModleService");
这个在构造方法执行,使用的是父线程session,而run中使用的新线程。应避免。
补充:Java ,  Web 开发
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,