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

如果遇到以上異常的話,恐怕是你的數據源配置的問題了,首先你可以排除一些可能,比如說數據源沒問題的,即是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>
發佈了41 篇原創文章 · 獲贊 6 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章