依据错误原理解决Hibernate执行出现No CurrentSessionContext configured!错误


(1)异常信息例如以下:

严重: Servlet.service() for servlet action threw exception
java.lang.RuntimeException: <u>No CurrentSessionContext configured!</u>
	at com.lc.utils.HibernateUtil.executeQuery(HibernateUtil.java:56)
	at com.lc.service.UsersService.checkUser(UsersService.java:22)
	at com.lc.struts.action.LoginAction.login(LoginAction.java:38)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
	at java.lang.reflect.Method.invoke(Method.java:597)
	at org.apache.struts.actions.DispatchAction.dispatchMethod(DispatchAction.java:269)
	at org.apache.struts.actions.DispatchAction.execute(DispatchAction.java:170)
	at org.apache.struts.chain.commands.servlet.ExecuteAction.execute(ExecuteAction.java:58)
	at org.apache.struts.chain.commands.AbstractExecuteAction.execute(AbstractExecuteAction.java:67)
	at org.apache.struts.chain.commands.ActionCommandBase.execute(ActionCommandBase.java:51)
	at org.apache.commons.chain.impl.ChainBase.execute(ChainBase.java:190)
	at org.apache.commons.chain.generic.LookupCommand.execute(LookupCommand.java:304)
	at org.apache.commons.chain.impl.ChainBase.execute(ChainBase.java:190)
	at org.apache.struts.chain.ComposableRequestProcessor.process(ComposableRequestProcessor.java:283)
	at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1913)
	at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:462)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:230)
	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:104)
	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:261)
	at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
	at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:581)
	at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
	at java.lang.Thread.run(Thread.java:619)

(2)启动调试找到问题原因所在:

问题出错的根本是:No CurrentSessionContext configured! (当然下边的错误解释肯定是非常多的!)

表示:没有CurrentSession上下文配置! 这就想到了我们在利用CurrentSession的时候,事实上就是getCurrentSession的时候没有在hibernate.cfg.xml文件里配置!

(3)解释原理:

1、在 SessionFactory(会话工厂)启动的时候。Hibernate 会依据配置创建对应的 CurrentSessionContext,

在getCurrentSession()被调用的时候,实际被运行的方法是 CurrentSessionContext.currentSession()。

2、这里的“依据配置”就是例如以下配置:

* 假设使用的是本地事务(jdbc事务 本地事务:数据库仅仅有一个)

<property name="hibernate.current_session_context_class">thread</property>
* 假设使用的是全局事务(jta事务 全局事务:数据库有多个)
<property name="hibernate.current_session_context_class">jta</property> 

(4)所以解决这个问题的办法就是依据需求在hibernate.cfg.xml文件里加上上边的配置就可以!


注:转载请注明出处!


posted @ 2017-07-30 18:18  yfceshi  阅读(1849)  评论(0编辑  收藏  举报