gpt4 book ai didi

java - 重写 Spring + Hibernate 事务性注解

转载 作者:行者123 更新时间:2023-12-02 04:25:50 24 4
gpt4 key购买 nike

我有这个 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");
}
}

我希望“save”方法在抛出异常时回滚,但是当异常发生时它似乎不起作用它不会回滚,正确的方法是什么? 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 层删除 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() 将回滚。

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

When using proxies, you should apply the @Transactional annotation only to methods with public visibility. If you do annotate protected, private or package-visible methods with the @Transactional annotation, no error is raised, but the annotated method does not exhibit the configured transactional settings.

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

Transaction support configuration setup:-

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>

关于java - 重写 Spring + Hibernate 事务性注解,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32197225/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com