Browse Source

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
pull/31644/head
Sébastien Deleuze 2 years ago
parent
commit
bec385d310
  1. 1
      spring-core/src/main/java/org/springframework/core/io/support/PathMatchingResourcePatternResolver.java
  2. 9
      spring-core/src/main/java/org/springframework/util/ResourceUtils.java

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

@ -680,7 +680,6 @@ public class PathMatchingResourcePatternResolver implements ResourcePatternResol @@ -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();

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

@ -18,6 +18,7 @@ package org.springframework.util; @@ -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 { @@ -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);
}
}
}

Loading…
Cancel
Save