spring、springmvc和hibernate整合

在sessionFactory.getCurrentSession()时,出现以下异常
No Session found for current thread

但使用sessionFactory.openSession()是没有任何问题的
严重: Servlet.service() for servlet [springDispatcherServlet] in context with path [/Demo] threw exception [Request processing failed; nested exception is org.hibernate.HibernateException: No Session found for current thread] with root cause
org.hibernate.HibernateException: No Session found for current thread
at org.springframework.orm.hibernate4.SpringSessionContext.currentSession(SpringSessionContext.java:)
at org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:)
at com.wzy.dao.BookShopDaoImpl.getAll(BookShopDaoImpl.java:)
at com.wzy.services.impl.BookSerImpl.getALL(BookSerImpl.java:)
at com.wzy.controller.Test.hello(Test.java:)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
1. getCurrentSession和openSession之间有什么不同呢

getCurrentSession的话会自动关闭,而openSession需要你手动关闭。 
如果你正在查询,使用的openSession而没有手动关闭,多次之后会导致连接池溢出。
getCurrentSession是与当前线程绑定的,当前线程结束时,session也会自动关闭
getCurrentSession是比较安全的,建议使用
2. 获取getCurrentSession时,为什么会出现以上异常
是因为没有配置事务
spring hibernate事务的流程
在方法开始之前,获取session,把session和当前线程绑定,这样就可以在dao中使用sessionFactory.getCurrentSession()来获取session了,然后开启事务。 若方法正常结束,即没有异常,则提交事务,解除和当前线程绑定的session,关闭session。 若方法出现异常,则回滚事务,解除绑定,关闭session。

3. 怎么配置事务
如下是spring的配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd"> <context:component-scan base-package="com.wzy">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<context:property-placeholder location="classpath:db.properties"/>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="user" value="${jdbc.user}"></property>
<property name="password" value="${jdbc.password}"></property>
<property name="driverClass" value="${jdbc.driverClass}"></property>
<property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
<property name="initialPoolSize" value="${jdbc.initPoolSize}"></property>
<property name="maxPoolSize" value="${jdbc.maxPoolSize}"></property> </bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="mappingLocations" value="classpath:com/wzy/entities/*.hbm.xml"></property>
<!--
<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
-->
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
</bean> <!-- 配置 Spring 的声明式事务
. 配置 hibernate 的事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean> <!-- . 配置事务属性 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="get*" read-only="true"/>
<tx:method name="*"/>
</tx:attributes>
</tx:advice> <!-- . 配置事务切入点, 再把事务属性和事务切入点关联起来 <aop:config>
<aop:pointcut expression="execution(* com.wzy.services.*.*(..))" id="txPointcut"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>
</aop:config>
--> </beans>

 4. 为什么我spring的配置文件已经配置好了,还是会出那个异常呢

如果是spring、strut2、hibernate整合的话,事务在spring的配置文件中配置就行

但如果是spring、springmvc、hibernate整合的话,事务切入点得配置在springmvc的配置文件中

因为spring和springmvc是两个容器

只需要把spring配置文件中的<!-- 3. 配置事务切入点, 再把事务属性和事务切入点关联起来-->

放到springmvc的配置文件中就可以了,spring中的那块就可以去掉了

如下是springMVC的配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd"> <context:component-scan base-package="com.wzy"></context:component-scan> <!-- . 配置事务切入点, 再把事务属性和事务切入点关联起来-->
<aop:config>
<aop:pointcut expression="execution(* com.wzy.services.*.*(..))" id="txPointcut"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>
</aop:config>
</beans>
总的来说,当spring、springmvc和hibernate整合时
在sessionFactory.getCurrentSession()时,出现No Session found for current thread
需要配置事务,并且在springmvc配置文件中配置事务的切入点


org.hibernate.HibernateException: No Session found for current thread的更多相关文章

  1. spring+hibernate整合:报错org.hibernate.HibernateException: No Session found for current thread

    spring+hibernate整合:报错信息如下 org.hibernate.HibernateException: No Session found for current thread at o ...

  2. 分析 org.hibernate.HibernateException: No Session found for current thread

    /**      *      * org.hibernate.HibernateException: No Session found for current thread      * 分析:ge ...

  3. SSH dao层异常 org.hibernate.HibernateException: No Session found for current thread

    解决方法: 在 接口方法中添加 事务注解 即可. public interface IBase<PK extends Serializable, T> { @Transactional v ...

  4. hibernate在使用getCurrentSession时提示no session found for current thread

    大致错误片段 org.hibernate.HibernateException: No Session found for current thread at org.springframework. ...

  5. 关于spring3中No Session found for current thread!and Transaction的配置和管理(转)

    今天我是特别的郁闷,本来项目做到一半,以前都好好的,结果下午就出现问题,苦逼的到现在才解决.它出现问题的时候都一声不坑, ,(天啦,现在才发现CSDN啥时候把QQ表情给整过来了)就在注册用户的时候,咦 ...

  6. Hibernate4集成spring4报错----No Session found for current thread

    在编写一个Hibernate4集成spring4的小demo的时候出现了该错误: org.hibernate.HibernateException: No Session found for curr ...

  7. Hibernate4 No Session found for current thread原因

    Hibernate4 与 spring3 集成之后, 如果在取得session 的地方使用了getCurrentSession, 可能会报一个错:“No Session found for curre ...

  8. Spring+Hibernate4 Junit 报错No Session found for current thread

    论坛上有另外一篇更全面的帖子,jinnianshilongnian写的:http://www.iteye.com/topic/1120924 本文的环境是:  spring-framework-3.1 ...

  9. SSH2 No Session found for current thread原因

    Hibernate4 与 spring3 集成之后, 如果在取得session 的地方使用了getCurrentSession, 可能会报一个错:“No Session found for curre ...

随机推荐

  1. jquery——左右按钮点击切换一组图片功能

    一.最终效果 二.功能分析 1.需求分析 点击左边pre按钮,显示前面三个图片,点击右边的next按钮,显示后面的一组(三个)图片.初始化只显示next按钮,到最后一组只显示pre按钮,中间过程两按钮 ...

  2. UDS(ISO14229-2006) 汉译(No.2参考标准)

    下列参考文件对本文件的系统是不可或缺的.注明日期的参考,仅关于对其引用的版本适用.未注明日期的,仅最新引用的文档(包括任何修改)适用. ISO 7498-1,信息技术——开放系统互联(OSI)——基本 ...

  3. 一个过滤特殊字符的JS

    <script language="javascript"> function checkForms() { var iu, iuu, regArray=new Arr ...

  4. 使用jenkins配置.net mvc网站进行持续集成一

    最近好久没有更新文章了,因为好久没有写代码了,以至于我不知道同大家分享些什么,刚好,今天突然叫我学习下jenkins每日构建,我就把今天的学习笔记记录下来,这其中很多东西都是公司同事之前调研总结的,我 ...

  5. PHP基本语法和输出语句方式

  6. MongoDB分片(sharding)

    1.概念 分片(sharding)是指将数据拆分,将其分散存在不同的机器上的过程.有时也用分区(partitioning)来表示这个概念.将数据分散到不同的机器上,不需要功能强大的大型计算机就可以储存 ...

  7. (原)3.3 Zookeeper应用 - 负载均衡

    本文为原创文章,转载请注明出处,谢谢 负载均衡 1.原理 服务端启动创建临时节点(下图中servers下节点),临时节点数据包含负载信息 客户端启动获取服务器列表,并根据负载去连接一个负载较轻的服务器 ...

  8. (原) 2.1 Zookeeper原生API使用

    本文为原创文章,转载请注明出处,谢谢 Zookeeper原生API使用 1.jar包引入,演示版本为3.4.6,非maven项目,可以下载jar包导入到项目中 <dependency> & ...

  9. TouchPoint.js – 可视化展示 HTML 原型点击效果

    TouchPoint.js 是一个用于 HTML 原型展示的 JavaScript 库(作为UX过程的一部分),通过视觉表现用户在屏幕上的点击.TouchPoint 是高度可定制,非常适合屏幕录制,用 ...

  10. 关于图片的PNG与JPG、JIF格式

    一:GIF(Graphics Interchange Format) 简介 GIF图形交换格式是一种位图图形文件格式,以8位色(即256种颜色)重现真彩色的图像. 它实际上是一种压缩文档,采用LZW压 ...