ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

需要Spring Transaction传播,REQUIRES_NEW

2019-10-08 15:24:11  阅读:253  来源: 互联网

标签:spring transactions propagation


在下面的代码方法中doService1()更新正确的sql但doService2()sql有一些问题,但是当我调用doService()时,它必须将doService1()更新提交到DB,即使doService2()有一个sql异常,因为doService2( )有一个REQUIRES_NEW传播类型但是当我没有这个doService1()更新不提交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()在一个单独的类中,但即使仍然有与上面相同的问题

@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,transactions,propagation
来源: https://codeday.me/bug/20191008/1872367.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有