Add support for Coroutines transactions
This commit adds Coroutines extensions for TransactionalOperator.transactional that accept suspending lambda or Kotlin Flow parameters. @Transactional on suspending functions is not supported yet, gh-23575 has been created for that purpose. Closes gh-22915
This commit is contained in:
@@ -0,0 +1,100 @@
|
||||
/*
|
||||
* Copyright 2002-2019 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.transaction.reactive
|
||||
|
||||
import kotlinx.coroutines.ExperimentalCoroutinesApi
|
||||
import kotlinx.coroutines.delay
|
||||
import kotlinx.coroutines.flow.flow
|
||||
import kotlinx.coroutines.flow.toList
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import org.assertj.core.api.Assertions.assertThat
|
||||
import org.junit.jupiter.api.Test
|
||||
import org.junit.jupiter.api.fail
|
||||
import org.springframework.transaction.support.DefaultTransactionDefinition
|
||||
|
||||
class TransactionalOperatorExtensionsTests {
|
||||
|
||||
private val tm = ReactiveTestTransactionManager(false, true)
|
||||
|
||||
@Test
|
||||
fun commitWithSuspendingFunction() {
|
||||
val operator = TransactionalOperator.create(tm, DefaultTransactionDefinition())
|
||||
runBlocking {
|
||||
operator.transactional {
|
||||
delay(1)
|
||||
true
|
||||
}
|
||||
}
|
||||
assertThat(tm.commit).isTrue()
|
||||
assertThat(tm.rollback).isFalse()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun rollbackWithSuspendingFunction() {
|
||||
val operator = TransactionalOperator.create(tm, DefaultTransactionDefinition())
|
||||
runBlocking {
|
||||
try {
|
||||
operator.transactional {
|
||||
delay(1)
|
||||
throw IllegalStateException()
|
||||
}
|
||||
} catch (ex: IllegalStateException) {
|
||||
assertThat(tm.commit).isFalse()
|
||||
assertThat(tm.rollback).isTrue()
|
||||
return@runBlocking
|
||||
}
|
||||
fail("")
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@ExperimentalCoroutinesApi
|
||||
fun commitWithFlow() {
|
||||
val operator = TransactionalOperator.create(tm, DefaultTransactionDefinition())
|
||||
val flow = flow {
|
||||
emit(1)
|
||||
emit(2)
|
||||
emit(3)
|
||||
emit(4)
|
||||
}
|
||||
runBlocking {
|
||||
val list = operator.transactional(flow).toList()
|
||||
assertThat(list).hasSize(4)
|
||||
}
|
||||
assertThat(tm.commit).isTrue()
|
||||
assertThat(tm.rollback).isFalse()
|
||||
}
|
||||
|
||||
@Test
|
||||
@ExperimentalCoroutinesApi
|
||||
fun rollbackWithFlow() {
|
||||
val operator = TransactionalOperator.create(tm, DefaultTransactionDefinition())
|
||||
val flow = flow<Int> {
|
||||
delay(1)
|
||||
throw IllegalStateException()
|
||||
}
|
||||
runBlocking {
|
||||
try {
|
||||
operator.transactional(flow).toList()
|
||||
} catch (ex: IllegalStateException) {
|
||||
assertThat(tm.commit).isFalse()
|
||||
assertThat(tm.rollback).isTrue()
|
||||
return@runBlocking
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user