Skip to content

Commit

Permalink
Use JarURLConnection caching defaults
Browse files Browse the repository at this point in the history
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 spring-projectsgh-9316 and
spring-projectsgh-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 spring-projectsgh-30955
  • Loading branch information
sdeleuze committed Nov 21, 2023
1 parent 878b33c commit bec385d
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
Expand Up @@ -680,7 +680,6 @@ protected Set<Resource> doFindPathMatchingJarResources(Resource rootDirResource,

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();
Expand Down
Expand Up @@ -18,6 +18,7 @@

import java.io.File;
import java.io.FileNotFoundException;
import java.net.JarURLConnection;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
Expand Down Expand Up @@ -422,12 +423,14 @@ public static URL toRelativeURL(URL root, String relativePath) throws MalformedU

/**
* 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);
}
}

}

0 comments on commit bec385d

Please sign in to comment.