forked from spring-projects/spring-framework
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for Coroutines transactions
This commits adds Coroutines extensions for TransactionalOperator.transactional that accept suspending lambda or Kotlin Flow parameters. @transactional on suspending functions is not supported yet, spring-projectsgh-23575 has been created for that purpose. This commit also renames invokeHandlerMethod to invokeSuspendingFunction in CoroutinesUtils. Closes spring-projectsgh-22915
- Loading branch information
Showing
8 changed files
with
166 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
...c/main/kotlin/org/springframework/transaction/reactive/TransactionalOperatorExtensions.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package org.springframework.transaction.reactive | ||
|
||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.ExperimentalCoroutinesApi | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.reactive.asFlow | ||
import kotlinx.coroutines.reactive.awaitFirstOrNull | ||
import kotlinx.coroutines.reactor.asFlux | ||
import kotlinx.coroutines.reactor.mono | ||
|
||
/** | ||
* Coroutines variant of [TransactionalOperator.transactional] with a [Flow] parameter. | ||
* | ||
* @author Sebastien Deleuze | ||
* @since 5.2 | ||
*/ | ||
@ExperimentalCoroutinesApi | ||
fun <T : Any> TransactionalOperator.transactional(flow: Flow<T>): Flow<T> = | ||
transactional(flow.asFlux()).asFlow() | ||
|
||
/** | ||
* Coroutines variant of [TransactionalOperator.transactional] with a suspending lambda | ||
* parameter. | ||
* | ||
* @author Sebastien Deleuze | ||
* @since 5.2 | ||
*/ | ||
suspend fun <T : Any> TransactionalOperator.transactional(f: suspend () -> T?): T? = | ||
transactional(mono(Dispatchers.Unconfined) { f() }).awaitFirstOrNull() |
100 changes: 100 additions & 0 deletions
100
...t/kotlin/org/springframework/transaction/reactive/TransactionalOperatorExtensionsTests.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters