#13 spring transaction : propagation - singleTx
This commit is contained in:
@@ -15,7 +15,7 @@ public class LogRepository {
|
||||
|
||||
private final EntityManager em;
|
||||
|
||||
@Transactional
|
||||
// @Transactional
|
||||
public void save(Log logMessage) {
|
||||
log.info("log 저장");
|
||||
em.persist(logMessage);
|
||||
|
||||
@@ -15,7 +15,7 @@ public class MemberRepository {
|
||||
|
||||
private final EntityManager em;
|
||||
|
||||
@Transactional
|
||||
// @Transactional
|
||||
public void save(Member member) {
|
||||
log.info("member 저장");
|
||||
em.persist(member);
|
||||
|
||||
@@ -3,6 +3,7 @@ package com.example.springtransaction.propagation;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
@Slf4j
|
||||
@Service
|
||||
@@ -12,6 +13,7 @@ public class MemberService {
|
||||
private final MemberRepository memberRepository;
|
||||
private final LogRepository logRepository;
|
||||
|
||||
@Transactional
|
||||
public void joinV1(String username) {
|
||||
Member member = new Member(username);
|
||||
Log logMessage = new Log(username);
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
package com.example.springtransaction.propagation;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.assertj.core.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
@Slf4j
|
||||
@SpringBootTest
|
||||
@@ -57,4 +57,21 @@ class MemberServiceTest {
|
||||
assertTrue(logRepository.find(username).isEmpty());
|
||||
}
|
||||
|
||||
/**
|
||||
* memberService @Transactional:ON
|
||||
* memberRepository @Transactional:OFF
|
||||
* logRepository @Transactional:OFF
|
||||
*/
|
||||
@Test
|
||||
void singleTx() {
|
||||
//given
|
||||
String username = "singleTx_success";
|
||||
|
||||
//when
|
||||
memberService.joinV1(username);
|
||||
|
||||
//then
|
||||
assertTrue(memberRepository.find(username).isPresent());
|
||||
assertTrue(logRepository.find(username).isPresent());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user