Skip to content

Commit 7263771

Browse files
committed
Add documentation for CompletableFuture-driven rollback
Closes gh-32709
1 parent fab3633 commit 7263771

File tree

2 files changed

+36
-8
lines changed

2 files changed

+36
-8
lines changed

framework-docs/modules/ROOT/pages/data-access/transaction/declarative/annotations.adoc

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,11 @@ Kotlin::
7474
----
7575
======
7676

77-
Used at the class level as above, the annotation indicates a default for all methods of
78-
the declaring class (as well as its subclasses). Alternatively, each method can be
79-
annotated individually. See xref:data-access/transaction/declarative/annotations.adoc#transaction-declarative-annotations-method-visibility[method visibility] for
80-
further details on which methods Spring considers transactional. Note that a class-level
77+
Used at the class level as above, the annotation indicates a default for all methods
78+
of the declaring class (as well as its subclasses). Alternatively, each method can be
79+
annotated individually. See
80+
xref:data-access/transaction/declarative/annotations.adoc#transaction-declarative-annotations-method-visibility[method visibility]
81+
for further details on which methods Spring considers transactional. Note that a class-level
8182
annotation does not apply to ancestor classes up the class hierarchy; in such a scenario,
8283
inherited methods need to be locally redeclared in order to participate in a
8384
subclass-level annotation.
@@ -436,9 +437,10 @@ properties of the `@Transactional` annotation:
436437
| Optional array of exception name patterns that must not cause rollback.
437438
|===
438439

439-
TIP: See xref:data-access/transaction/declarative/rolling-back.adoc#transaction-declarative-rollback-rules[Rollback rules] for further details
440-
on rollback rule semantics, patterns, and warnings regarding possible unintentional
441-
matches for pattern-based rollback rules.
440+
TIP: See
441+
xref:data-access/transaction/declarative/rolling-back.adoc#transaction-declarative-rollback-rules[Rollback rules]
442+
for further details on rollback rule semantics, patterns, and warnings
443+
regarding possible unintentional matches for pattern-based rollback rules.
442444

443445
Currently, you cannot have explicit control over the name of a transaction, where 'name'
444446
means the transaction name that appears in a transaction monitor and in logging output.

framework-docs/modules/ROOT/pages/data-access/transaction/declarative/rolling-back.adoc

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ Vavr's `Try` method to trigger transaction rollbacks when it returns a 'Failure'
2424
This allows you to handle functional-style errors using Try and have the transaction
2525
automatically rolled back in case of a failure. For more information on Vavr's Try,
2626
refer to the https://docs.vavr.io/#_try[official Vavr documentation].
27-
2827
Here's an example of how to use Vavr's Try with a transactional method:
28+
2929
[tabs]
3030
======
3131
Java::
@@ -42,6 +42,32 @@ Java::
4242
----
4343
======
4444

45+
As of Spring Framework 6.1, there is also special treatment of `CompletableFuture`
46+
(and general `Future`) return values, triggering a rollback for such a handle if it
47+
was exceptionally completed at the time of being returned from the original method.
48+
This is intended for `@Async` methods where the actual method implementation may
49+
need to comply with a `CompletableFuture` signature (auto-adapted to an actual
50+
asynchronous handle for a call to the proxy by `@Async` processing at runtime),
51+
preferring exposure in the returned handle rather than rethrowing an exception:
52+
53+
[tabs]
54+
======
55+
Java::
56+
+
57+
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
58+
----
59+
@Transactional @Async
60+
public CompletableFuture<String> myTransactionalMethod() {
61+
try {
62+
return CompletableFuture.completedFuture(delegate.myDataAccessOperation());
63+
}
64+
catch (DataAccessException ex) {
65+
return CompletableFuture.failedFuture(ex);
66+
}
67+
}
68+
----
69+
======
70+
4571
Checked exceptions that are thrown from a transactional method do not result in a rollback
4672
in the default configuration. You can configure exactly which `Exception` types mark a
4773
transaction for rollback, including checked exceptions by specifying _rollback rules_.

0 commit comments

Comments
 (0)