From e10551b1ec27a455dff8a7f1d1c5a3d383ba8473 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Deleuze?= Date: Tue, 21 Nov 2023 11:18:45 +0100 Subject: [PATCH] Use URLConnection caching defaults In order to prevent leaks of large amounts of non-heap memory (and potential other efficiency and performance side effects), this commit turns ResourceUtils#useCachesIfNecessary into a no-op method and deprecates it. It is recommended to not use it anymore. This method has been 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 --- .../core/io/AbstractFileResolvingResource.java | 4 +--- .../io/support/PathMatchingResourcePatternResolver.java | 1 - .../core/io/support/PropertiesLoaderUtils.java | 2 -- .../main/java/org/springframework/util/ResourceUtils.java | 8 ++++---- 4 files changed, 5 insertions(+), 10 deletions(-) diff --git a/spring-core/src/main/java/org/springframework/core/io/AbstractFileResolvingResource.java b/spring-core/src/main/java/org/springframework/core/io/AbstractFileResolvingResource.java index dba00a04518f..a37b7a02e6c5 100644 --- a/spring-core/src/main/java/org/springframework/core/io/AbstractFileResolvingResource.java +++ b/spring-core/src/main/java/org/springframework/core/io/AbstractFileResolvingResource.java @@ -297,14 +297,12 @@ public long lastModified() throws IOException { /** * Customize the given {@link URLConnection} before fetching the resource. - *

Calls {@link ResourceUtils#useCachesIfNecessary(URLConnection)} and - * delegates to {@link #customizeConnection(HttpURLConnection)} if possible. + *

Delegates to {@link #customizeConnection(HttpURLConnection)} if possible. * Can be overridden in subclasses. * @param con the URLConnection to customize * @throws IOException if thrown from URLConnection methods */ protected void customizeConnection(URLConnection con) throws IOException { - ResourceUtils.useCachesIfNecessary(con); if (con instanceof HttpURLConnection httpConn) { customizeConnection(httpConn); } diff --git a/spring-core/src/main/java/org/springframework/core/io/support/PathMatchingResourcePatternResolver.java b/spring-core/src/main/java/org/springframework/core/io/support/PathMatchingResourcePatternResolver.java index 84b085036dee..318855cadc03 100644 --- a/spring-core/src/main/java/org/springframework/core/io/support/PathMatchingResourcePatternResolver.java +++ b/spring-core/src/main/java/org/springframework/core/io/support/PathMatchingResourcePatternResolver.java @@ -680,7 +680,6 @@ protected Set 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(); diff --git a/spring-core/src/main/java/org/springframework/core/io/support/PropertiesLoaderUtils.java b/spring-core/src/main/java/org/springframework/core/io/support/PropertiesLoaderUtils.java index af5d58e8ec9e..13b8f1d6efae 100644 --- a/spring-core/src/main/java/org/springframework/core/io/support/PropertiesLoaderUtils.java +++ b/spring-core/src/main/java/org/springframework/core/io/support/PropertiesLoaderUtils.java @@ -30,7 +30,6 @@ import org.springframework.util.ClassUtils; import org.springframework.util.DefaultPropertiesPersister; import org.springframework.util.PropertiesPersister; -import org.springframework.util.ResourceUtils; /** * Convenient utility methods for loading of {@code java.util.Properties}, @@ -178,7 +177,6 @@ public static Properties loadAllProperties(String resourceName, @Nullable ClassL while (urls.hasMoreElements()) { URL url = urls.nextElement(); URLConnection con = url.openConnection(); - ResourceUtils.useCachesIfNecessary(con); try (InputStream is = con.getInputStream()) { if (resourceName.endsWith(XML_FILE_EXTENSION)) { props.loadFromXML(is); diff --git a/spring-core/src/main/java/org/springframework/util/ResourceUtils.java b/spring-core/src/main/java/org/springframework/util/ResourceUtils.java index db7f2e0e6f3f..52a00f580a8d 100644 --- a/spring-core/src/main/java/org/springframework/util/ResourceUtils.java +++ b/spring-core/src/main/java/org/springframework/util/ResourceUtils.java @@ -421,13 +421,13 @@ 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. + * Leave the {@link URLConnection#setUseCaches "useCaches"} flag to its + * default value. * @param con the URLConnection to set the flag on + * @deprecated since now a no-op implementations */ + @Deprecated(since = "6.1.1", forRemoval = true) public static void useCachesIfNecessary(URLConnection con) { - con.setUseCaches(con.getClass().getSimpleName().startsWith("JNLP")); } }