Skip to content

Commit

Permalink
Use URLConnection 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 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 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 e249540 commit e10551b
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 10 deletions.
Expand Up @@ -297,14 +297,12 @@ public long lastModified() throws IOException {

/**
* Customize the given {@link URLConnection} before fetching the resource.
* <p>Calls {@link ResourceUtils#useCachesIfNecessary(URLConnection)} and
* delegates to {@link #customizeConnection(HttpURLConnection)} if possible.
* <p>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);
}
Expand Down
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 @@ -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},
Expand Down Expand Up @@ -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);
Expand Down
Expand Up @@ -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"));
}

}

0 comments on commit e10551b

Please sign in to comment.