【问题标题】:Overriding transactional Annotation Spring + hibernate覆盖事务注解 Spring + hibernate
【发布时间】:2015-11-18 18:10:55
【问题描述】:

我有这个 DAO:

@Transactional("transactionManager")
public class DAO{
     public void save(String a){...}
}

我有这门课:

public class test{
...
@Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRES_NEW)
    public void save(){
        DAO.save("a");
        DAO.save("b");
       }
    }

我希望“保存”方法在抛出异常时回滚,但是当发生异常时它似乎不起作用它不回滚,什么是正确的方法? DAO 中的所有其他方法都是事务性的。有没有办法可以覆盖覆盖的事务设置?

编辑: 我已经更新为,但在抛出异常时仍然无法正常工作:

public class test{
...

    public void save(){
        Service.test(a,b);
       }
    }

    public class Service{
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRES_NEW)
        public void testSave(object a, object b){
            dao.updateEntry(a);
            dao.updateEntry(b);

        }
    }

【问题讨论】:

  • 我认为服务方法应该是事务性的,而不是 dao 方法。
  • @Jens 我应该将所有注释移动到服务层吗?因为我在 daos 中有很多不需要事务处理的方法,这就是我注释 dao 的原因。
  • 大部分情况下事务在服务层方法上,因为方法中所有的dao访问都属于一个事务。所以这就是为什么注释应该在服务层方法上。

标签: java spring hibernate transactions propagation


【解决方案1】:

从 Dao 层中移除 Transactional 注解并放置一个Transactional annotation in your service layer。看看我的代码:-

@Transactional
@Service
public class Service {

    @Autowired
    private Dao1 dao1;

    @Autowired
    private Dao2 dao2;

    public Dao1 getDao1() {
        return dao1;
    }

    public void setDao1(Dao1 dao1) {
        this.dao1 = dao1;
    }

    public Dao2 getDao2() {
        return dao2;
    }

    public void setDao2(Dao2 dao2) {
        this.dao2 = dao2;
    }

    public void insertData(){
        dao1.insert1();
        dao2.insert2();
    }

在上述代码中,如果 dao2.insert2() 失败,则 dao1.insert1() 将回滚。

如果您在服务类中有多个具有不同事务属性的方法:
您可以使用以下规则在公共方法上定义 @Transactional 注释:-

当使用代理时,你应该应用@Transactional注解 仅适用于具有公共可见性的方法。如果您确实注释受保护, 带有 @Transactional 注释的私有或包可见方法, 没有引发错误,但带注释的方法没有表现出 配置事务设置。

链接1:Transactional annotation on whole class + excluding a single method

事务支持配置设置:-

1) spring-config.xml

<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:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd
     http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd">

      <context:component-scan base-package="com.concept" />
     <tx:annotation-driven transaction-manager="txManager"/>

      <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
      </bean>

     <bean id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
        <property name="url" value="" />
        <property name="username" value="" />
        <property name="password" value="" />
    </bean> 
</beans>

【讨论】:

  • 如果我有多个具有不同事务属性的方法?
  • 您可以使用适当的传播属性在方法级别定义@Transactional注解。
  • 在你的问题中检查我的编辑..编辑部分并告诉我它是否不起作用。
  • 看到你的编辑你试图在testSave上做回滚,我想在保存方法出错时做回滚
猜你喜欢
  • 2020-12-14
  • 2015-08-20
  • 1970-01-01
  • 1970-01-01
  • 2016-07-16
  • 2018-01-02
  • 2014-10-26
  • 2017-02-18
  • 2012-12-14
相关资源
最近更新 更多