Skip to content

Commit 4b19483

Browse files
committed
Use separate transactions for implementations of Inbox and Transactional outbox patterns
This simplifies transaction management and error handling
1 parent b1c5b28 commit 4b19483

File tree

12 files changed

+23
-19
lines changed

12 files changed

+23
-19
lines changed

book-service/src/main/kotlin/com/romankudryashov/eventdrivenarchitecture/bookservice/exception/Exceptions.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.romankudryashov.eventdrivenarchitecture.bookservice.exception
22

3-
open class BookServiceException(message: String) : Exception(message)
3+
open class BookServiceException(message: String) : RuntimeException(message)
44

55
class AccessRestrictedException : BookServiceException("Access to entity is restricted")
66

book-service/src/main/kotlin/com/romankudryashov/eventdrivenarchitecture/bookservice/service/impl/AuthorServiceImpl.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class AuthorServiceImpl(
3535

3636
override fun getEntityById(id: Long): AuthorEntity? = authorRepository.findByIdOrNull(id)
3737

38-
@Transactional(propagation = Propagation.REQUIRES_NEW)
38+
@Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = [Exception::class])
3939
override fun update(id: Long, author: AuthorToSave): AuthorDto {
4040
log.debug("Start update an author: id={}, new state={}", id, author)
4141

book-service/src/main/kotlin/com/romankudryashov/eventdrivenarchitecture/bookservice/service/impl/BookServiceImpl.kt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ class BookServiceImpl(
5050
bookEntityToDtoConverter.convert(entity)
5151
}
5252

53-
@Transactional(propagation = Propagation.REQUIRES_NEW)
53+
@Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = [Exception::class])
5454
override fun create(book: BookToSave): BookDto {
5555
log.debug("Start creating a book: {}", book)
5656

@@ -63,7 +63,7 @@ class BookServiceImpl(
6363
return bookEntityToDtoConverter.convert(createdBook)
6464
}
6565

66-
@Transactional(propagation = Propagation.REQUIRES_NEW)
66+
@Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = [Exception::class])
6767
override fun update(id: Long, book: BookToSave): BookDto {
6868
log.debug("Start updating a book: id={}, new state={}", id, book)
6969

@@ -78,7 +78,7 @@ class BookServiceImpl(
7878
return bookEntityToDtoConverter.convert(updatedBook)
7979
}
8080

81-
@Transactional(propagation = Propagation.REQUIRES_NEW)
81+
@Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = [Exception::class])
8282
override fun delete(id: Long) {
8383
log.debug("Start deleting a book: id={}", id)
8484

@@ -94,7 +94,7 @@ class BookServiceImpl(
9494
}
9595
}
9696

97-
@Transactional(propagation = Propagation.REQUIRES_NEW)
97+
@Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = [Exception::class])
9898
override fun lendBook(bookId: Long, bookLoan: BookLoanToSave): BookLoanDto {
9999
log.debug("Start lending a book: bookId={}, bookLoan={}", bookId, bookLoan)
100100

@@ -116,7 +116,7 @@ class BookServiceImpl(
116116
return bookLoanEntityToDtoConverter.convert(bookToLend.currentLoan()!!)
117117
}
118118

119-
@Transactional(propagation = Propagation.MANDATORY, noRollbackFor = [RuntimeException::class])
119+
@Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = [Exception::class])
120120
override fun cancelBookLoan(bookId: Long, bookLoanId: Long) {
121121
log.debug("Start cancelling book loan for a book: bookId={}, bookLoanId={}", bookId, bookLoanId)
122122

@@ -131,7 +131,7 @@ class BookServiceImpl(
131131
outboxMessageService.saveBookLoanCanceledEventMessage(modelOfBookToCancelLoan)
132132
}
133133

134-
@Transactional(propagation = Propagation.REQUIRES_NEW)
134+
@Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = [Exception::class])
135135
override fun returnBook(bookId: Long, bookLoanId: Long) {
136136
log.debug("Start returning a book: bookId={}, bookLoanId={}", bookId, bookLoanId)
137137

book-service/src/main/kotlin/com/romankudryashov/eventdrivenarchitecture/bookservice/service/impl/InboxMessageServiceImpl.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class InboxMessageServiceImpl(
2222

2323
private val log = LoggerFactory.getLogger(this.javaClass)
2424

25-
@Transactional(propagation = Propagation.REQUIRES_NEW)
25+
@Transactional(propagation = Propagation.REQUIRES_NEW, noRollbackFor = [RuntimeException::class])
2626
override fun markInboxMessagesAsReadyForProcessingByInstance(batchSize: Int): Int {
2727
fun saveReadyForProcessing(inboxMessage: InboxMessageEntity) {
2828
log.debug("Start saving a message ready for processing, id={}", inboxMessage.id)
@@ -46,7 +46,7 @@ class InboxMessageServiceImpl(
4646
override fun getBatchForProcessing(batchSize: Int): List<InboxMessageEntity> =
4747
inboxMessageRepository.findAllByStatusAndProcessedByOrderByCreatedAtAsc(InboxMessageEntity.Status.ReadyForProcessing, applicationName, PageRequest.of(0, batchSize))
4848

49-
@Transactional(propagation = Propagation.REQUIRES_NEW)
49+
@Transactional(propagation = Propagation.REQUIRES_NEW, noRollbackFor = [RuntimeException::class])
5050
override fun process(inboxMessage: InboxMessageEntity) {
5151
log.debug("Start processing an inbox message with id={}", inboxMessage.id)
5252

book-service/src/main/kotlin/com/romankudryashov/eventdrivenarchitecture/bookservice/service/impl/IncomingEventServiceImpl.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class IncomingEventServiceImpl(
2222

2323
private val log = LoggerFactory.getLogger(this.javaClass)
2424

25-
@Transactional(propagation = Propagation.MANDATORY, noRollbackFor = [RuntimeException::class])
25+
@Transactional(propagation = Propagation.NOT_SUPPORTED)
2626
override fun process(eventType: EventType, payload: JsonNode) {
2727
log.debug("Start processing an incoming event: type={}, payload={}", eventType, payload)
2828

Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
package com.romankudryashov.eventdrivenarchitecture.notificationservice.exception
22

3-
class NotificationServiceException(message: String) : Exception(message)
3+
class NotificationServiceException(message: String) : RuntimeException(message)

notification-service/src/main/kotlin/com/romankudryashov/eventdrivenarchitecture/notificationservice/service/impl/InboxMessageServiceImpl.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class InboxMessageServiceImpl(
2222

2323
private val log = LoggerFactory.getLogger(this.javaClass)
2424

25-
@Transactional(propagation = Propagation.REQUIRES_NEW)
25+
@Transactional(propagation = Propagation.REQUIRES_NEW, noRollbackFor = [RuntimeException::class])
2626
override fun markInboxMessagesAsReadyForProcessingByInstance(batchSize: Int): Int {
2727
fun saveReadyForProcessing(inboxMessage: InboxMessageEntity) {
2828
log.debug("Start saving a message ready for processing, id={}", inboxMessage.id)
@@ -46,7 +46,7 @@ class InboxMessageServiceImpl(
4646
override fun getBatchForProcessing(batchSize: Int): List<InboxMessageEntity> =
4747
inboxMessageRepository.findAllByStatusAndProcessedByOrderByCreatedAtAsc(InboxMessageEntity.Status.ReadyForProcessing, applicationName, PageRequest.of(0, batchSize))
4848

49-
@Transactional(propagation = Propagation.REQUIRES_NEW)
49+
@Transactional(propagation = Propagation.REQUIRES_NEW, noRollbackFor = [RuntimeException::class])
5050
override fun process(inboxMessage: InboxMessageEntity) {
5151
log.debug("Start processing an inbox message with id={}", inboxMessage.id)
5252

notification-service/src/main/kotlin/com/romankudryashov/eventdrivenarchitecture/notificationservice/service/impl/IncomingEventServiceImpl.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class IncomingEventServiceImpl(
2222

2323
private val log = LoggerFactory.getLogger(this.javaClass)
2424

25-
@Transactional(propagation = Propagation.MANDATORY, noRollbackFor = [RuntimeException::class])
25+
@Transactional(propagation = Propagation.NOT_SUPPORTED)
2626
override fun process(eventType: EventType, payload: JsonNode) {
2727
log.debug("Start processing an incoming event: type={}, payload={}", eventType, payload)
2828

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
package com.romankudryashov.eventdrivenarchitecture.userservice.exception
22

3-
class UserServiceException(message: String) : Exception(message)
3+
class UserServiceException(message: String) : RuntimeException(message)

user-service/src/main/kotlin/com/romankudryashov/eventdrivenarchitecture/userservice/service/impl/InboxMessageServiceImpl.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class InboxMessageServiceImpl(
2222

2323
private val log = LoggerFactory.getLogger(this.javaClass)
2424

25-
@Transactional(propagation = Propagation.REQUIRES_NEW)
25+
@Transactional(propagation = Propagation.REQUIRES_NEW, noRollbackFor = [RuntimeException::class])
2626
override fun markInboxMessagesAsReadyForProcessingByInstance(batchSize: Int): Int {
2727
fun saveReadyForProcessing(inboxMessage: InboxMessageEntity) {
2828
log.debug("Start saving a message ready for processing, id={}", inboxMessage.id)
@@ -46,7 +46,7 @@ class InboxMessageServiceImpl(
4646
override fun getBatchForProcessing(batchSize: Int): List<InboxMessageEntity> =
4747
inboxMessageRepository.findAllByStatusAndProcessedByOrderByCreatedAtAsc(InboxMessageEntity.Status.ReadyForProcessing, applicationName, PageRequest.of(0, batchSize))
4848

49-
@Transactional(propagation = Propagation.REQUIRES_NEW)
49+
@Transactional(propagation = Propagation.REQUIRES_NEW, noRollbackFor = [RuntimeException::class])
5050
override fun process(inboxMessage: InboxMessageEntity) {
5151
log.debug("Start processing an inbox message with id={}", inboxMessage.id)
5252

0 commit comments

Comments
 (0)