Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,11 @@ List<Integer> findUserIdsByNotifiedArticleIdAndUserIdIn(

@Modifying(flushAutomatically = true, clearAutomatically = true)
@Query(value = """
INSERT INTO user_notification_status (user_id, last_notified_article_id)
VALUES (:userId, :notifiedArticleId)
ON DUPLICATE KEY UPDATE last_notified_article_id = :notifiedArticleId
INSERT INTO user_notification_status (user_id, last_notified_article_id, created_at, updated_at)
VALUES (:userId, :notifiedArticleId, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)
ON DUPLICATE KEY UPDATE
last_notified_article_id = :notifiedArticleId,
updated_at = CURRENT_TIMESTAMP
""", nativeQuery = true)
void upsertLastNotifiedArticleId(
@Param("userId") Integer userId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import in.koreatech.koin.domain.community.article.exception.ArticleNotFoundException;
Expand Down Expand Up @@ -206,7 +207,7 @@ public void fetchTopKeywordsFromLastWeek() {
}
}

@Transactional
@Transactional(propagation = Propagation.REQUIRES_NEW)
public void createNotifiedArticleStatus(Integer userId, Integer articleId) {
userNotificationStatusRepository.upsertLastNotifiedArticleId(userId, articleId);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package in.koreatech.koin.unit.domain.community.keyword.service;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoInteractions;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.when;

import java.lang.reflect.Method;
import java.util.List;
import java.util.Map;

Expand All @@ -16,6 +18,8 @@
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import in.koreatech.koin.common.event.ArticleKeywordEvent;
import in.koreatech.koin.domain.community.article.model.Article;
Expand Down Expand Up @@ -107,4 +111,14 @@ void createNotifiedArticleStatus_usesAtomicUpsert() {

verify(userNotificationStatusRepository).upsertLastNotifiedArticleId(1, 100);
}

@Test
@DisplayName("발송 이력 저장은 항상 새로운 트랜잭션에서 수행한다.")
void createNotifiedArticleStatus_startsNewTransaction() throws NoSuchMethodException {
Method method = KeywordService.class.getMethod("createNotifiedArticleStatus", Integer.class, Integer.class);
Transactional transactional = method.getAnnotation(Transactional.class);

assertThat(transactional).isNotNull();
assertThat(transactional.propagation()).isEqualTo(Propagation.REQUIRES_NEW);
}
}
Loading