Spring3.2整合hibernate4.2报错:No Session found for current thread

高金山 发布于 2014/08/17 13:09
阅读 555
收藏 0

我本来用的hibernate3的+spring3.2,我现在将hibernate3升级到hibernate4.2后,总报这个错:

2014-08-17 13:04:10,132 ERROR (UserDaoImpl.java:41) - login
org.hibernate.HibernateException: No Session found for current thread
at org.springframework.orm.hibernate4.SpringSessionContext.currentSession(SpringSessionContext.java:97)
at org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:993)
at com.back.dao.impl.UserDaoImpl.login(UserDaoImpl.java:33)

请各位高手帮我看看到底是什么问题?感谢!!!
我的配置文件如下:

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" 
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/aop 
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx 
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">


<context:annotation-config></context:annotation-config>
<context:component-scan base-package="com.back.dao.impl"></context:component-scan>
<context:component-scan base-package="com.back.service.impl"></context:component-scan>
<context:component-scan base-package="com.back.action"></context:component-scan>
<!-- 数据库连接池 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<property name="driverClass" value="org.gjt.mm.mysql.Driver" />
<property name="jdbcUrl"
value="jdbc:mysql://127.0.0.1:3306/push?autoReconnect=true&amp;useUnicode=true&amp;characterEncoding=UTF-8" />
<property name="user" value="root" />
<property name="password" value="gjs@y1" />
<!-- 初始化时获取的连接数,取值应在minPoolSize与maxPoolSize之间。Default: 3 chengkai@fee -->
<property name="initialPoolSize" value="1" />
<!-- 连接池中保留的最小连接数。 -->
<property name="minPoolSize" value="1" />
<!-- 连接池中保留的最大连接数。Default: 15 -->
<property name="maxPoolSize" value="300" />
<!-- 最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 -->
<property name="maxIdleTime" value="60" />
<!-- 当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 -->
<property name="acquireIncrement" value="5" />
<!-- 每60秒检查所有连接池中的空闲连接。Default: 0 -->
<property name="idleConnectionTestPeriod" value="60" />
</bean>
<!-- 数据源 -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="mappingResources">
<list>
<value>com/back/entity/User.hbm.xml</value>
<value>com/back/entity/Project.hbm.xml</value>
<value>com/back/entity/Product.hbm.xml</value>
<value>com/back/entity/Item.hbm.xml</value>
<value>com/back/entity/Machine.hbm.xml</value>
<value>com/back/entity/Integrate.hbm.xml</value>
<value>com/back/entity/Apk.hbm.xml</value>
<value>com/back/entity/Apkactive.hbm.xml</value>
<value>com/back/entity/Department.hbm.xml</value>
<value>com/back/entity/Channel.hbm.xml</value>
<value>com/back/entity/Pactive.hbm.xml</value>
<value>com/back/entity/Pinstall.hbm.xml</value>
<value>com/back/entity/Linkads.hbm.xml</value>
<value>com/back/entity/Linklog.hbm.xml</value>
<value>com/back/entity/Smsads.hbm.xml</value>
<value>com/back/entity/Smslog.hbm.xml</value>
<value>com/back/entity/BindLink.hbm.xml</value>
<value>com/back/entity/Statis.hbm.xml</value>
<value>com/back/entity/BindSms.hbm.xml</value>
</list>
</property>
<!-- hibernate.dialect=org.hibernate.dialect.MySQL5Dialect -->
<property name="hibernateProperties">
<props>
<prop key="current_session_context_class">thread</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.jdbc.batch_size">20</prop>
<prop key="format_sql">true</prop>
<prop key="show_sql">false</prop>
</props>
</property>
</bean>
<bean id="SpringContextUtil" class="com.back.util.SpringContextUtil" />
<bean id="txManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- 使用基于注解方式配置事务 -->
<tx:annotation-driven transaction-manager="txManager" />
</beans>
加载中
0
高金山
高金山

将如下这行去掉即可(去掉就是默认的Propagation.REQUIRED)
@Transactional(propagation = Propagation.NOT_SUPPORTED)

0
plugin
plugin
上次我也遇到。好像4.2以后把什么东西去掉了,忘了,我直接换成3.6了
0
高金山
高金山

引用来自“高金山”的评论

将如下这行去掉即可(去掉就是默认的Propagation.REQUIRED)
@Transactional(propagation = Propagation.NOT_SUPPORTED)

这篇文章很好:
http://www.yihaomen.com/article/java/466.htm
Hibernate4 No Session found for current thread原因

0
純白陰影
純白陰影
没有开opensessioninview
0
渔樵耕读
渔樵耕读

你没配置事务~ 

spring来管理hibernate的话  如果不声明事务的管理方式的话

openSession的话会报错~

返回顶部
顶部