From be54661828244e4a7bec9392d531b3000c395ce2 Mon Sep 17 00:00:00 2001 From: paeng <127924700+juuuuone@users.noreply.github.com> Date: Fri, 17 Jan 2025 17:01:47 +0900 Subject: [PATCH] =?UTF-8?q?[refactor/#49]=20=EA=B8=B0=EC=A1=B4=20=EC=84=9C?= =?UTF-8?q?=EB=B9=84=EC=8A=A4=EC=97=90=20transactional=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../member/application/GetUserQueryServiceImpl.java | 12 ++++++++---- .../application/MemberRepositoryServiceImpl.java | 11 ++++++++--- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/clokey/server/domain/member/application/GetUserQueryServiceImpl.java b/src/main/java/com/clokey/server/domain/member/application/GetUserQueryServiceImpl.java index 6e9fa37..d4834c2 100644 --- a/src/main/java/com/clokey/server/domain/member/application/GetUserQueryServiceImpl.java +++ b/src/main/java/com/clokey/server/domain/member/application/GetUserQueryServiceImpl.java @@ -21,7 +21,7 @@ public class GetUserQueryServiceImpl implements GetUserQueryService { private EntityManager entityManager; @Override - @Transactional + @Transactional(readOnly = true) // 트랜잭션 읽기 전용으로 설정 public MemberDTO.GetUserRP getUser(String clokeyId) { Member member = memberRepositoryService.findMemberByClokeyId(clokeyId); @@ -32,21 +32,24 @@ public MemberDTO.GetUserRP getUser(String clokeyId) { return GetUserConverter.toGetUserResponseDTO(member, recordCount, followerCount, followingCount); } - private Long countHistoryByMember(Member member) { + @Transactional(readOnly = true) // 트랜잭션 읽기 전용으로 설정 + public Long countHistoryByMember(Member member) { String jpql = "SELECT COUNT(h) FROM History h WHERE h.member = :member"; TypedQuery query = entityManager.createQuery(jpql, Long.class); query.setParameter("member", member); return query.getSingleResult(); } - private Long countFollowersByMember(Member member) { + @Transactional(readOnly = true) // 트랜잭션 읽기 전용으로 설정 + public Long countFollowersByMember(Member member) { String jpql = "SELECT COUNT(f) FROM Follow f WHERE f.followed = :member"; TypedQuery query = entityManager.createQuery(jpql, Long.class); query.setParameter("member", member); return query.getSingleResult(); } - private Long countFollowingByMember(Member member) { + @Transactional(readOnly = true) // 트랜잭션 읽기 전용으로 설정 + public Long countFollowingByMember(Member member) { String jpql = "SELECT COUNT(f) FROM Follow f WHERE f.following = :member"; TypedQuery query = entityManager.createQuery(jpql, Long.class); query.setParameter("member", member); @@ -56,3 +59,4 @@ private Long countFollowingByMember(Member member) { + diff --git a/src/main/java/com/clokey/server/domain/member/application/MemberRepositoryServiceImpl.java b/src/main/java/com/clokey/server/domain/member/application/MemberRepositoryServiceImpl.java index 0401a63..b61006c 100644 --- a/src/main/java/com/clokey/server/domain/member/application/MemberRepositoryServiceImpl.java +++ b/src/main/java/com/clokey/server/domain/member/application/MemberRepositoryServiceImpl.java @@ -10,6 +10,7 @@ import jakarta.persistence.TypedQuery; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; import java.util.Optional; @@ -23,28 +24,32 @@ public class MemberRepositoryServiceImpl implements MemberRepositoryService { private EntityManager entityManager; @Override + @Transactional(readOnly = true) // 읽기 전용 트랜잭션 public boolean memberExist(Long memberId) { return memberRepository.existsById(memberId); } @Override + @Transactional(readOnly = true) // 읽기 전용 트랜잭션 public Optional getMember(Long memberId) { return memberRepository.findById(memberId); } - @Override + @Transactional(readOnly = true) // 읽기 전용 트랜잭션 public Member findMemberById(Long memberId) { return memberRepository.findById(memberId) .orElseThrow(() -> new MemberException(ErrorStatus.NO_SUCH_MEMBER)); } @Override + @Transactional // 쓰기 트랜잭션 public Member saveMember(Member member) { return memberRepository.save(member); } @Override + @Transactional(readOnly = true) // 읽기 전용 트랜잭션 public boolean idExist(String clokeyId) { String jpql = "SELECT COUNT(m) > 0 FROM Member m WHERE m.clokeyId = :clokeyId"; TypedQuery query = entityManager.createQuery(jpql, Boolean.class); @@ -53,6 +58,7 @@ public boolean idExist(String clokeyId) { } @Override + @Transactional(readOnly = true) // 읽기 전용 트랜잭션 public Member findMemberByClokeyId(String clokeyId) { String jpql = "SELECT m FROM Member m WHERE m.clokeyId = :clokeyId"; TypedQuery query = entityManager.createQuery(jpql, Member.class); @@ -62,5 +68,4 @@ public Member findMemberByClokeyId(String clokeyId) { .findFirst() .orElseThrow(() -> new IllegalArgumentException("클로키 아이디에 해당하는 사용자가 없습니다.")); } - -} \ No newline at end of file +}