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 threadat org.springframework.orm.hibernate4.SpringSessionContext.currentSession(SpringSessionContext.java:106)at org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:988)at com.wzy.dao.BookShopDaoImpl.getAll(BookShopDaoImpl.java:16)at com.wzy.services.impl.BookSerImpl.getALL(BookSerImpl.java:19)at com.wzy.controller.Test.hello(Test.java:36)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.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsdhttp://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 的声明式事务 1. 配置 hibernate 的事务管理器 --><bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"><property name="sessionFactory" ref="sessionFactory"></property></bean><!--  2. 配置事务属性 --> <tx:advice id="txAdvice" transaction-manager="transactionManager"><tx:attributes><tx:method name="get*" read-only="true"/><tx:method name="*"/></tx:attributes></tx:advice><!--  3. 配置事务切入点, 再把事务属性和事务切入点关联起来<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.xsdhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsdhttp://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><!--  3. 配置事务切入点, 再把事务属性和事务切入点关联起来--><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配置文件中配置事务的切入点


转载于:https://www.cnblogs.com/wwzyy/p/5560935.html

org.hibernate.HibernateException: No Session found for current thread相关推荐

  1. SpringMVC @Transactional的陷井大坑引发No Session found for current thread

    一.TransactionManager事务配置 (1)注解配置 配置spring的xml加@Transactional <tx:annotation-driven transaction-ma ...

  2. 错误记录(九)Could not obtain transaction-synchronized Session for current thread

    报错信息: org.hibernate.HibernateException: Could not obtain transaction-synchronized Session for curren ...

  3. [已解决] org.hibernate.HibernateException:未配置CurrentSessionContext

    Hibernate requires a lot of configurations and sometimes you get weird exception and there is no clu ...

  4. org.hibernate.HibernateException: connnection proxy not usable after transaction

    2019独角兽企业重金招聘Python工程师标准>>> org.hibernate.HibernateException: connnection proxy not usable ...

  5. [已解决] org.hibernate.HibernateException:没有活动事务,get无效

    Recently I was developing a simple hibernate application with version 4.3.5.Final and everything loo ...

  6. Could not obtain transaction-synchronized Session for current thread

    Could not obtain transaction-synchronized Session for current thread hibernate4和5不支持你用hibernate3的 ge ...

  7. hibernate的异常 Session was already closed

    今天写hibernate时候遇到一些异常 代码: Session session = sessionFactory.getCurrentSession(); session.beginTransact ...

  8. hibernate中SessionFactory,Session的理解?

    Session接口         Session接口对于Hibernate   开发人员来说是一个最重要的接口.然而在Hibernate中,实例化的Session是一个轻量级的类,创建和销毁它都不会 ...

  9. Hibernate中发生Session is closed 的另一种可能!

    Hibernate中发生"Session is closed" 的另一种可能! 文章分类:Java编程 关键字: hibernate session is closed Hiber ...

最新文章

  1. tornado+nginx上传视频文件
  2. Ajax请求session超时处理流程(DWZ)
  3. 剑指offer之判断链表是否包含环
  4. 6段Python代码刻画深度学习历史:从最小二乘法到深度神经网络
  5. 容器编排技术 -- Kubernetes Ingress解析
  6. Zookeeper常用命令详解(Zookeeper3.4.14)
  7. javax.net.ssl.SSLException: java.lang.RuntimeException: Could not generate DH keypair
  8. Web前端面试指导(十四):如何居中一个元素(正常、绝对定位、浮动元素)?
  9. Linux命令之passwd
  10. android API Guides学习--Introduction(1)
  11. 5、Python-函数
  12. 高效办公之Windows高效技巧提高你的工作效率
  13. Python|十五个超级炫酷的代码
  14. 在线翻译、词典、离线工具大全
  15. A* 算法求解八数码问题
  16. vue使用talkIngData统计
  17. ups监控软件测试表格,ups检测报告单完整版.docx
  18. 数学物理方法 数学物理方程
  19. Windows安装 choco包管理工具
  20. the disk drive for uuid=XXXX ( /media/sda1) is not reday yet or not present

热门文章

  1. 程序幽默:会让程序员争论起来的几个话题
  2. .NET Core 3.0 中的新变化
  3. mysql调度触发器,MySQL触发器:达到某个datetime时更新
  4. 考研计算机专业课统考吗,09考研计算机专业课统考增至4部分内容
  5. java replaceall 大小写_Java replaceAll不区分大小写
  6. 按钮 交互_SwiftUI中的微交互—菜单按钮动画
  7. 如何创建和谐的色彩系统
  8. TiDB 在小米的应用实践
  9. Sublime Text怎么快速建立一个html5页面模板
  10. nginx和apache限制IP地址访问的设置方法