Browse Source

Polishing

pull/30218/head
Juergen Hoeller 2 years ago
parent
commit
b2be07c73c
  1. 30
      framework-docs/src/docs/asciidoc/data-access.adoc
  2. 27
      spring-context/src/main/java/org/springframework/scheduling/concurrent/ConcurrentTaskScheduler.java

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

@ -1030,26 +1030,28 @@ marks a transaction for rollback only in the case of runtime, unchecked exceptio @@ -1030,26 +1030,28 @@ marks a transaction for rollback only in the case of runtime, unchecked exceptio
That is, when the thrown exception is an instance or subclass of `RuntimeException`.
(`Error` instances also, by default, result in a rollback).
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.
As of Spring Framework 5.2, 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. For more information on Vavr's Try,
refer to the [official Vavr documentation](https://www.vavr.io/vavr-docs/#_try).
Here's an example of how to use Vavr's Try:
Here's an example of how to use Vavr's Try with a transactional method:
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
.Java
----
@Transactional
public Try<String> myTransactionalMethod() {
// 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.
return Try.of(serviceA::transactionalMethod);
}
@Transactional
public Try<String> myTransactionalMethod() {
// If myDataAccessOperation 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.
return Try.of(delegate::myDataAccessOperation);
}
----
For more information on Vavr's Try, refer to the [official Vavr documentation](https://www.vavr.io/vavr-docs/#_try).
Checked exceptions that are
thrown from a transactional method do not result in rollback in the default
configuration.
You can configure exactly which `Exception` types mark a transaction for rollback,
including checked exceptions by specifying _rollback rules_.
Checked exceptions that are thrown from a transactional method do not result in a rollback
in the default configuration. You can configure exactly which `Exception` types mark a
transaction for rollback, including checked exceptions by specifying _rollback rules_.
.Rollback rules
[[transaction-declarative-rollback-rules]]

27
spring-context/src/main/java/org/springframework/scheduling/concurrent/ConcurrentTaskScheduler.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2023 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.
@ -281,11 +281,11 @@ public class ConcurrentTaskScheduler extends ConcurrentTaskExecutor implements T @@ -281,11 +281,11 @@ public class ConcurrentTaskScheduler extends ConcurrentTaskExecutor implements T
return executor.schedule(task, new TriggerAdapter(trigger));
}
private static class TriggerAdapter implements jakarta.enterprise.concurrent.Trigger {
private final Trigger adaptee;
public TriggerAdapter(Trigger adaptee) {
this.adaptee = adaptee;
}
@ -294,48 +294,45 @@ public class ConcurrentTaskScheduler extends ConcurrentTaskExecutor implements T @@ -294,48 +294,45 @@ public class ConcurrentTaskScheduler extends ConcurrentTaskExecutor implements T
@Nullable
public Date getNextRunTime(@Nullable LastExecution le, Date taskScheduledTime) {
Instant instant = this.adaptee.nextExecution(new LastExecutionAdapter(le));
return instant != null ? Date.from(instant) : null;
return (instant != null ? Date.from(instant) : null);
}
@Nullable
private static Instant toInstant(@Nullable Date date) {
return date != null ? date.toInstant() : null;
}
@Override
public boolean skipRun(LastExecution lastExecutionInfo, Date scheduledRunTime) {
return false;
}
private static class LastExecutionAdapter implements TriggerContext {
@Nullable
private final LastExecution le;
public LastExecutionAdapter(@Nullable LastExecution le) {
this.le = le;
}
@Override
public Instant lastScheduledExecution() {
return (this.le != null) ? toInstant(this.le.getScheduledStart()) : null;
return (this.le != null ? toInstant(this.le.getScheduledStart()) : null);
}
@Override
public Instant lastActualExecution() {
return (this.le != null) ? toInstant(this.le.getRunStart()) : null;
return (this.le != null ? toInstant(this.le.getRunStart()) : null);
}
@Override
public Instant lastCompletion() {
return (this.le != null) ? toInstant(this.le.getRunEnd()) : null;
return (this.le != null ? toInstant(this.le.getRunEnd()) : null);
}
@Nullable
private static Instant toInstant(@Nullable Date date) {
return (date != null ? date.toInstant() : null);
}
}
}
}
}

Loading…
Cancel
Save