2010-11-14 59 views
2

試圖讓Spring的事務管理工作,但它並沒有像我希望的那樣工作。Spring&Hibernate:沒有綁定到線程的會話

DEBUG: org.springframework.orm.hibernate3.SessionFactoryUtils - Opening Hibernate Session 
DEBUG: org.springframework.orm.hibernate3.SessionFactoryUtils - Opening Hibernate Session 
DEBUG: org.hibernate.impl.SessionImpl - opened session at timestamp: 12897642913 
DEBUG: org.springframework.orm.hibernate3.SessionFactoryUtils - Closing Hibernate Session 
DEBUG: org.springframework.orm.hibernate3.SessionFactoryUtils - Closing Hibernate Session 
14-nov-2010 20:51:31 org.apache.catalina.core.StandardWrapperValve invoke 
SEVERE: Servlet.service() for servlet mvc-dispatcher threw exception 
org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here 

我已經感動我的屬性,以我的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:aop="http://www.springframework.org/schema/aop" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:oxm="http://www.springframework.org/schema/oxm" 
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd 
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd 
     http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd 
     http://www.springframework.org/schema/oxm http://www.springframework.org/schema/oxm/spring-oxm-3.0.xsd 
     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd 
     http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd"> 

    <bean id="myDataSource" 
     class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 
     <property name="driverClassName" value="com.mysql.jdbc.Driver" /> 
     <property name="url" value="jdbc:mysql://localhost/kidscalcula" /> 
     <property name="username" value="root" /> 
     <property name="password" value="" /> 
    </bean> 

    <bean class="org.springframework.orm.hibernate3.LocalSessionFactoryBean" 
     id="sessionFactory"> 
     <property name="dataSource" ref="myDataSource" /> 
     <property name="mappingResources"> 
      <list> 
       <value>be/howest/kidscalcula/model/Foto.hbm.xml</value> 
       <value>be/howest/kidscalcula/model/Kindleerplanonderdeel.hbm.xml 
       </value> 
       <value>be/howest/kidscalcula/model/Klas.hbm.xml</value> 
       <value>be/howest/kidscalcula/model/Leerkracht.hbm.xml</value> 
       <value>be/howest/kidscalcula/model/Leerling.hbm.xml</value> 
       <value>be/howest/kidscalcula/model/Leerplan.hbm.xml</value> 
       <value>be/howest/kidscalcula/model/LeerplanOefenreeks.hbm.xml 
       </value> 
       <value>be/howest/kidscalcula/model/Leerplanonderdeel.hbm.xml</value> 
       <value>be/howest/kidscalcula/model/Niveau.hbm.xml</value> 
       <value>be/howest/kidscalcula/model/Oefenreeks.hbm.xml</value> 
       <value>be/howest/kidscalcula/model/Overgangsregel.hbm.xml</value> 
       <value>be/howest/kidscalcula/model/Rapport.hbm.xml</value> 
       <value>be/howest/kidscalcula/model/RapportLeerplanonderdeel.hbm.xml 
       </value> 
       <value>be/howest/kidscalcula/model/Schooljaar.hbm.xml</value> 
       <value>be/howest/kidscalcula/model/Subonderdeel.hbm.xml</value> 
      </list> 
     </property> 
     <property name="hibernateProperties"> 
      <props> 
       <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> 
       <prop key="hibernate.hbm2ddl.auto">update</prop> 
       <prop key="hibernate.connection.pool_size">3</prop> 
       <prop key="hibernate.show_sql">true</prop> 
       <prop key="hibernate.format_sql">true</prop> 
       <prop key="hibernate.use_sql_comments">true</prop> 
       <prop key="hibernate.cache.use_second_level_cache">false</prop> 
      </props> 
     </property> 
    </bean> 


    <!-- Transaction manager for a single Hibernate SessionFactory (alternative 
     to JTA) --> 
    <bean id="transactionManager" 
     class="org.springframework.orm.hibernate3.HibernateTransactionManager"> 
     <property name="sessionFactory"> 
      <ref bean="sessionFactory" /> 
     </property> 
    </bean> 

    <tx:annotation-driven /> 
</beans> 

我只是注入我的方法,或在我的課堂我org.hibernate.SessionFactory到我的DAO,並使用@Transactional註解。

@Repository 
public class LeerlingDAOimpl implements LeerlingDAO { 
    @Autowired 
    public LeerlingDAOimpl(SessionFactory sessionFactory) { 
     this.sessionFactory = sessionFactory; 
    } 

有沒有人有一個想法,我忘了,配置有誤?基本的想法是通過這種配置通常,只要在我的服務層中調用事務方法,就會打開一個會話。這也允許我使用同一個事務方法加載懶集合。但由於某種原因,它甚至找不到該線程。

+0

獲取異常的堆棧跟蹤。 TransactionInterceptor是否出現在它中? – meriton 2010-11-14 20:32:55

+3

我發現了這個問題。我的註釋掃描發生在另一個xml中,然後是我的。它從來沒有發現它必須開始一個交易,所以從來沒有找到一個會話。 – toomuchcs 2010-11-14 20:39:56

回答

7

由於您的配置看起來不錯,可能有以下幾個原因:

  • 你用new(即不是從春天獲得)

  • 你叫@Transactional方法創建的對象上調用@Transactional方法從同一對象的另一種方法(在這種情況下,交易方面不適用,因爲它是基於代理的)

  • 你有@Transactional方法對象在<tx:annotation-driven>未生效的背景下宣佈(例如,您的@Transactional對象在...-servlet.xml聲明,而<tx:annotation-driven>applicationContext.xml僅申報)

+0

它是從Spring MVC控制器調用的,在這裏注入服務。這兩種第一種可能性都不適用。至於第三:可能就是這樣!我會測試一下。不過,我並沒有像春季推薦的那樣拆分配置;只有一個root-context.xml導入3個其他xml,我的servlet調度器,hibernate配置和另一個用於JSON。 – toomuchcs 2010-11-14 20:28:50

+0

非常感謝。我在這個問題上已經打破了約2天的時間。從來沒有想過這麼微不足道的東西!但我很高興,我學到了另一件關於春天的東西,我永遠不會忘記;) – toomuchcs 2010-11-14 20:35:43

相關問題