簡體   English   中英

休眠春季交易

[英]Hibernate Spring Transactions

我正在嘗試使我的春季DAO正常工作,但我僅遇到此異常

PropertyAccessException 1: org.springframework.beans.MethodInvocationException: Property 'sessionFactory' threw exception; nested exception is org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here

我的applicationContext.xml看起來像這樣

<beans xmlns="http://www.springframework.org/schema/beans"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     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/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

    <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/> 

    <bean id="ds1Datasource" class="org.springframework.jndi.JndiObjectFactoryBean">
        <property name="jndiName" value="java:comp/env/jdbc/ds1"/>
     </bean>

 <bean id="mySessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="dataSource" ref="db2Datasource"/>
    <property name="mappingResources">
      <list>
        <value>hibernate/P1.hbm.xml</value>
      </list>
    </property>
    <property name="hibernateProperties">
      <value>
        hibernate.dialect=org.hibernate.dialect.DB2400Dialect
        hibernate.connection.autocommit=false
        hibernate.connection.charset=UTF-8
        hibernate.show_sql=true
      </value>
    </property>
  </bean>

<tx:annotation-driven transaction-manager="myTxManager"/>   
<bean id="myTxManager"  class="org.springframework.transaction.jta.JtaTransactionManager" />


  <bean id="p1" class="dao.DomainP1Impl">
    <property name="sessionFactory" ref="mySessionFactory" />
  </bean>

我的DAO是這樣的

@Transactional
public class DomainP1Impl implements DomainP1 {...}

我做錯了什么? 忘記提及:我正在使用JBoss AS 4.2.3

將此添加到會話工廠bean(mySessionFactory):

<property name="exposeTransactionAwareSessionFactory"><value>false</value></property>

然后在這里查看解釋。

也許是愚蠢的問題,但是您如何在dao中獲得會話?

您應該使用“ getCurrentSession()”來獲取會話,或者最好還是為您的dto擴展spring的HibernateDAOSupport。

您的配置與我們在最近的項目中使用的配置相同,並且沒有遇到任何問題。 不必進行比原始問題中所示更多的配置。 但是,我們確實總是在DAO中擴展了HibernateDAOSupport。 而且我們也在Jboss。

我不確定您是否應該將該屬性設置為false(exposeTransactionAwareSessionFactory)-如果沒有它,它應該可以工作。

我發現LocalSessionFactoryBean的javadocs特別有用。

沒有查詢執行,因為您的數據源都被ds1Datasourcedb2Datasource搞砸了

<bean id="ds1Datasource" class="org.springframework.jndi.JndiObjectFactoryBean">
            <property name="jndiName" value="java:comp/env/jdbc/ds1"/>
         </bean>

 <bean id="mySessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="dataSource" ref="db2Datasource"/>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM