org.hibernate.HibernateException: No Hibernate Session bound to thread

2014-11-24 09:04:13 · 作者: · 浏览: 0
如果遇到以上异常的话,恐怕是你的数据源配置的问题了,首先你可以排除一些可能,比如说数据源没问题的,即是DataSource没问题的话,就可以进一步去确定是什么环节出现的问题。刚开始我也是对这个异常感觉到很陌生,很多问题终究还是要一个人去解决。在我重做一遍这个实验之后,我发现这个问题是你的SessionFactory注解时候的一些问题。切确的来说你是要应该理解Session中的一个getCurrentSession() 与openSession()这两个方法的问题。这就是这个问题的实质问题了。重点理解在SSH架构中,spring如何对事务和hibernate session进行管理
1.getCurrentSession()是必须提交事务的。所以呢,你在用到session.getCurrentSession()的这个方法一定是要配置声明式事务管理。具体的声明式管理可以去网上google一下就知道怎么配置了。
2.openSession()恰恰是与以上的方法想法,它不需要提交事务。但是他的资源必须手动关闭。
所以简单的解决方法有两种是
1.获得session的时候你就用openSession就行了。
2如果用到getCurrenctSession()的话,你就在sessionFactory 那个bean中配置
thread
org.hibernate.transaction.JDBCTransactionFactory
以下的环节是
< xml version="1.0" encoding="UTF-8" >
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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
mysql.jdbc.Driver" />
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
com.ssh.test.model
org.hibernate.dialect.MySQLDialect
true