Skip to content

Commit cc5ae23

Browse files
committed
Suppress rollback attempt in case of timeout (connection closed)
Closes gh-34714
1 parent dbd47ff commit cc5ae23

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

Diff for: spring-jdbc/src/main/java/org/springframework/jdbc/datasource/LazyConnectionDataSourceProxy.java

+16-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 the original author or authors.
2+
* Copyright 2002-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -77,6 +77,9 @@
7777
* You will get the same effect with non-transactional reads, but lazy fetching
7878
* of JDBC Connections allows you to still perform reads in transactions.
7979
*
80+
* <p>As of 6.2.6, this DataSource proxy also suppresses a rollback attempt
81+
* in case of a timeout where the connection has been closed in the meantime.
82+
*
8083
* <p><b>NOTE:</b> This DataSource proxy needs to return wrapped Connections
8184
* (which implement the {@link ConnectionProxy} interface) in order to handle
8285
* lazy fetching of an actual JDBC Connection. Use {@link Connection#unwrap}
@@ -436,11 +439,19 @@ public Object invoke(Object proxy, Method method, Object[] args) throws Throwabl
436439
return null;
437440
}
438441

439-
// Target Connection already fetched,
440-
// or target Connection necessary for current operation ->
441-
// invoke method on target connection.
442+
443+
// Target Connection already fetched, or target Connection necessary for current operation
444+
// -> invoke method on target connection.
442445
try {
443-
return method.invoke(getTargetConnection(method), args);
446+
Connection conToUse = getTargetConnection(method);
447+
448+
if ("rollback".equals(method.getName()) && conToUse.isClosed()) {
449+
// Connection closed in the meantime, probably due to a resource timeout. Since a
450+
// rollback attempt typically happens right before close, we leniently suppress it.
451+
return null;
452+
}
453+
454+
return method.invoke(conToUse, args);
444455
}
445456
catch (InvocationTargetException ex) {
446457
throw ex.getTargetException();

0 commit comments

Comments
 (0)