Skip to content

Commit a59fee4

Browse files
committed
Simplify TransactionalOperator.executeAndAwait
This commit simplifies TransactionalOperator.executeAndAwait by removing Optional. It is based on a proposal by @vlsi refined to handle properly reactive transaction commits (see related commit 217b6e3). Closes spring-projectsgh-36039
1 parent 9711db7 commit a59fee4

1 file changed

Lines changed: 3 additions & 2 deletions

File tree

spring-tx/src/main/kotlin/org/springframework/transaction/reactive/TransactionalOperatorExtensions.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import kotlinx.coroutines.Job
2020
import kotlinx.coroutines.currentCoroutineContext
2121
import kotlinx.coroutines.flow.Flow
2222
import kotlinx.coroutines.reactive.asFlow
23+
import kotlinx.coroutines.reactive.awaitFirstOrNull
2324
import kotlinx.coroutines.reactive.awaitLast
2425
import kotlinx.coroutines.reactor.asFlux
2526
import kotlinx.coroutines.reactor.mono
@@ -47,6 +48,6 @@ fun <T : Any> Flow<T>.transactional(operator: TransactionalOperator, context: Co
4748
*/
4849
suspend fun <T> TransactionalOperator.executeAndAwait(f: suspend (ReactiveTransaction) -> T): T {
4950
val context = currentCoroutineContext().minusKey(Job.Key)
50-
return execute { status -> mono(context) { f(status) } }.map { value -> Optional.ofNullable(value) }
51-
.defaultIfEmpty(Optional.empty()).awaitLast().orElse(null)
51+
@Suppress("UNCHECKED_CAST")
52+
return execute { status -> mono<T & Any>(context) { f(status) } }.singleOrEmpty().awaitFirstOrNull() as T
5253
}

0 commit comments

Comments
 (0)