当前位置: 代码迷 >> 驱动开发 >> Spring4+Hibernate4+测试驱动开发时发现的有关问题:No session (上)
  详细解决方案

Spring4+Hibernate4+测试驱动开发时发现的有关问题:No session (上)

热度:170   发布时间:2016-04-28 10:17:44.0
Spring4+Hibernate4+测试驱动开发时发现的问题:No session (上)

在Service层上配置了切面加入了事务控制,但是在Dao层上没有配置事务。

用Spring-test编写了测试Dao的类,但是运行的时候报错:

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:1012)

?

首先,不存在线程绑定的问题,我没有配置<prop key="hibernate.current_session_context_class">这个参数,注意是根本没加上这句话。

?

其次,也不存在Spring context之间的冲突问题。

?

最后发现问题出在SessionFactory.getCurrentSession必须放在事务中进行。在本文面对的问题上,因为是使用Spring test进行测试用例的编写,[email protected]务管理。

?

?

这个事情挺神奇的,明天结合Spring和Hibernate的源码看看它们是怎么管理Session和事务的。

  相关解决方案