Skip to content

Commit

Permalink
[Feat/#450] v2 jpa 설정 추가 (#461)
Browse files Browse the repository at this point in the history
* refactor: 기존 설정 빈 DataSource 기반임을 나타낼 수 있도록 구체적으로 수정

* feat: DataSourceTransactional 어노테이션 추가

* refactor: DataSourceTransactional 어노테이션 적용

* feat: JPA 관련 allOpen 설정 추가

* feat: Jpa 의존성 추가

* feat: Jpa 필요 프로퍼티 추가

* feat: JpaConfig 구현

* feat: Jpa 추가로 중복되는 빈 설정 문제 해결

* feat: Jpa 편의 어노테이션 추가

- EnableJpaRepositories
- EntityTimeJsonFormat
- JpaTransactional

* feat: ApiJpaConfig 구현

* refactor: V2에는 domain에 따라 dataMigration 파일 필터링 할 수 있도록 수정

* feat: BaseEntity 구현

* refactor: TxConfig로 TransactionalManager 설정 분리

* refactor: TxConfig로 TransactionalManager 설정 분리 반영

* refactor: TxConfig에서 TransactionManagementConfigurer를 구현하여 JpaTx를 지정하여 삭제
  • Loading branch information
belljun3395 authored Dec 22, 2024
1 parent af709c0 commit 9df6081
Show file tree
Hide file tree
Showing 49 changed files with 379 additions and 89 deletions.
8 changes: 8 additions & 0 deletions api/src/main/kotlin/com/few/api/config/ApiJpaConfig.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.few.api.config

import org.springframework.context.annotation.Configuration
import repo.jpa.EnableJpaRepositories

@Configuration
@EnableJpaRepositories(basePackages = [ApiConfig.BASE_PACKAGE])
class ApiJpaConfig
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import com.few.api.domain.problem.repo.command.InsertProblemsCommand
import com.few.api.domain.problem.repo.support.Content
import com.few.api.domain.problem.repo.support.Contents
import org.springframework.stereotype.Component
import org.springframework.transaction.annotation.Transactional
import repo.jooq.DataSourceTransactional
import storage.document.PutDocumentProvider
import java.io.File
import java.time.LocalDateTime
Expand All @@ -41,7 +41,7 @@ class AddArticleUseCase(
private val getUrlService: GetUrlService,
private val adminArticleMainCardService: AdminArticleMainCardService,
) {
@Transactional
@DataSourceTransactional
fun execute(useCaseIn: AddArticleUseCaseIn): AddArticleUseCaseOut {
val writerRecord =
memberDao.selectMemberByEmail(SelectMemberByEmailQuery(useCaseIn.writerEmail))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ import com.few.api.domain.common.exception.InsertException
import com.few.api.domain.workbook.repo.WorkbookDao
import com.few.api.domain.workbook.repo.command.InsertWorkBookCommand
import org.springframework.stereotype.Component
import org.springframework.transaction.annotation.Transactional
import repo.jooq.DataSourceTransactional

@Component
class AddWorkbookUseCase(
private val workbookDao: WorkbookDao,
) {
@Transactional
@DataSourceTransactional
fun execute(useCaseIn: AddWorkbookUseCaseIn): AddWorkbookUseCaseOut {
val workbookId =
workbookDao.insertWorkBook(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import com.few.api.domain.admin.utils.ObjectPathGenerator
import com.few.api.domain.common.exception.ExternalIntegrationException
import com.few.api.domain.common.exception.InsertException
import org.springframework.stereotype.Component
import org.springframework.transaction.annotation.Transactional
import repo.jooq.DataSourceTransactional
import storage.document.PutDocumentProvider
import java.io.File

Expand All @@ -21,7 +21,7 @@ class ConvertContentUseCase(
private val putDocumentService: PutDocumentProvider,
private val getUrlService: GetUrlService,
) {
@Transactional
@DataSourceTransactional
fun execute(useCaseIn: ConvertContentUseCaseIn): ConvertContentUseCaseOut {
val contentSource = useCaseIn.content

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ import com.few.api.domain.admin.usecase.dto.MapArticleUseCaseIn
import com.few.api.domain.workbook.repo.WorkbookDao
import com.few.api.domain.workbook.repo.command.MapWorkBookToArticleCommand
import org.springframework.stereotype.Component
import org.springframework.transaction.annotation.Transactional
import repo.jooq.DataSourceTransactional

@Component
class MapArticleUseCase(
private val workbookDao: WorkbookDao,
private val adminArticleMainCardService: AdminArticleMainCardService,
) {
@Transactional
@DataSourceTransactional
fun execute(useCaseIn: MapArticleUseCaseIn) {
workbookDao.mapWorkBookToArticle(
MapWorkBookToArticleCommand(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import com.few.api.domain.common.exception.InsertException
import com.sksamuel.scrimage.ImmutableImage
import com.sksamuel.scrimage.webp.WebpWriter
import org.springframework.stereotype.Component
import org.springframework.transaction.annotation.Transactional
import repo.jooq.DataSourceTransactional
import storage.image.PutImageProvider
import java.io.File

Expand All @@ -22,7 +22,7 @@ class PutImageUseCase(
private val putImageService: PutImageProvider,
private val getUrlService: GetUrlService,
) {
@Transactional
@DataSourceTransactional
fun execute(useCaseIn: PutImageUseCaseIn): PutImageUseCaseOut {
val imageSource = useCaseIn.source
val suffix = imageSource.originalFilename?.substringAfterLast(".") ?: "jpg"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import com.few.api.domain.common.vo.CategoryType
import io.github.oshai.kotlinlogging.KotlinLogging
import org.springframework.scheduling.annotation.Async
import org.springframework.stereotype.Component
import org.springframework.transaction.annotation.Transactional
import repo.jooq.DataSourceTransactional

@Component
class ArticleViewHisAsyncHandler(
Expand All @@ -19,7 +19,7 @@ class ArticleViewHisAsyncHandler(
private val log = KotlinLogging.logger {}

@Async(value = DATABASE_ACCESS_POOL)
@Transactional
@DataSourceTransactional
fun addArticleViewHis(
articleId: Long,
memberId: Long,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import com.few.api.domain.article.usecase.dto.*
import com.few.api.domain.common.exception.NotFoundException
import com.few.api.domain.common.vo.CategoryType
import org.springframework.stereotype.Component
import org.springframework.transaction.annotation.Transactional
import repo.jooq.DataSourceTransactional
import java.util.*
import kotlin.Comparator

Expand All @@ -22,7 +22,7 @@ class BrowseArticlesUseCase(
private val articleMainCardDao: ArticleMainCardDao,
private val articleDao: ArticleDao,
) {
@Transactional(readOnly = true)
@DataSourceTransactional(readOnly = true)
fun execute(useCaseIn: ReadArticlesUseCaseIn): ReadArticlesUseCaseOut {
/**
* 아티클 조회수 테이블에서 마지막 읽은 아티클 아이디, 카테고리를 기반으로 Offset(테이블 row 순위)을 구함
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ import com.few.api.domain.article.usecase.dto.ReadArticleByEmailUseCaseIn
import com.few.api.domain.common.exception.NotFoundException
import com.few.api.domain.common.vo.EmailLogEventType
import org.springframework.stereotype.Component
import org.springframework.transaction.annotation.Transactional
import repo.jooq.DataSourceTransactional

@Component
class ReadArticleByEmailUseCase(
private val memberService: ArticleMemberService,
private val articleLogService: ArticleLogService,
) {
@Transactional
@DataSourceTransactional
fun execute(useCaseIn: ReadArticleByEmailUseCaseIn) {
val memberId =
memberService.readMemberByEmail(ReadMemberByEmailDto(useCaseIn.destination[0]))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import com.few.api.domain.common.exception.NotFoundException
import com.few.api.domain.common.vo.CategoryType
import org.springframework.context.ApplicationEventPublisher
import org.springframework.stereotype.Component
import org.springframework.transaction.annotation.Transactional
import repo.jooq.DataSourceTransactional

@Component
class ReadArticleUseCase(
Expand All @@ -25,7 +25,7 @@ class ReadArticleUseCase(
private val articleViewCountTxCase: ArticleViewCountTxCase,
private val applicationEventPublisher: ApplicationEventPublisher,
) {
@Transactional(readOnly = true)
@DataSourceTransactional(readOnly = true)
fun execute(useCaseIn: ReadArticleUseCaseIn): ReadArticleUseCaseOut {
val articleRecord =
articleDao.selectArticleRecord(SelectArticleRecordQuery(useCaseIn.articleId))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ import com.few.api.domain.article.repo.command.ArticleViewCountCommand
import org.springframework.stereotype.Component
import org.springframework.transaction.annotation.Isolation
import org.springframework.transaction.annotation.Propagation
import org.springframework.transaction.annotation.Transactional
import repo.jooq.DataSourceTransactional

@Component
class ArticleViewCountTxCase(
private val articleViewCountDao: ArticleViewCountDao,
) {
@Transactional(isolation = Isolation.READ_UNCOMMITTED, propagation = Propagation.REQUIRES_NEW)
@DataSourceTransactional(isolation = Isolation.READ_UNCOMMITTED, propagation = Propagation.REQUIRES_NEW)
fun browseArticleViewCount(articleId: Long): Long =
(articleViewCountDao.selectArticleViewCount(ArticleViewCountCommand(articleId)) ?: 0L) + 1L
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import com.few.api.domain.batch.article.reader.WorkBookSubscriberReader
import com.few.api.domain.batch.article.writer.WorkBookSubscriberWriter
import com.few.api.domain.batch.log.ApiBatchCallExecutionService
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
import repo.jooq.DataSourceTransactional

@Service
class ApiBatchSendArticleEmailService(
Expand All @@ -14,7 +14,7 @@ class ApiBatchSendArticleEmailService(
private val batchCallExecutionService: ApiBatchCallExecutionService,
private val objectMapper: ObjectMapper,
) {
@Transactional
@DataSourceTransactional
fun execute() {
val startTime = System.currentTimeMillis()
workBookSubscriberReader.execute().let { item ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import org.jooq.Condition
import org.jooq.DSLContext
import org.jooq.TableField
import org.springframework.stereotype.Component
import org.springframework.transaction.annotation.Transactional
import repo.jooq.DataSourceTransactional
import java.time.DayOfWeek
import java.time.LocalDate
import java.time.LocalTime
Expand All @@ -19,7 +19,7 @@ class WorkBookSubscriberReader(
private val dslContext: DSLContext,
) {
/** 구독 테이블에서 학습지를 구독하고 있는 회원의 정보를 조회한다.*/
@Transactional(readOnly = true)
@DataSourceTransactional(readOnly = true)
fun execute(): List<WorkBookSubscriberItem> {
val time = LocalTime.now(ZoneId.of("Asia/Seoul")).hour.let { LocalTime.of(it, 0, 0) }
val date = LocalDate.now(ZoneId.of("Asia/Seoul"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import org.jooq.DSLContext
import org.jooq.InsertSetMoreStep
import org.jooq.UpdateConditionStep
import org.springframework.stereotype.Component
import org.springframework.transaction.annotation.Transactional
import repo.jooq.DataSourceTransactional
import java.time.LocalDate
import java.time.LocalDateTime

Expand All @@ -35,7 +35,7 @@ class WorkBookSubscriberWriter(
/**
* 구독자들에게 이메일을 전송하고 진행률을 업데이트한다.
*/
@Transactional
@DataSourceTransactional
fun execute(items: List<WorkBookSubscriberItem>): Map<Any, Any> {
val memberIds = items.toMemberIds()
val targetWorkBookIds = items.toTargetWorkBookIds()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import com.few.api.domain.log.dto.AddApiLogUseCaseIn
import com.few.api.domain.log.repo.LogIfoDao
import com.few.api.domain.log.repo.command.InsertLogCommand
import org.springframework.stereotype.Component
import org.springframework.transaction.annotation.Transactional
import repo.jooq.DataSourceTransactional

@Component
class AddApiLogUseCase(
private val logIfoDao: LogIfoDao,
) {
@Transactional
@DataSourceTransactional
fun execute(useCaseIn: AddApiLogUseCaseIn) {
logIfoDao.insertLogIfo(InsertLogCommand(useCaseIn.history))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ import com.few.api.domain.log.repo.query.SelectEventByMessageIdAndEventTypeQuery
import com.few.api.domain.member.repo.MemberDao
import com.few.api.domain.member.repo.query.SelectMemberByEmailQuery
import org.springframework.stereotype.Component
import org.springframework.transaction.annotation.Transactional
import repo.jooq.DataSourceTransactional

@Component
class AddEmailLogUseCase(
private val memberDao: MemberDao,
private val sendArticleEventHistoryDao: SendArticleEventHistoryDao,
) {
@Transactional
@DataSourceTransactional
fun execute(useCaseIn: AddEmailLogUseCaseIn) {
val (memberId, _, _, _) =
memberDao.selectMemberByEmail(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import com.few.api.domain.member.service.dto.DeleteSubscriptionDto
import com.few.api.domain.subscription.repo.SubscriptionDao
import com.few.api.domain.subscription.repo.command.UpdateDeletedAtInAllSubscriptionCommand
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
import repo.jooq.DataSourceTransactional

@Service
class MemberSubscriptionService(
private val subscriptionDao: SubscriptionDao,
) {
@Transactional
@DataSourceTransactional
fun deleteSubscription(dto: DeleteSubscriptionDto) {
subscriptionDao.updateDeletedAtInAllSubscription(
UpdateDeletedAtInAllSubscriptionCommand(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ import com.few.api.domain.member.service.dto.DeleteSubscriptionDto
import com.few.api.domain.member.usecase.dto.DeleteMemberUseCaseIn
import com.few.api.domain.member.usecase.dto.DeleteMemberUseCaseOut
import org.springframework.stereotype.Component
import org.springframework.transaction.annotation.Transactional
import repo.jooq.DataSourceTransactional

@Component
class DeleteMemberUseCase(
private val memberDao: MemberDao,
private val memberSubscriptionService: MemberSubscriptionService,
) {
@Transactional
@DataSourceTransactional
fun execute(useCaseIn: DeleteMemberUseCaseIn): DeleteMemberUseCaseOut {
memberDao.deleteMember(
DeleteMemberCommand(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import com.few.api.domain.member.usecase.dto.SaveMemberUseCaseIn
import com.few.api.domain.member.usecase.dto.SaveMemberUseCaseOut
import com.few.api.domain.member.usecase.transaction.SaveMemberTxCase
import org.springframework.stereotype.Component
import org.springframework.transaction.annotation.Transactional
import repo.jooq.DataSourceTransactional
import security.encryptor.IdEncryptor
import java.net.URL

Expand All @@ -21,7 +21,7 @@ class SaveMemberUseCase(
private val idEncryption: IdEncryptor,
private val saveMemberTxCase: SaveMemberTxCase,
) {
@Transactional
@DataSourceTransactional
fun execute(useCaseIn: SaveMemberUseCaseIn): SaveMemberUseCaseOut {
/** email을 통해 가입 이력이 있는지 확인 */
val (headComment, subComment, memberId) =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import com.few.api.domain.member.repo.command.UpdateMemberTypeCommand
import com.few.api.domain.member.usecase.dto.TokenUseCaseIn
import com.few.api.domain.member.usecase.dto.TokenUseCaseOut
import org.springframework.stereotype.Component
import org.springframework.transaction.annotation.Transactional
import repo.jooq.DataSourceTransactional
import security.Roles
import security.TokenGenerator
import security.TokenResolver
Expand All @@ -22,7 +22,7 @@ class TokenUseCase(
private val memberDao: MemberDao,
private val idEncryption: IdEncryptor,
) {
@Transactional
@DataSourceTransactional
fun execute(useCaseIn: TokenUseCaseIn): TokenUseCaseOut {
var isLogin = true

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import com.few.api.domain.member.repo.record.MemberIdAndIsDeletedRecord
import com.few.api.domain.member.usecase.dto.SaveMemberTxCaseIn
import com.few.api.domain.member.usecase.dto.SaveMemberTxCaseOut
import org.springframework.stereotype.Component
import org.springframework.transaction.annotation.Transactional
import repo.jooq.DataSourceTransactional

@Component
class SaveMemberTxCase(
Expand All @@ -22,7 +22,7 @@ class SaveMemberTxCase(
private const val SIGNUP_SUB_COMMENT = "가입하신 이메일 주소를 확인해주세요."
}

@Transactional
@DataSourceTransactional
fun execute(dto: SaveMemberTxCaseIn): SaveMemberTxCaseOut =
dto.record?.let { signUpBeforeMember ->
signUpBeforeMember.takeIf { it.isDeleted }?.let {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ import com.few.api.domain.problem.repo.query.SelectProblemsByArticleIdQuery
import com.few.api.domain.problem.usecase.dto.BrowseProblemsUseCaseIn
import com.few.api.domain.problem.usecase.dto.BrowseProblemsUseCaseOut
import org.springframework.stereotype.Component
import org.springframework.transaction.annotation.Transactional
import repo.jooq.DataSourceTransactional

@Component
class BrowseProblemsUseCase(
private val problemDao: ProblemDao,
) {
@Transactional(readOnly = true)
@DataSourceTransactional(readOnly = true)
fun execute(useCaseIn: BrowseProblemsUseCaseIn): BrowseProblemsUseCaseOut {
problemDao
.selectProblemsByArticleId(SelectProblemsByArticleIdQuery(useCaseIn.articleId))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import com.few.api.domain.problem.service.dto.BrowseWorkbookIdAndProgressInDto
import com.few.api.domain.problem.usecase.dto.BrowseProblemsUseCaseOut
import com.few.api.domain.problem.usecase.dto.BrowseUndoneProblemsUseCaseIn
import org.springframework.stereotype.Component
import org.springframework.transaction.annotation.Transactional
import repo.jooq.DataSourceTransactional

@Component
class BrowseUndoneProblemsUseCase(
Expand All @@ -21,7 +21,7 @@ class BrowseUndoneProblemsUseCase(
private val problemArticleService: ProblemArticleService,
private val submitHistoryDao: SubmitHistoryDao,
) {
@Transactional(readOnly = true)
@DataSourceTransactional(readOnly = true)
fun execute(useCaseIn: BrowseUndoneProblemsUseCaseIn): BrowseProblemsUseCaseOut {
/**
* 유저가 구독한 워크북들에 속한 아티클 개수를 조회함
Expand Down
Loading

0 comments on commit 9df6081

Please sign in to comment.