• 카테고리

    질문 & 답변
  • 세부 분야

    백엔드

  • 해결 여부

    미해결

트랜잭션 템플릿 사용 로직

22.10.02 19:40 작성 조회수 477

4

txTemplate.executeWithoutResult((status) -> {

try {

//비즈니스 로직

bizLogic(fromId, toId, money);

} catch (SQLException e) {

throw new IllegalStateException(e);

} });

위의 로직에서 파라미터로 status를 받는데

이 status는 어디에서 받아오는건가요?

이전 MemberServiceV3_1 에서는

//트랜잭션 시작

TransactionStatus status = transactionManager.getTransaction(new DefaultTransactionDefinition());

이렇게 status를 받아와서 커밋과 롤백에 status를 사용했는데

MemberServiceV3_2 에서는

여러번 반복해 돌려보아도 저 status 에 대한 설명이 없어서

답변 부탁드립니다.

 

 

 

 

답변 2

·

답변을 작성해보세요.

3

ZZAMBA님의 프로필

ZZAMBA

2023.01.29

status는 TransactionTemplate에서 생성해서 넘겨줍니다.


executeWithoutResult 함수는 execute 함수를 호출합니다.

TransactionTemplate에는 execute 함수가 구현되어 있고 간략히는 아래와 같습니다.

@Override
@Nullable
public <T> T execute(TransactionCallback<T> action) throws TransactionException {
                /*
                 (중략)
                     */
      TransactionStatus status = this.transactionManager.getTransaction(this);
      T result;
      try {
         result = action.doInTransaction(status);
      }
      /* 
        (중략) 
             */
      this.transactionManager.commit(status);
      return result;
   }
}

즉, status는 TransactionTemplate에서 생성해서 doInTransaction의 매개인자로 넣어줍니다.

2

OMG님의 프로필

OMG

2022.10.02

안녕하세요. IGOR님, 공식 서포터즈 OMG입니다.
자바의 람다와 함수형 프로그래밍을 알고 계시면 이해에 도움이 되실텐데요

간단히 예를 들어 설명드리면,

메서드를 작성할 때

public int add(int first, int second) {
    return first + second;
}

매개변수의 이름은 메서드를 작성하는 사람이 의도를 갖고 지으면 되고, 어떻게 짓던 동작에는 영향을 주지 않는 것처럼 람다에서도 매개변수의 이름은 어떻게 짓던 상관 없습니다.

 

public int add(int one, int two) {
    return one + two;
}

영한님은 강의에서 통일성을 위해 status로 지은 것 같습니다.

함수형 프로그래밍과 Consumer를 학습하시면 해당 인자의 의미를 아시게 될거라 생각합니다.

image


감사합니다.