Skip to content

Commit

Permalink
[Fix/#230] ArticleViewCountDao Jooq 의존성 수정 (#231)
Browse files Browse the repository at this point in the history
* fix: Jooq 의존성 수정

* fix: JooQ SQL execute 부재 처리

* chore: 주석 추가 (Async 사용시 트랜잭션 분리에 대한 주의 문구)

* refactor: try~catch -> runCatching~onFailure 사용하도록 변경
  • Loading branch information
hun-ca authored Jul 21, 2024
1 parent 28d00e8 commit 4d5a4bd
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package com.few.api.repo.dao.article

import com.few.api.repo.dao.article.command.ArticleViewCountCommand
import com.few.api.repo.dao.article.query.ArticleViewCountQuery
import jooq.jooq_dsl.Tables.ARTICLE_VIEW_COUNT
import jooq.jooq_dsl.tables.ArticleViewCount.ARTICLE_VIEW_COUNT
import org.jooq.DSLContext
import org.springframework.stereotype.Repository

Expand All @@ -18,6 +18,7 @@ class ArticleViewCountDao(
.set(ARTICLE_VIEW_COUNT.CATEGORY_CD, query.categoryType.code)
.onDuplicateKeyUpdate()
.set(ARTICLE_VIEW_COUNT.VIEW_COUNT, ARTICLE_VIEW_COUNT.VIEW_COUNT.plus(1))
.execute()
}

fun selectArticleViewCount(command: ArticleViewCountCommand): Long? {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,16 @@ class ArticleViewHisAsyncHandler(
@Async(value = DATABASE_ACCESS_POOL)
@Transactional
fun addArticleViewHis(articleId: Long, memberId: Long, categoryType: CategoryType) {
try {
articleViewHisDao.insertArticleViewHis(ArticleViewHisCommand(articleId, memberId))
log.debug { "Successfully inserted article view history for articleId: $articleId and memberId: $memberId" }
runCatching {
articleViewHisDao.insertArticleViewHis(ArticleViewHisCommand(articleId, memberId)).also {
log.debug { "Successfully inserted article view history for articleId: $articleId and memberId: $memberId" }
}

articleViewCountDao.upsertArticleViewCount(ArticleViewCountQuery(articleId, categoryType))
log.debug { "Successfully upserted article view count for articleId: $articleId and memberId: $memberId" }
} catch (e: Exception) {
log.error {
articleViewCountDao.upsertArticleViewCount(ArticleViewCountQuery(articleId, categoryType)).also {
log.debug { "Successfully upserted article view count for articleId: $articleId and categoryType: $categoryType" }
}
}.onFailure { e ->
log.error(e) {
"Failed insertion article view history and upsertion article view count " +
"for articleId: $articleId and memberId: $memberId"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ class ReadArticleUseCase(
CategoryType.fromCode(articleRecord.category) ?: throw NotFoundException("article.invalid.category")
)

/**
* NOTE: The articleViewHisAsyncHandler creates a new transaction that is separate from the current context.
* So this section, the logic after the articleViewHisAsyncHandler call,
* is where the mismatch between the two transactions can occur if an exception is thrown.
*/

return ReadArticleUseCaseOut(
id = articleRecord.articleId,
writer = WriterDetail(
Expand Down

0 comments on commit 4d5a4bd

Please sign in to comment.