【问题标题】:Hibernate Spring Transactions休眠 Spring 事务
【发布时间】:2009-11-30 13:14:46
【问题描述】:

我正在尝试让我的 Spring 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

【问题讨论】:

  • 你能发布你的错误的完整堆栈跟踪吗?
  • 我想我差不多明白了,我的 jta 事务管理器已被识别,所有 bean 都已初始化但我现在得到:无法打开连接。

标签: java hibernate spring


【解决方案1】:

将此添加到会话工厂 bean (mySessionFactory):

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

然后看解释here

【讨论】:

  • 这可行,但有一件事:如果我使用 Spring 托管事务,我也会收到此异常。
  • 想我明白了:hibernate.current_session_context_class=我的配置中没有设置线程,这就是为什么它不适用于 Spring 托管事务。
  • SessionFactory 已创建,我的 bean 也是。但是没有查询被执行,我是否还遗漏了什么?
  • 我认为这不能解决 OP 的问题。 OP 想要使用 spring 管理的事务,但是这个设置禁用了它。
【解决方案2】:

也许是个愚蠢的问题,但你是如何在你的 dao 中获得会话的?

您应该使用“getCurrentSession()”来获取会话,或者更好地为您的 dto 扩展 spring 的 HibernateDAOSupport。

您的配置与我们在最近的一个项目中使用的配置相同,并且没有您遇到的任何问题。无需进行比您在原始问题中显示的更多的配置。然而,我们确实总是在我们的 DAO 中扩展 HibernateDAOSupport。我们也在 Jboss 上。

我不确定您是否应该将该属性设置为 false (exposeTransactionAwareSessionFactory) - 没有它它应该可以工作。

我发现 LocalSessionFactoryBean 的 javadocs 特别有用。

【讨论】:

  • 我正在使用“getCurrentSession()”方法,我将使用 HibernateDAOSupport 扩展我的 DAO 并对其进行测试。我遇到了另一个问题/异常: Proxy$xx 无法针对 DomainP1Impl 进行强制转换 - 好吧,它是针对接口编程的,所以我真的不知道这里出了什么问题。
【解决方案3】:

没有查询被执行,因为你的数据源都搞砸了 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"/>

【讨论】:

    猜你喜欢
    • 2015-10-15
    • 2011-08-19
    • 2011-11-16
    • 2016-03-19
    • 2014-06-09
    • 1970-01-01
    • 2015-08-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多