org.hibernate.HibernateException: No Session found for current thread
生活随笔
收集整理的這篇文章主要介紹了
org.hibernate.HibernateException: No Session found for current thread
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
spring、springmvc和hibernate整合
在sessionFactory.getCurrentSession()時(shí),出現(xiàn)以下異常 No Session found for current thread但使用sessionFactory.openSession()是沒(méi)有任何問(wèn)題的 嚴(yán)重: 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的話會(huì)自動(dòng)關(guān)閉,而openSession需要你手動(dòng)關(guān)閉。
如果你正在查詢,使用的openSession而沒(méi)有手動(dòng)關(guān)閉,多次之后會(huì)導(dǎo)致連接池溢出。
getCurrentSession是與當(dāng)前線程綁定的,當(dāng)前線程結(jié)束時(shí),session也會(huì)自動(dòng)關(guān)閉
getCurrentSession是比較安全的,建議使用 2. 獲取getCurrentSession時(shí),為什么會(huì)出現(xiàn)以上異常
是因?yàn)闆](méi)有配置事務(wù)
spring hibernate事務(wù)的流程
在方法開(kāi)始之前,獲取session,把session和當(dāng)前線程綁定,這樣就可以在dao中使用sessionFactory.getCurrentSession()來(lái)獲取session了,然后開(kāi)啟事務(wù)。
若方法正常結(jié)束,即沒(méi)有異常,則提交事務(wù),解除和當(dāng)前線程綁定的session,關(guān)閉session。
若方法出現(xiàn)異常,則回滾事務(wù),解除綁定,關(guān)閉session。
3. 怎么配置事務(wù)
如下是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 的聲明式事務(wù) 1. 配置 hibernate 的事務(wù)管理器 --><bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"><property name="sessionFactory" ref="sessionFactory"></property></bean><!-- 2. 配置事務(wù)屬性 --> <tx:advice id="txAdvice" transaction-manager="transactionManager"><tx:attributes><tx:method name="get*" read-only="true"/><tx:method name="*"/></tx:attributes></tx:advice><!-- 3. 配置事務(wù)切入點(diǎn), 再把事務(wù)屬性和事務(wù)切入點(diǎn)關(guān)聯(lián)起來(lái)<aop:config><aop:pointcut expression="execution(* com.wzy.services.*.*(..))" id="txPointcut"/><aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/></aop:config>--> </beans>
?4. 為什么我spring的配置文件已經(jīng)配置好了,還是會(huì)出那個(gè)異常呢
如果是spring、strut2、hibernate整合的話,事務(wù)在spring的配置文件中配置就行
但如果是spring、springmvc、hibernate整合的話,事務(wù)切入點(diǎn)得配置在springmvc的配置文件中
因?yàn)閟pring和springmvc是兩個(gè)容器
只需要把spring配置文件中的<!-- 3. 配置事務(wù)切入點(diǎn), 再把事務(wù)屬性和事務(wù)切入點(diǎn)關(guān)聯(lián)起來(lái)-->
放到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. 配置事務(wù)切入點(diǎn), 再把事務(wù)屬性和事務(wù)切入點(diǎn)關(guān)聯(lián)起來(lái)--><aop:config><aop:pointcut expression="execution(* com.wzy.services.*.*(..))" id="txPointcut"/><aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/></aop:config> </beans>?
總的來(lái)說(shuō),當(dāng)spring、springmvc和hibernate整合時(shí) 在sessionFactory.getCurrentSession()時(shí),出現(xiàn)No Session found for current thread 需要配置事務(wù),并且在springmvc配置文件中配置事務(wù)的切入點(diǎn)?
?
轉(zhuǎn)載于:https://www.cnblogs.com/wwzyy/p/5560935.html
總結(jié)
以上是生活随笔為你收集整理的org.hibernate.HibernateException: No Session found for current thread的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: IOS设计模式之二(门面模式,装饰器模式
- 下一篇: DbUtil的介绍使用