org.springframework.dao.TransientDataAccessResourceException: Cannot change the ExecutorType when there is an existing transaction - 원인과 해결방법
스프링 부트 배치 개발 중에 org.springframework.dao.TransientDataAccessResourceException: Cannot change the ExecutorType when there is an existing transaction 예외가 발생했다.
MyBatisItemReader로 읽은 데이터를 ItemProcessor가 처리하면서 마이바티스 매퍼를 사용하고자 할 때 발생하는 것으로 추적되었다. 원인을 분석해보니, MyBatisItemReader는 트랜잭션을 생성할 때, ExecuteType.BATCH로 생성하는 반면, Mybatis의 트랜잭션 생성 기본 전략은 ExecuteType.SIMPLE 인 것이 문제였다. 동일한 트랜잭션에서 ExecuteType을 변경하여 쿼리를 호출할 수 없기 때문에 위 예외를 발생시킨 것이다.
ItemProcessor의 process 메서드 위에 @Transactional(propagation = Propagation.REQUIRES_NEW) 어노테이션을 추가하여, 강제로 트랜잭션을 분리 생성하도록 했다. 즉, MyBatisItemReader가 ExecuteType.BATCH로 생성한 트랜잭션이 전파되지 않도록 강제했다. 이렇게 하면 위 예외가 발생하지 않으면서 MyBatisItemReader가 전달한 데이터를 ItemProcessor에서 처리하되 마이바티스 매퍼를 사용해 처리할 수 있다.
*ItemProcessor.process(*Item item)
참고출처
"Transaction 내에서 ExecutorType 변경 불가 오류", tomining.tistory.com, 2017년 3월 15일 수정, 2023년 2월 17일 접속, https://tomining.tistory.com/178.
Transaction 내에서 ExecutorType 변경 불가 오류
Spring-Batch로 개발을 진행하던 중 아래와 같은 오류를 접하였다. org.springframework.dao.TransientDataAccessResourceException: Cannot change the ExecutorType when there is an existing transaction 현상은 "Transaction내에서 ExecutorT
tomining.tistory.com
"Spring Transaction 전파", mypiece 네이버 블로그, 2019년 11월 27일 수정, 2023년 2월 17일 접속, https://m.blog.naver.com/PostView.naver?isHttpsRedirect=true&blogId=vefe&logNo=221719503894.
Spring Transaction 전파
Spring에서는 Transaction이 걸려있는 메소드에서 Transaction 걸려있는 또 다른 메소드를 호출할 때 T...
blog.naver.com