Skip to content

Commit

Permalink
Merge pull request #368 from kookmin-sw/transaction
Browse files Browse the repository at this point in the history
  • Loading branch information
tmdtmdqorekf authored May 28, 2024
2 parents 447d59e + ec26ed0 commit c19fd01
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public class MatchService {
private static final String LOCK_KEY_PREFIX = "lock:senderId:";

// 매칭 요청
@Transactional
public MatchDto sendMatchRequest(MatchRequestDto dto) {
log.trace("sendMatchRequest()");

Expand Down Expand Up @@ -176,6 +177,7 @@ public List<MatchReceivedInfoDto> getMatchReceivedInfo(Long receiverId) {
}

// 매칭 요청 수락
@Transactional
public MatchAcceptResponse acceptMatchRequest(MatchIdDto dto) {
log.trace("acceptMatchRequest()");

Expand All @@ -194,9 +196,6 @@ public MatchAcceptResponse acceptMatchRequest(MatchIdDto dto) {
ChatroomCreationDto chatroomCreationDto = new ChatroomCreationDto(senderId, receiverId);
Long chatroomId = chatroomService.createChatroom(chatroomCreationDto);

redisTemplate.opsForHash().put(key, "status", "accepted");
redisTemplate.opsForHash().put("receiverId:" + receiverId + "-senderId:" + senderId, "status", "accepted");

// 알림
User fromUser = userRepository.findByUserId(receiverId).orElseThrow();
User toUser = userRepository.findByUserId(senderId).orElseThrow();
Expand All @@ -215,6 +214,11 @@ public MatchAcceptResponse acceptMatchRequest(MatchIdDto dto) {
redisTemplate.delete(LOCK_KEY_PREFIX + receiverId); // receiver 락 해제
}

// key -> receiverId:2-senderId:1
redisTemplate.opsForHash().put(key, "status", "accepted");
redisTemplate.opsForHash().put("receiverId:" + receiverId + "-senderId:" + senderId, "status", "accepted");

// key -> matchId:abcd-1234-abcd-1234-abcd-1234
redisTemplate.opsForHash().put("matchId:" + dto.getMatchId(), "status", "accepted");
redisTemplate.delete(LOCK_KEY_PREFIX + senderId); // sender 락 해제

Expand Down Expand Up @@ -243,6 +247,7 @@ private void validateIfAlreadyAccepted(String matchId) {
}

// 매칭 요청 거절
@Transactional
public MatchDto declineMatchRequest(MatchIdDto dto) {
log.trace("declineMatchRequest()");

Expand Down Expand Up @@ -277,6 +282,7 @@ public MatchDto declineMatchRequest(MatchIdDto dto) {
}

// 매칭 요청 취소
@Transactional
public MatchDto cancelMatchRequest(MatchIdDto dto) {
log.trace("cancelMatchRequest()");

Expand Down Expand Up @@ -353,6 +359,7 @@ private void validateRequest(String matchId) {
}

// 매칭 종료
@Transactional
public MatchStatusDto finishMatch(MatchFinishRequestDto dto) {
log.trace("finishMatch()");

Expand Down
10 changes: 10 additions & 0 deletions backend/src/main/java/com/coffee/backend/global/RedisConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,13 @@
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.repository.configuration.EnableRedisRepositories;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@Configuration
@EnableRedisRepositories
@EnableTransactionManagement
public class RedisConfig {
// application.yml 에서 host, port 값 가져오기
@Value("${spring.data.redis.host}")
Expand All @@ -27,6 +31,7 @@ public RedisConnectionFactory connectionFactory() {
public RedisTemplate<String, Object> redisTemplate() {
final RedisTemplate<String, Object> template = new RedisTemplate<>();
template.setConnectionFactory(connectionFactory());
template.setEnableTransactionSupport(true);

template.setKeySerializer(new StringRedisSerializer());
template.setValueSerializer(new StringRedisSerializer());
Expand All @@ -36,4 +41,9 @@ public RedisTemplate<String, Object> redisTemplate() {

return template;
}

@Bean
public PlatformTransactionManager transactionManager() {
return new JpaTransactionManager();
}
}

0 comments on commit c19fd01

Please sign in to comment.