问题描述
HibernateUtil代码packagecommon;importorg.hibernate.Session;importorg.hibernate.SessionFactory;importorg.hibernate.cfg.Configuration;publicclassHibernateUtil{privatestaticfinalSessionFactorysessionFactory=buildSessionFactory();privatestaticSessionFactorybuildSessionFactory(){try{returnnewConfiguration().configure().buildSessionFactory();}catch(Throwableex){System.err.println("InitialSessionFactorycreationfailed."+ex);thrownewExceptionInInitializerError(ex);}}publicstaticSessionFactorygetSessionFactory(){returnsessionFactory;}}
ExamInfoImpl代码packageservice.impl;importjava.io.UnsupportedEncodingException;importjava.util.List;importorg.hibernate.Query;importorg.hibernate.Session;importorm.exam_info;importservice.ExamInfo;importcommon.HibernateUtil;publicclassExamInfoImplextendsHibernateUtilimplementsExamInfo{/*(non-Javadoc)*@seeservice.ExamInfo#addExamInfo(orm.exam_info)*/publicvoidaddExamInfo(exam_infoexaminfo){Sessionss=HibernateUtil.getSessionFactory().openSession();ss.beginTransaction();ss.save(examinfo);ss.getTransaction().commit();}publicListgetExamInfo(){Sessionss=HibernateUtil.getSessionFactory().openSession();Listlist=null;Queryq=ss.createSQLQuery("select*fromSYS_EXAM_INFO").addEntity(exam_info.class);list=q.list();returnlist;}publicStringgetExamInfoById(Stringid){Sessionss=HibernateUtil.getSessionFactory().openSession();Listlist=null;StringBuildersb=newStringBuilder();Queryq=ss.createSQLQuery("select*fromSYS_EXAM_INFOwhereexam_eid="+id).addEntity(exam_info.class);list=q.list();if(list!=null){exam_infoobj=(exam_info)list.get(0);try{sb.append("[").append("{EXAM_EID:").append(obj.getEID()).append(",EXAM_ETIME:"").append(obj.getETIME()).append("",EXAM_MCOUNT:").append(obj.getMCOUNT()).append(",EXAM_MONEY:").append(obj.getMONEY()).append(",EXAM_NOWCOUNT:").append(obj.getNOWCOUNT()).append(",EXAM_OTHER:"").append(newString(obj.getOTHER().getBytes("iso-8859-1"),"gb2312")).append(""}").append("]");}catch(UnsupportedEncodingExceptione){e.printStackTrace();}}returnsb.toString();}}
代码逻辑上实现都没问题,问题是当我第一次调用ExamInfoImpl的getExamInfoById方法的时候第一次可以成功调用但是第二次再用的时候就提示org.hibernate.SessionException:Sessionisclosed!项目中就只用了Hibernate,希望高手能给出问题的原因和解决的办法谢谢
解决方案
解决方案二:
你代码应该没铁全,你找找看哪里把session关了,肯定就是不该关的时候关闭了session。。
解决方案三:
ExamInfoImpl类干吗要继承HibernateUtils类不懂个人建议最好一个数据库操作对应一个session操作,而且这里只给了个方法代码不全
解决方案四:
HibernateUtil类的问题在Hibernate给我们提供的一个sessionFactory类中,有如下的声明及使用:privatestaticfinalThreadLocal<Session>threadLocal=newThreadLocal<Session>();publicstaticvoidcloseSession()throwsHibernateException{Sessionsession=(Session)threadLocal.get();threadLocal.set(null);if(session!=null){session.close();}}从上面的代码应该可以看出.hibernate的session是取的当前线程中的.而你自定义类中。只是关闭了session.而并未对当前线程做处理。这样的话,会导致取到的session永远是关闭的.如果我的理解有误,请高手纠正
解决方案五:
[code]1111[/code]
解决方案六:
[java]111[/java]
解决方案七:
修改配置文件在一对多或多对多后面加lazy="false"
解决方案八:
dddd
解决方案九:
供参考packagecom.sideline.Hibernate;importorg.hibernate.HibernateException;importorg.hibernate.Session;importorg.hibernate.cfg.Configuration;/***ConfiguresandprovidesaccesstoHibernatesessions,tiedtothe*currentthreadofexecution.FollowstheThreadLocalSession*pattern,see{@linkhttp://hibernate.org/42.html}.*/publicclassSessionFactory{/***Locationofhibernate.cfg.xmlfile.*LocationshouldbeontheclasspathasHibernateuses*#resourceAsStreamstylelookupforitsconfigurationfile.*Thedefaultclasspathlocationofthehibernateconfigfileis*inthedefaultpackage.Use#setConfigFile()toupdate*thelocationoftheconfigurationfileforthecurrentsession.*/privatestaticStringCONFIG_FILE_LOCATION="/hibernate.cfg.xml";privatestaticfinalThreadLocal<Session>threadLocal=newThreadLocal<Session>();privatestaticConfigurationconfiguration=newConfiguration();privatestaticorg.hibernate.SessionFactorysessionFactory;privatestaticStringconfigFile=CONFIG_FILE_LOCATION;privateSessionFactory(){}/***ReturnstheThreadLocalSessioninstance.Lazyinitialize*the<code>SessionFactory</code>ifneeded.**@returnSession*@throwsHibernateException*/publicstaticSessiongetSession()throwsHibernateException{Sessionsession=(Session)threadLocal.get();if(session==null||!session.isOpen()){if(sessionFactory==null){rebuildSessionFactory();}session=(sessionFactory!=null)?sessionFactory.openSession():null;threadLocal.set(session);}returnsession;}/***Rebuildhibernatesessionfactory**/publicstaticvoidrebuildSessionFactory(){try{configuration.configure(configFile);sessionFactory=configuration.buildSessionFactory();}catch(Exceptione){System.err.println("%%%%ErrorCreatingSessionFactory%%%%");e.printStackTrace();}}/***Closethesinglehibernatesessioninstance.**@throwsHibernateException*/publicstaticvoidcloseSession()throwsHibernateException{Sessionsession=(Session)threadLocal.get();threadLocal.set(null);if(session!=null){session.close();}}/***returnsessionfactory**/publicstaticorg.hibernate.SessionFactorygetSessionFactory(){returnsessionFactory;}/***returnsessionfactory**sessionfactorywillberebuildedinthenextcall*/publicstaticvoidsetConfigFile(StringconfigFile){SessionFactory.configFile=configFile;sessionFactory=null;}/***returnhibernateconfiguration**/publicstaticConfigurationgetConfiguration(){returnconfiguration;}}
解决方案十:
有正解么。等。
解决方案十一:
每个方法都要关闭session,像这样的:publicListgetExamInfo(){Sessionss=HibernateUtil.getSessionFactory().openSession();Listlist=null;Queryq=ss.createSQLQuery("select*fromSYS_EXAM_INFO").addEntity(exam_info.class);list=q.list();ss.close();returnlist;}你把方法里面都加下试下看,看还会不会报错
解决方案十二:
修改Hibernate配制文件将配制文件中的lazy属性设置为"false"
解决方案十三:
引用11楼huai__ye的回复:
修改Hibernate配制文件将配制文件中的lazy属性设置为"false"
??????????