Skip to content

Commit 50d01ce

Browse files
edyda99jhoeller
authored andcommitted
feat: Add support for Vavr's Try monad to trigger transaction rollbacks
Updated the Spring Framework documentation to include an example of using Vavr's Try monad to trigger transaction rollbacks when a @Transactional-annotated method returns a Failure. The modified documentation demonstrates how to use Try in a transactional method and how to check if an exception has been wrapped inside a Try.Failure instance. Additionally, a link to the official Vavr documentation was added to provide more information on the Try method.
1 parent 0c0cda9 commit 50d01ce

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

framework-docs/src/docs/asciidoc/data-access.adoc

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1028,7 +1028,23 @@ the call stack and makes a determination whether to mark the transaction for rol
10281028
In its default configuration, the Spring Framework's transaction infrastructure code
10291029
marks a transaction for rollback only in the case of runtime, unchecked exceptions.
10301030
That is, when the thrown exception is an instance or subclass of `RuntimeException`.
1031-
(`Error` instances also, by default, result in a rollback). Checked exceptions that are
1031+
(`Error` instances also, by default, result in a rollback).
1032+
1033+
However, starting with Spring Framework 5.2.0 the default configuration also provides support for Vavr's `Try` method to trigger transaction rollbacks when it returns a 'Failure'. This allows you to handle functional-style errors using Try and have the transaction automatically rolled back in case of a failure.
1034+
1035+
Here's an example of how to use Vavr's Try:
1036+
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
1037+
.Java
1038+
----
1039+
@Transactional
1040+
public Try<String> myTransactionalMethod() {
1041+
// If transactionMethod throws an exception, it will be caught by the Try instance created with Try.of() and wrapped inside the Failure class, which can be checked using the isFailure() method on the Try instance.
1042+
return Try.of(serviceA::transactionalMethod);
1043+
}
1044+
----
1045+
For more information on Vavr's Try, refer to the [official Vavr documentation](https://www.vavr.io/vavr-docs/#_try).
1046+
1047+
Checked exceptions that are
10321048
thrown from a transactional method do not result in rollback in the default
10331049
configuration.
10341050

0 commit comments

Comments
 (0)