• 카테고리

    질문 & 답변
  • 세부 분야

    백엔드

  • 해결 여부

    미해결

Controller 안에서 @Transaction 설정이 다른 2개의 method 호출

24.02.02 14:22 작성 조회수 106

0

@Slf4j
@RestController
public class AController {

  @Autowired AService aService;

  @Autowired BService bService;

  @PostMapping("/api/v2/aaaa")
  public ResponseEntity<ResponseDto> postA(
      @RequestBody @Valid PostADto postADto, HttpServletRequest request) throws Exception {
    A a = aService.getA(request);
    HashMap<String, Object> result = bService.createB(postADto, a);
    ....
  }

위 와 같이 컨트롤러에서 aService.getA(request); 와 bService.createB(postADto, user); 메서드를 호출합니다.

각 메서드는 아래와 같이 선언돼있습니다.

@Transactional(readOnly = true)
public A getA(HttpServletRequest request) {

  ....
  return aRepository.findById(id).orElse(null);
}
@Transactional
public HashMap<String, Object> createB(PostADto postADto, A a) {
   ......
   bRepository.save(postADto.toB());
   ......
   return ...;
}

getA 메서드안에서 TransactionSynchronizationManager.isCurrentTransactionReadOnly(); log 출력했을때 readonly = true로 나오고 readDB로 잘 연결됩니다.

 

하지만 createB 메서드안에서 TransactionSynchronizationManager.isCurrentTransactionReadOnly(); log 출력했을때 readonly = false로 나오는데
실제로는 readDB로 연결되고 query를 발생시켜 아래와 같은 오류가 발생합니다.

Caused by: java.sql.SQLException: The MySQL server is running with the --read-only option so it cannot execute this statement
	at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:129)
	at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97)
Caused by: java.sql.SQLException: The MySQL server is running with the --read-only option so it cannot execute this statement

 

컨트롤러에 @transactional 을 선언하지 않았기 때문에 각 메서드에서 트랜잭션이 수행되어 getA 메서드에서는 readDB로 createB 메서드에서는 writeDB로 요청이 된다고 알고 있었는데 그런 방식으로 동작이 안되 혼란스럽습니다.

 

어떤 이유로 이런 문제가 발생하는지 궁금합니다.

그리고 왜 이렇게 동작하는지 어느 부분을 학습하면 좋은지 궁금합니다.

답변 1

답변을 작성해보세요.

1

안녕하세요 heekyum kim님

데이터베이스가 read, write로 분리되어 있는 상황이군요.

이 부분에 대해서는 직접 도움을 드리기 어렵습니다.

대신에 데이터베이스 연결 설정, 데이터베이스 드라이버 버전 등을 확인해보시는 것을 권장드립니다.

감사합니다.