Skip to content

Commit 40466d0

Browse files
authored
fix: Catch retryable exceptions thrown by SDN itself. (#2945)
This fix #2944 by making the retryable exception messages consistent by introducing constants for prematurely closed transactions and sessions omitting the trailing dot as agreed within the bigger Spring Data team.
1 parent 79d8aaf commit 40466d0

File tree

3 files changed

+12
-7
lines changed

3 files changed

+12
-7
lines changed

src/main/java/org/springframework/data/neo4j/core/support/RetryExceptionPredicate.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.neo4j.driver.exceptions.TransientException;
2727
import org.springframework.dao.TransientDataAccessResourceException;
2828

29+
2930
/**
3031
* A predicate indicating {@literal true} for {@link Throwable throwables} that can be safely retried and {@literal false}
3132
* in any other case. This predicate can be used for example with Resilience4j.
@@ -37,9 +38,12 @@
3738
@API(status = API.Status.STABLE, since = "6.0")
3839
public final class RetryExceptionPredicate implements Predicate<Throwable> {
3940

41+
public static final String TRANSACTION_MUST_BE_OPEN_BUT_HAS_ALREADY_BEEN_CLOSED = "Transaction must be open, but has already been closed";
42+
public static final String SESSION_MUST_BE_OPEN_BUT_HAS_ALREADY_BEEN_CLOSED = "Session must be open, but has already been closed";
4043
private static final Set<String> RETRYABLE_ILLEGAL_STATE_MESSAGES = Set.of(
41-
"Transaction must be open, but has already been closed.",
42-
"Session must be open, but has already been closed.");
44+
TRANSACTION_MUST_BE_OPEN_BUT_HAS_ALREADY_BEEN_CLOSED,
45+
SESSION_MUST_BE_OPEN_BUT_HAS_ALREADY_BEEN_CLOSED
46+
);
4347

4448
@Override
4549
public boolean test(Throwable throwable) {

src/main/java/org/springframework/data/neo4j/core/transaction/Neo4jTransactionHolder.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.neo4j.driver.Transaction;
2323
import org.springframework.data.neo4j.core.DatabaseSelection;
2424
import org.springframework.data.neo4j.core.UserSelection;
25+
import org.springframework.data.neo4j.core.support.RetryExceptionPredicate;
2526
import org.springframework.lang.Nullable;
2627
import org.springframework.transaction.support.ResourceHolderSupport;
2728
import org.springframework.util.Assert;
@@ -68,7 +69,7 @@ Transaction getTransaction(DatabaseSelection inDatabase, UserSelection asUser) {
6869

6970
Collection<Bookmark> commit() {
7071

71-
Assert.state(hasActiveTransaction(), "Transaction must be open, but has already been closed");
72+
Assert.state(hasActiveTransaction(), RetryExceptionPredicate.TRANSACTION_MUST_BE_OPEN_BUT_HAS_ALREADY_BEEN_CLOSED);
7273
Assert.state(!isRollbackOnly(), "Resource must not be marked as rollback only");
7374

7475
transaction.commit();
@@ -79,15 +80,15 @@ Collection<Bookmark> commit() {
7980

8081
void rollback() {
8182

82-
Assert.state(hasActiveTransaction(), "Transaction must be open, but has already been closed");
83+
Assert.state(hasActiveTransaction(), RetryExceptionPredicate.TRANSACTION_MUST_BE_OPEN_BUT_HAS_ALREADY_BEEN_CLOSED);
8384

8485
transaction.rollback();
8586
transaction.close();
8687
}
8788

8889
void close() {
8990

90-
Assert.state(hasActiveSession(), "Session must be open, but has already been closed");
91+
Assert.state(hasActiveSession(), RetryExceptionPredicate.SESSION_MUST_BE_OPEN_BUT_HAS_ALREADY_BEEN_CLOSED);
9192

9293
if (hasActiveTransaction()) {
9394
transaction.close();

src/test/java/org/springframework/data/neo4j/core/support/RetryExceptionPredicateTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@
3434
class RetryExceptionPredicateTest {
3535

3636
@ParameterizedTest
37-
@ValueSource(strings = { "Transaction must be open, but has already been closed.",
38-
"Session must be open, but has already been closed." })
37+
@ValueSource(strings = { "Transaction must be open, but has already been closed",
38+
"Session must be open, but has already been closed" })
3939
void shouldRetryOnSomeIllegalStateExceptions(String msg) {
4040

4141
RetryExceptionPredicate predicate = new RetryExceptionPredicate();

0 commit comments

Comments
 (0)