Skip to content

Commit 7140ab5

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 615c25d commit 7140ab5

1 file changed

Lines changed: 3 additions & 4 deletions

File tree

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,10 @@ 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.awaitLast
23+
import kotlinx.coroutines.reactive.awaitFirstOrNull
2424
import kotlinx.coroutines.reactor.asFlux
2525
import kotlinx.coroutines.reactor.mono
2626
import org.springframework.transaction.ReactiveTransaction
27-
import java.util.*
2827
import kotlin.coroutines.CoroutineContext
2928
import kotlin.coroutines.EmptyCoroutineContext
3029

@@ -47,6 +46,6 @@ fun <T : Any> Flow<T>.transactional(operator: TransactionalOperator, context: Co
4746
*/
4847
suspend fun <T> TransactionalOperator.executeAndAwait(f: suspend (ReactiveTransaction) -> T): T {
4948
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)
49+
@Suppress("UNCHECKED_CAST")
50+
return execute { status -> mono<T & Any>(context) { f(status) } }.singleOrEmpty().awaitFirstOrNull() as T
5251
}

0 commit comments

Comments
 (0)