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
This commit is contained in:
Sébastien Deleuze
2023-11-21 12:00:33 +01:00
parent 878b33c56f
commit bec385d310
2 changed files with 6 additions and 4 deletions

View File

@@ -680,7 +680,6 @@ public class PathMatchingResourcePatternResolver implements ResourcePatternResol
if (con instanceof JarURLConnection jarCon) {
// Should usually be the case for traditional JAR files.
ResourceUtils.useCachesIfNecessary(jarCon);
jarFile = jarCon.getJarFile();
jarFileUrl = jarCon.getJarFileURL().toExternalForm();
JarEntry jarEntry = jarCon.getJarEntry();

View File

@@ -18,6 +18,7 @@ package org.springframework.util;
import java.io.File;
import java.io.FileNotFoundException;
import java.net.JarURLConnection;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
@@ -422,12 +423,14 @@ public abstract class ResourceUtils {
/**
* Set the {@link URLConnection#setUseCaches "useCaches"} flag on the
* given connection, preferring {@code false} but leaving the
* flag at {@code true} for JNLP based resources.
* given connection, preferring {@code false} but leaving the flag at
* its JVM default value for jar resources (typically {@code true}).
* @param con the URLConnection to set the flag on
*/
public static void useCachesIfNecessary(URLConnection con) {
con.setUseCaches(con.getClass().getSimpleName().startsWith("JNLP"));
if (!(con instanceof JarURLConnection)) {
con.setUseCaches(false);
}
}
}