Skip to content

Commit

Permalink
[refactor/#49] 기존 서비스에 transactional 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
juuuuone committed Jan 17, 2025
1 parent 6ee2b03 commit be54661
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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<Long> 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<Long> 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<Long> query = entityManager.createQuery(jpql, Long.class);
query.setParameter("member", member);
Expand All @@ -56,3 +59,4 @@ private Long countFollowingByMember(Member member) {




Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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<Member> 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<Boolean> query = entityManager.createQuery(jpql, Boolean.class);
Expand All @@ -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<Member> query = entityManager.createQuery(jpql, Member.class);
Expand All @@ -62,5 +68,4 @@ public Member findMemberByClokeyId(String clokeyId) {
.findFirst()
.orElseThrow(() -> new IllegalArgumentException("클로키 아이디에 해당하는 사용자가 없습니다."));
}

}
}

0 comments on commit be54661

Please sign in to comment.