Skip to content

Commit bec385d

Browse files
committed
Use JarURLConnection caching defaults
In order to prevent leaks of large amounts of non-heap memory (and potential other efficiency and performance side effects), this commit updates ResourceUtils#useCachesIfNecessary to leave the caching flag to its JVM default value for instances of JarURLConnection. The previous behavior was originally introduced via gh-9316 and gh-13755 to avoid I/O failure during webapp hot reloading in Servlet containers. This is not a popular deployment mode anymore and we have not been able to reproduce the original issue with a Java 17 JVM and Tomcat 10. Closes gh-30955
1 parent 878b33c commit bec385d

File tree

2 files changed

+6
-4
lines changed

2 files changed

+6
-4
lines changed

spring-core/src/main/java/org/springframework/core/io/support/PathMatchingResourcePatternResolver.java

-1
Original file line numberDiff line numberDiff line change
@@ -680,7 +680,6 @@ protected Set<Resource> doFindPathMatchingJarResources(Resource rootDirResource,
680680

681681
if (con instanceof JarURLConnection jarCon) {
682682
// Should usually be the case for traditional JAR files.
683-
ResourceUtils.useCachesIfNecessary(jarCon);
684683
jarFile = jarCon.getJarFile();
685684
jarFileUrl = jarCon.getJarFileURL().toExternalForm();
686685
JarEntry jarEntry = jarCon.getJarEntry();

spring-core/src/main/java/org/springframework/util/ResourceUtils.java

+6-3
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import java.io.File;
2020
import java.io.FileNotFoundException;
21+
import java.net.JarURLConnection;
2122
import java.net.MalformedURLException;
2223
import java.net.URI;
2324
import java.net.URISyntaxException;
@@ -422,12 +423,14 @@ public static URL toRelativeURL(URL root, String relativePath) throws MalformedU
422423

423424
/**
424425
* Set the {@link URLConnection#setUseCaches "useCaches"} flag on the
425-
* given connection, preferring {@code false} but leaving the
426-
* flag at {@code true} for JNLP based resources.
426+
* given connection, preferring {@code false} but leaving the flag at
427+
* its JVM default value for jar resources (typically {@code true}).
427428
* @param con the URLConnection to set the flag on
428429
*/
429430
public static void useCachesIfNecessary(URLConnection con) {
430-
con.setUseCaches(con.getClass().getSimpleName().startsWith("JNLP"));
431+
if (!(con instanceof JarURLConnection)) {
432+
con.setUseCaches(false);
433+
}
431434
}
432435

433436
}

0 commit comments

Comments
 (0)