복붙노트

[SPRING] Spring 트랜잭션 전파 필수, REQUIRES_NEW

SPRING

Spring 트랜잭션 전파 필수, REQUIRES_NEW

doService2 ()에 SQL 예외가 있지만 doService1 ()은 올바른 sql을 업데이트하지만 doService2 ()에는 몇 가지 문제가 있지만 doService ()를 호출하면 doService2 ()에 SQL 예외가 있어도 doService1 () )에는 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>

해결법

  1. ==============================

    1.나는 이전에 같은 문제가 있었고 여기에서 해결되었습니다 : @Transactional (propagation = Propagation.REQUIRES_NEW)을 사용한 이상한 행동

    나는 이전에 같은 문제가 있었고 여기에서 해결되었습니다 : @Transactional (propagation = Propagation.REQUIRES_NEW)을 사용한 이상한 행동

    기본 설정을 사용하면 동일한 클래스에서 doService2 ()를 호출 할 때 새 트랜잭션 프록시가 작성되지 않으므로 주석이 사용자가 아닙니다.

    이 문제를 피하려면 doService2 ()를 다른 클래스에 넣거나 다음과 같이 선언하여 트랜잭션에 대해 aspectJ를 사용할 수 있습니다.

    최상의 솔루션은 응용 프로그램에 따라 다릅니다. (두 번째 것은 여기에 더 적절합니다)

  2. ==============================

    2.doService2 ()에 대한 호출은 아마도 CGLIB 기반 프록시 대신 JDK 동적 프록시 (인터페이스 프록시)를 사용하고 있다고 가정하기 때문에 트랜잭션 실행 조언이 없을 것입니다. 이 방법의 작동 방식을 아직 모르는 경우 http://static.springsource.org/spring/docs/3.0.x/reference/aop.html#aop-proxying을 읽어 보시기 바랍니다.

    doService2 ()에 대한 호출은 아마도 CGLIB 기반 프록시 대신 JDK 동적 프록시 (인터페이스 프록시)를 사용하고 있다고 가정하기 때문에 트랜잭션 실행 조언이 없을 것입니다. 이 방법의 작동 방식을 아직 모르는 경우 http://static.springsource.org/spring/docs/3.0.x/reference/aop.html#aop-proxying을 읽어 보시기 바랍니다.

    CGLIB (target-class proxying)를 사용하지 않는다면 doService2 ()를 호출 할 때 Spring Transaction Advisor를 거치지 않을 것이다. 래퍼를 거치지 않고 직접 메소드를 호출하기 때문에 Spring은 시작시 서비스를 위해 생성한다. .

    doService2 ()를 다른 서비스 클래스로 이동 한 다음 doService2 ()를이 서비스에 삽입하여 예제를 사용할 수있다. 그러면 프록시를 통해 트랜잭션 충고가 실행됩니다.

    그렇지 않으면 프로젝트를 더 크게 변경할 준비가 된 경우 예제를 그대로 사용할 수 있습니다. 1) CGLIB가 클래스 경로에 있는지 확인하십시오. 2) proxy-target-class를 켜거나, 서비스 인터페이스를 없애 CGLIB 프록시를 사용하도록 강요하십시오.

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

    이 작업을 수행하려는 경우 첫 번째 링크를 통해 읽었는지 확인하십시오. :).

  3. ==============================

    3.Spring 문서 (10.5.6.1 절 참조)에 따르면 Spring 프레임 워크 트랜잭션은 RunTimeException 만 롤백됩니다. SqlException과 같은 다른 확인 된 항목에는 해당되지 않습니다. 따라서이 예외에 대한 롤백을 원할 경우 아래와 같이 지정해야합니다.

    Spring 문서 (10.5.6.1 절 참조)에 따르면 Spring 프레임 워크 트랜잭션은 RunTimeException 만 롤백됩니다. SqlException과 같은 다른 확인 된 항목에는 해당되지 않습니다. 따라서이 예외에 대한 롤백을 원할 경우 아래와 같이 지정해야합니다.

    @Transactional(propagation=Propagation.REQUIRES_NEW,rollbackFor=SQLException.class)
    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);
    }
    

    시도해보고 작동하는지 알려주세요. 또한 이것은 더 많은 도움이 될 수 있습니다.

  4. from https://stackoverflow.com/questions/15795985/spring-transaction-propagation-required-requires-new by cc-by-sa and MIT license