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

来源:互联网 发布:最近什么网络歌曲好听 编辑:程序博客网 时间:2024/05/21 13:56

如果遇到以上异常的话,恐怕是你的数据源配置的问题了,首先你可以排除一些可能,比如说数据源没问题的,即是DataSource没问题的话,就可以进一步去确定是什么环节出现的问题。刚开始我也是对这个异常感觉到很陌生,很多问题终究还是要一个人去解决。在我重做一遍这个实验之后,我发现这个问题是你的SessionFactory注解时候的一些问题。切确的来说你是要应该理解Session中的一个getCurrentSession() 与openSession()这两个方法的问题。这就是这个问题的实质问题了。重点理解在SSH架构中,spring如何对事务和hibernate session进行管理

1.getCurrentSession()是必须提交事务的。所以呢,你在用到session.getCurrentSession()的这个方法一定是要配置声明式事务管理。具体的声明式管理可以去网上google一下就知道怎么配置了。

2.openSession()恰恰是与以上的方法想法,它不需要提交事务。但是他的资源必须手动关闭。


所以简单的解决方法有两种是

1.获得session的时候你就用openSession就行了。

2如果用到getCurrenctSession()的话,你就在sessionFactory 那个bean中配置

    <prop key="hibernate.current_session_context_class">
                    thread
                </prop>
                <prop key="hibernate.transaction.factory_class">
                    org.hibernate.transaction.JDBCTransactionFactory</prop>



以下的环节是

<?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: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">

    <context:annotation-config />
    <context:component-scan base-package="com.ssh.test" />

    <bean id="dataSource"
        class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost/spring" />
        <property name="username" value="root" />
        <property name="password" value="root" />
    </bean>

    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="packagesToScan">
            <list>
                <value>com.ssh.test.model</value>
            </list>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">
                    org.hibernate.dialect.MySQLDialect
                </prop>
                <prop key="hibernate.show_sql">true</prop>
                <!--
                <prop key="hibernate.current_session_context_class">
                    thread
                </prop>
                <prop key="hibernate.transaction.factory_class">
                    org.hibernate.transaction.JDBCTransactionFactory</prop>
                     -->
            </props>
        </property>
    </bean>


</beans>
原创粉丝点击