#13 spring transaction : propagation - recover(require_new)
This commit is contained in:
@@ -3,6 +3,7 @@ package com.example.springtransaction.propagation;
|
|||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.stereotype.Repository;
|
import org.springframework.stereotype.Repository;
|
||||||
|
import org.springframework.transaction.annotation.Propagation;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
import javax.persistence.EntityManager;
|
import javax.persistence.EntityManager;
|
||||||
@@ -15,7 +16,7 @@ public class LogRepository {
|
|||||||
|
|
||||||
private final EntityManager em;
|
private final EntityManager em;
|
||||||
|
|
||||||
@Transactional
|
@Transactional(propagation = Propagation.REQUIRES_NEW)
|
||||||
public void save(Log logMessage) {
|
public void save(Log logMessage) {
|
||||||
log.info("log 저장");
|
log.info("log 저장");
|
||||||
em.persist(logMessage);
|
em.persist(logMessage);
|
||||||
|
|||||||
@@ -27,6 +27,7 @@ public class MemberService {
|
|||||||
log.info("== logRepository 호출 종료 ==");
|
log.info("== logRepository 호출 종료 ==");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
public void joinV2(String username) {
|
public void joinV2(String username) {
|
||||||
Member member = new Member(username);
|
Member member = new Member(username);
|
||||||
Log logMessage = new Log(username);
|
Log logMessage = new Log(username);
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import lombok.extern.slf4j.Slf4j;
|
|||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.boot.test.context.SpringBootTest;
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
import org.springframework.transaction.UnexpectedRollbackException;
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
@@ -110,4 +111,40 @@ class MemberServiceTest {
|
|||||||
assertTrue(memberRepository.find(username).isEmpty());
|
assertTrue(memberRepository.find(username).isEmpty());
|
||||||
assertTrue(logRepository.find(username).isEmpty());
|
assertTrue(logRepository.find(username).isEmpty());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* memberService @Transactional:ON
|
||||||
|
* memberRepository @Transactional:ON
|
||||||
|
* logRepository @Transactional:ON Exception
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
void recoverException_fail() {
|
||||||
|
//given
|
||||||
|
String username = "로그예외_recoverException_fail";
|
||||||
|
|
||||||
|
//when
|
||||||
|
assertThrows(UnexpectedRollbackException.class, () -> memberService.joinV2(username));
|
||||||
|
|
||||||
|
//then
|
||||||
|
assertTrue(memberRepository.find(username).isEmpty());
|
||||||
|
assertTrue(logRepository.find(username).isEmpty());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* memberService @Transactional:ON
|
||||||
|
* memberRepository @Transactional:ON
|
||||||
|
* logRepository @Transactional:ON(REQUIRES_NEW) Exception
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
void recoverException_success() {
|
||||||
|
//given
|
||||||
|
String username = "로그예외_recoverException_success";
|
||||||
|
|
||||||
|
//when
|
||||||
|
memberService.joinV2(username);
|
||||||
|
|
||||||
|
//then
|
||||||
|
assertTrue(memberRepository.find(username).isPresent());
|
||||||
|
assertTrue(logRepository.find(username).isEmpty());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user