在使用struts2+spring4+hibernate4出现了No Session found for current thread具体如下:
org.hibernate.HibernateException: No Session found for current thread
at org.springframework.orm.hibernate4.SpringSessionContext.currentSession(SpringSessionContext.java:106)
at org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:1014)
at com.xyc.mt.base.DaoSupportImpl.getSession(DaoSupportImpl.java:40)
at com.xyc.mt.base.DaoSupportImpl.save(DaoSupportImpl.java:44)
at cn.itcast.oa.test.TestService.testSerivce(TestService.java:74)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
在网上查找有人说配上这个<propkey="hibernate.current_session_context_class">thread</prop>就可以解决,配上后再次测试出现org.hibernate.HibernateException:No CurrentSessionContext configured!或save is not valid without active transacton异常
处理这个问题处理好久没解决,我原来的做法是service直接继承BaseDao和BaseDaoImpl,其中BaseDao和BaseDaoImpl在com.base包下,service在com.service包下
<!-- 配置事务切面 -->
<aop:config>
<aop:pointcut id="serviceOperation"
expression="execution(* com.service..*.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="serviceOperation" />
</aop:config>
后来因为这个错误,尝试抽取BaseService和BaseServiceImpl并放在了com.base包下,结果同样报以上错误,后来突发奇想把BaseService和BaseServiceImpl移到了com.service,问题解决,测试通过
发现问题:配置的事务横切于在com.service包下所有类,而真正执行增删改查等数据库访问的方法是在com.Base包下。猜想:虽然service继承了BaseDao但是因为BaseDao不放在service包下,所以还是无法使用声明式事务管理...求高手帮验证...
提供参考:http://blog.csdn.net/dearLHB/article/details/20213283