本文介绍了覆盖事务注释Spring + Hibernate的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个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中的所有其他方法都是事务性的.有没有办法可以覆盖替代的事务设置?

I want the "save" method to rollback when it throws an exception, but it doesn't seem to work when an exception occurs it does not rollback, what is the proper approach for this? All the other methods in the DAO are transactional. Is there a way I can override the transactional settings of the override?

我已经更新为,并且在引发异常时仍然无法正常工作:

I have updated to be, and it is still not working when throwing exception:

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层中删除事务注释,并放置.看一下我的代码:-

Remove the Transactional annotation from the Dao layer and place a Transactional annotation in your service layer. Take a look at my code:-

@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()将回滚.

In above code, if dao2.insert2() fails then dao1.insert1() will rollback.

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

In case when you have multiple methods in service class with different transaction properties :
You can define the @Transactional annotation on your public methods with below rule:-

链接1:在整个类上的事务注释+排除单个方法

1)spring-config.xml

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>

这篇关于覆盖事务注释Spring + Hibernate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 06:59
查看更多