gpt4 book ai didi

Spring事务传播REQUIRED,REQUIRES_NEW

转载 作者:行者123 更新时间:2023-12-03 01:21:48 28 4
gpt4 key购买 nike

在下面的代码方法doService1()中更新了正确的sql,但是doService2() sql有一些问题,但是当我调用doService()时即使 doService2()sql 异常,它也必须将 doService1() 更新提交到数据库,因为 doService2() 具有 REQUIRES_NEW Propagation 类型,但是当我执行此 doService1() 更新时不会提交数据库..

@Service public class DbClass {

static Logger log = Logger.getLogger(
DbClass.class.getName());

@Autowired
private DataSource dataSource;

@Transactional(propagation=Propagation.REQUIRED)
public void doService(){
doService1();
doService2();
}

@Transactional(propagation=Propagation.REQUIRED)
public void doService1(){
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
String sql = " update BATCHJOBSTATUS set PROCESSINGDATE = '20130322' " ;
int rowCount1 = jdbcTemplate.update(sql);
System.out.println(" rowCount1 >" + rowCount1);
}

@Transactional(propagation=Propagation.REQUIRES_NEW)
public void doService2(){
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
String sql = " update aa set a_name = 'hhh' where a_id = 4 and " ;
int rowCount1 = jdbcTemplate.update(sql);
System.out.println(" rowCount2 >" + rowCount1);
}
}
<小时/>

正如你们建议的那样,也按照以下方式进行测试,但仍然面临同样的问题。这里我在一个单独的类中 doService2() 但即使仍然有与上面相同的问题

@Service
public class DbClass {

static Logger log = Logger.getLogger(
DbClass.class.getName());

@Autowired
private DataSource dataSource;

@Autowired
private DbClass2 dbClass2;

@Transactional
public void doService(){
doService1();
dbClass2.doService2();
}

@Transactional(propagation=Propagation.REQUIRED )
public void doService1(){
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
String sql = " update BATCHJOBSTATUS set PROCESSINGDATE = '20130322' " ;
int rowCount1 = jdbcTemplate.update(sql);
System.out.println(" rowCount1 >" + rowCount1);

}


}


@Service
public class DbClass2 {


@Autowired
private DataSource dataSource;

@Transactional(propagation=Propagation.REQUIRES_NEW)
public void doService2() {
System.out.println("*******doService2*********`");

JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);

String sql = " update aa set a_name = 'hhh' where a_id_ = 4 " ;

int rowCount2 = jdbcTemplate.update(sql);

System.out.println(" rowCount2 >" + rowCount2);

}

}



<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:oxm="http://www.springframework.org/schema/oxm"
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.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd

http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/oxm http://www.springframework.org/schema/oxm/spring-oxm-3.0.xsd">


<context:annotation-config />

<context:component-scan base-package="com.spring"/>

<tx:annotation-driven transaction-manager="txManager1" proxy-target-class="true"/>



<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
<property name="url" value="jdbc:oracle:thin:@192.168.8.121:1521:h3" />
<property name="username" value="admin" />
<property name="password" value="admin" />
</bean>


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

<bean id="batchJob" class="com.spring.jdbc.BatchJob">
</bean>


</beans>

最佳答案

我之前也遇到过同样的问题,在这里解决了:Strange behaviour with @Transactional(propagation=Propagation.REQUIRES_NEW)

使用默认设置,调用doService2()时不会创建任何新的事务代理。来自同一个类,因此您的注释不是用户。

要避免此问题,您可以输入 doService2()在另一个类中或通过如下声明使用aspectJ进行事务:<tx:annotation-driven transaction-manager="transactionManager" mode="aspectj"/>

最佳解决方案将取决于您的应用程序。 (这里第二个似乎更合适)

关于Spring事务传播REQUIRED,REQUIRES_NEW,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15795985/

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