【问题标题】:@Transactional in Spring+HibernateSpring+Hibernate 中的@Transactional
【发布时间】:2012-10-15 11:38:15
【问题描述】:

我在我的 Web 应用程序中使用 Spring 3.1 + Hibernate 4.x。 在我的 DAO 中,我将用户类型对象保存为以下

sessionFactory.getCurrentSession().save(user);

但得到以下异常:

org.hibernate.HibernateException: save is not valid without active transaction

我在 SO 上搜索并找到了类似的 question,解决方案如下:

   Session session=getSessionFactory().getCurrentSession();
   Transaction trans=session.beginTransaction();
   session.save(entity);
   trans.commit();

这样就解决了问题。但是在那个解决方案中,手动开始和提交事务有很多混乱。

我不能在不手动开始/提交事务的情况下直接使用sessionFactory.getCurrentSession().save(user); 吗?

我也尝试在我的服务/dao 方法上使用@Transactional,但问题仍然存在。

编辑:这是我的 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:p="http://www.springframework.org/schema/p"
     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-3.1.xsd
     http://www.springframework.org/schema/tx
     http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
     http://www.springframework.org/schema/aop 
     http://www.springframework.org/schema/aop/spring-aop-3.1.xsd">


  <!-- enable the configuration of transactional behavior based on annotations -->
  <tx:annotation-driven transaction-manager="txManager"/>

    <bean id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource"
        p:driverClassName="${db.driverClassName}" p:url="${db.url}"
        p:username="${db.username}" p:password="${db.password}" />

    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="packagesToScan" value="com.myapp.entities" />
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
            </props>
        </property>
    </bean>


    <bean id="txManager"
        class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory">
            <ref bean="sessionFactory" />
        </property>
    </bean>

</beans>

我正在使用以下 Hibernate 4 依赖项:

<!-- Hibernate Dependency -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>4.1.7.Final</version>
        </dependency>

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>4.1.1.Final</version>
        </dependency>

        <dependency>
            <groupId>cglib</groupId>
            <artifactId>cglib</artifactId>
            <version>${cglib.version}</version>
            <scope>runtime</scope>
        </dependency>

请帮忙。

【问题讨论】:

  • 能发一下spring的配置文件吗
  • stackoverflow.com/questions/1079114/… 在这个问题上有一些很好的建议。我肯定会在服务层(不是 DAO 层)中创建事务,使用 @Transactional 或通过 AOP
  • @JigarParekh 我已经发布了配置文件内容并更新了我的问题。
  • andyb :是的。将@Transactional 放置在您的服务类中是一种很好的做法。我也将注释放在了我的 Service 方法上,但问题仍然存在。

标签: spring hibernate transactions


【解决方案1】:

基本上需要做的是从 applicationContext.xml 文件中删除 Hibernate 属性的以下行:

<prop key="hibernate.current_session_context_class">thread</prop>

移除后,Hibernate 使用 Spring 进行事务管理

祝大家好运。

【讨论】:

  • 我删除了那行,但之后出现了这个异常org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here
  • 我否决了您的答案,因为我认为 hibernate.current_session_context_class 与此错误无关。但删除它实际上解决了错误。对不起。你能分享一个解释这个的链接吗?谢谢。
【解决方案2】:

我认为您使用的是 Hibernate 4.x 那么为什么您在应用程序上下文文件中使用 Hibernate 3 事务管理器?

<bean id="txManager"
    class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory">
        <ref bean="sessionFactory" />
    </property>
</bean>

我觉得应该是

<bean id="txManager"
    class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <property name="sessionFactory">
        <ref bean="sessionFactory" />
    </property>
</bean>

只需尝试使用 hibernate 4 事务管理器和 @Transactional 属性就可以了。

【讨论】:

  • java.lang.ClassNotFoundException: org.hibernate.engine.spi.SessionFactoryImplementor。我认为org.springframework.orm.hibernate4.HibernateTransactionManager 不是一个有效的类。
  • 这是有效的类static.springsource.org/spring/docs/current/javadoc-api/org/… 看起来你缺少一些jar,如果你使用的是maven,那么发布pom文件。
  • 是的。我会试着找到丢失的罐子。
  • 我在更新的问题中列出了 hibernate 4 依赖项。
  • 将事务管理器更新到版本 4 不起作用。仍然遇到同样的异常:org.hibernate.HibernateException: save is not valid without active transaction.
【解决方案3】:

使用 Spring XML 的工作版本和 @Transactional 注释的 DAO 类,您是否在 Spring XML 中定义 DAO? (也许作为原型)因为如果您不是,那么据我所知,您的 DAO 不会在事务方面进行 AOP。我认为这是最简单的方法。此示例来自Spring 3 Doc,第 10.5.6 节使用@Transactional。

  <!-- this is the service object that we want to make transactional -->
  <bean id="fooService" class="x.y.service.DefaultFooService"/>

  <!-- enable the configuration of transactional behavior based on annotations -->
  <tx:annotation-driven transaction-manager="txManager"/>

...您可以将 DefaultFooService 替换为您的 DAO。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-02-23
    • 2019-07-10
    • 2017-12-10
    • 1970-01-01
    • 1970-01-01
    • 2011-01-22
    • 2011-07-19
    • 2011-05-11
    相关资源
    最近更新 更多