Skip to content

Commit

Permalink
fix : 락 제거
Browse files Browse the repository at this point in the history
- 현재 상황에선 오버 엔지니어링인 것으로 예상되어 락 제거
  • Loading branch information
seokho-1116 committed Jun 4, 2024
1 parent 2c7119d commit 041cac8
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 50 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package site.timecapsulearchive.core.domain.friend.entity;

import jakarta.persistence.CascadeType;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
Expand All @@ -26,11 +27,11 @@ public class FriendInvite extends BaseEntity {
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@ManyToOne(fetch = FetchType.LAZY)
@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.PERSIST)
@JoinColumn(name = "owner_id", nullable = false)
private Member owner;

@ManyToOne(fetch = FetchType.LAZY)
@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.PERSIST)
@JoinColumn(name = "friend_id", nullable = false)
private Member friend;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
package site.timecapsulearchive.core.domain.friend.repository.friend_invite;

import jakarta.persistence.LockModeType;
import jakarta.persistence.QueryHint;
import java.util.List;
import java.util.Optional;
import org.springframework.data.jpa.repository.Lock;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.jpa.repository.QueryHints;
import org.springframework.data.repository.Repository;
import org.springframework.data.repository.query.Param;
import site.timecapsulearchive.core.domain.friend.entity.FriendInvite;
Expand All @@ -18,8 +13,6 @@ public interface FriendInviteRepository extends Repository<FriendInvite, Long>,

void delete(FriendInvite friendInvite);

@Lock(LockModeType.PESSIMISTIC_WRITE)
@QueryHints({@QueryHint(name = "jakarta.persistence.lock.timeout", value = "3000")})
Optional<FriendInvite> findFriendSendingInviteForUpdateByOwnerIdAndFriendId(Long memberId,
Long friendId);

Expand All @@ -28,12 +21,10 @@ Optional<FriendInvite> findFriendSendingInviteForUpdateByOwnerIdAndFriendId(Long
from FriendInvite fi
join fetch fi.owner
join fetch fi.friend
where fi.owner.id =:friendId and fi.friend.id =:memberId
where fi.owner.id =:ownerId and fi.friend.id =:friendId
""")
@Lock(LockModeType.PESSIMISTIC_WRITE)
@QueryHints({@QueryHint(name = "jakarta.persistence.lock.timeout", value = "3000")})
Optional<FriendInvite> findFriendReceptionInviteForUpdateByOwnerIdAndFriendId(
@Param(value = "memberId") Long memberId,
Optional<FriendInvite> findFriendReceivingInviteForUpdateByOwnerIdAndFriendId(
@Param(value = "ownerId") Long ownerId,
@Param(value = "friendId") Long friendId
);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package site.timecapsulearchive.core.domain.friend.service.command;

import jakarta.persistence.OptimisticLockException;
import java.util.List;
import java.util.Optional;
import lombok.RequiredArgsConstructor;
Expand All @@ -21,7 +22,7 @@
import site.timecapsulearchive.core.domain.member.entity.Member;
import site.timecapsulearchive.core.domain.member.exception.MemberNotFoundException;
import site.timecapsulearchive.core.domain.member.repository.MemberRepository;
import site.timecapsulearchive.core.global.error.ErrorCode;
import site.timecapsulearchive.core.global.error.exception.InternalServerException;
import site.timecapsulearchive.core.infra.queue.manager.SocialNotificationManager;

@Slf4j
Expand Down Expand Up @@ -115,33 +116,34 @@ private void validateTwoWayAndDuplicateInvite(final Long memberId, final Long fr
}


public void acceptFriend(final Long memberId, final Long friendId) {
validateSelfFriendOperation(memberId, friendId);
public void acceptFriend(final Long memberId, final Long ownerId) {
validateSelfFriendOperation(memberId, ownerId);

final String ownerNickname = transactionTemplate.execute(status -> {
FriendInvite friendInvite = friendInviteRepository.findFriendReceptionInviteForUpdateByOwnerIdAndFriendId(
memberId, friendId)
FriendInvite friendInvite = friendInviteRepository.findFriendReceivingInviteForUpdateByOwnerIdAndFriendId(
ownerId, memberId)
.orElseThrow(FriendInviteNotFoundException::new);

final MemberFriend ownerRelation = friendInvite.ownerRelation();
final MemberFriend friendRelation = friendInvite.friendRelation();

memberFriendRepository.save(ownerRelation);
memberFriendRepository.save(friendRelation);

friendInviteRepository.delete(friendInvite);

return ownerRelation.getOwnerNickname();
});

socialNotificationManager.sendFriendAcceptMessage(ownerNickname, friendId);
socialNotificationManager.sendFriendAcceptMessage(ownerNickname, ownerId);
}

@Transactional
public void denyRequestFriend(final Long memberId, final Long friendId) {
validateSelfFriendOperation(memberId, friendId);
public void denyRequestFriend(final Long memberId, final Long ownerId) {
validateSelfFriendOperation(memberId, ownerId);

FriendInvite friendInvite = friendInviteRepository.findFriendReceptionInviteForUpdateByOwnerIdAndFriendId(
memberId, friendId)
FriendInvite friendInvite = friendInviteRepository.findFriendReceivingInviteForUpdateByOwnerIdAndFriendId(
ownerId, memberId)
.orElseThrow(FriendInviteNotFoundException::new);

friendInviteRepository.delete(friendInvite);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,18 @@
package site.timecapsulearchive.core.domain.member.entity;

import jakarta.persistence.CascadeType;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.EnumType;
import jakarta.persistence.Enumerated;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.OneToMany;
import jakarta.persistence.Table;
import jakarta.validation.constraints.Email;
import java.util.List;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import site.timecapsulearchive.core.domain.capsule.entity.Capsule;
import site.timecapsulearchive.core.domain.friend.entity.FriendInvite;
import site.timecapsulearchive.core.domain.friend.entity.MemberFriend;
import site.timecapsulearchive.core.domain.history.entity.History;
import site.timecapsulearchive.core.domain.member_group.entity.MemberGroup;
import site.timecapsulearchive.core.global.entity.BaseEntity;
import site.timecapsulearchive.core.global.util.NullCheck;
import site.timecapsulearchive.core.global.util.TagGenerator;
Expand Down Expand Up @@ -74,24 +66,6 @@ public class Member extends BaseEntity {
@Column(name = "tag", nullable = false, unique = true)
private String tag;

@OneToMany(mappedBy = "member", cascade = CascadeType.ALL, orphanRemoval = true)
private List<Capsule> capsules;

@OneToMany(mappedBy = "member", cascade = CascadeType.ALL, orphanRemoval = true)
private List<MemberGroup> groups;

@OneToMany(mappedBy = "friend", cascade = CascadeType.ALL, orphanRemoval = true)
private List<MemberFriend> friends;

@OneToMany(mappedBy = "owner", cascade = CascadeType.ALL, orphanRemoval = true)
private List<FriendInvite> friendsRequests;

@OneToMany(mappedBy = "friend", cascade = CascadeType.ALL, orphanRemoval = true)
private List<FriendInvite> notifications;

@OneToMany(mappedBy = "member", cascade = CascadeType.ALL, orphanRemoval = true)
private List<History> histories;

@Builder
private Member(String profileUrl, String nickname, SocialType socialType, String email,
String authId, String password, String tag, byte[] phone, byte[] phone_hash) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;
import site.timecapsulearchive.core.global.error.exception.BusinessException;
import site.timecapsulearchive.core.global.error.exception.InternalServerException;
import site.timecapsulearchive.core.global.error.exception.NullCheckValidateException;
import site.timecapsulearchive.core.infra.sms.exception.ExternalApiException;

Expand Down Expand Up @@ -47,6 +48,17 @@ protected ResponseEntity<ErrorResponse> handleBusinessException(final BusinessEx
.body(errorResponse);
}

@ExceptionHandler(InternalServerException.class)
protected ResponseEntity<ErrorResponse> handleInternalServerException(final InternalServerException e) {
log.error(e.getMessage(), e);

ErrorCode errorCode = INTERNAL_SERVER_ERROR;
final ErrorResponse errorResponse = ErrorResponse.fromErrorCode(errorCode);

return ResponseEntity.status(errorCode.getStatus())
.body(errorResponse);
}

@ExceptionHandler(MethodArgumentNotValidException.class)
protected ResponseEntity<ErrorResponse> handleRequestArgumentNotValidException(
MethodArgumentNotValidException e
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package site.timecapsulearchive.core.global.error.exception;

import site.timecapsulearchive.core.global.error.ErrorCode;

public class InternalServerException extends RuntimeException {

public InternalServerException(Throwable e) {
super(ErrorCode.INTERNAL_SERVER_ERROR.getMessage(), e);
}
}

0 comments on commit 041cac8

Please sign in to comment.