问题描述
在下面的代码方法doService1()
中更新正确的sql,但是doService2()
sql有一些问题,但是当我调用doService()
时,即使doService2()
具有doService1()更新提交给数据库>,因为doService2()
具有REQUIRES_NEW Propagation
类型,但是当我修女时,此doService1()
更新不会提交数据库.
in following code method doService1()
update correct sql but doService2()
sql has some issue , but when i call doService()
it has to commit the doService1()
update to DB even though the doService2()
has a sql exception
because doService2()
has a REQUIRES_NEW Propagation
type but when i nun this doService1()
update does not commit DB..
@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()
在一个单独的类中,尽管仍然存在与上面相同的问题
as your guys suggestion test in following way as well but still facing the same problem.here i doService2()
in a separate class but even though still have the same problem as above
@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>
推荐答案
我之前也遇到过同样的问题,并且在这里得到了解决:
I had the same problem earlier and it was solved here : Strange behaviour with @Transactional(propagation=Propagation.REQUIRES_NEW)
使用默认设置,当您从同一类调用doService2()
时不会创建任何新的交易代理,因此您的注释不是用户.
Using default setting, there won't be any new transaction proxy created when you call doService2()
from the same class, as a result your annotation is not user.
为避免此问题,您可以将doService2()
放在另一个类中,或通过如下声明它来使用AspectJ进行事务处理:<tx:annotation-driven transaction-manager="transactionManager" mode="aspectj"/>
To avoid this issue you can put doService2()
in another class or use aspectJ for transaction by declaring it like this : <tx:annotation-driven transaction-manager="transactionManager" mode="aspectj"/>
最佳解决方案将取决于您的应用程序. (这里的第二个似乎更合适)
Best solution will depend on your application. (The second one here seems more appropriate)
这篇关于春季交易传播,REQUIRED,REQUIRES_NEW的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!