mirror of
https://github.com/spring-projects/spring-boot.git
synced 2024-09-03 04:03:22 +08:00
Fix potential resource leaks
See gh-11624
This commit is contained in:
parent
0b22eb90b5
commit
5a4238acfc
@ -16,6 +16,8 @@
|
||||
|
||||
package org.springframework.boot.devtools.autoconfigure;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.Statement;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
@ -97,7 +99,11 @@ public class DevToolsDataSourceAutoConfiguration {
|
||||
@Override
|
||||
public void destroy() throws Exception {
|
||||
if (dataSourceRequiresShutdown()) {
|
||||
this.dataSource.getConnection().createStatement().execute("SHUTDOWN");
|
||||
try (Connection connection = this.dataSource.getConnection()) {
|
||||
try (Statement statement = connection.createStatement()) {
|
||||
statement.execute("SHUTDOWN");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -215,16 +215,16 @@ public class JarWriter implements LoaderClassesWriter, AutoCloseable {
|
||||
@Override
|
||||
public void writeLoaderClasses(String loaderJarResourceName) throws IOException {
|
||||
URL loaderJar = getClass().getClassLoader().getResource(loaderJarResourceName);
|
||||
JarInputStream inputStream = new JarInputStream(
|
||||
new BufferedInputStream(loaderJar.openStream()));
|
||||
JarEntry entry;
|
||||
while ((entry = inputStream.getNextJarEntry()) != null) {
|
||||
if (entry.getName().endsWith(".class")) {
|
||||
writeEntry(new JarArchiveEntry(entry),
|
||||
new InputStreamEntryWriter(inputStream, false));
|
||||
try (JarInputStream inputStream = new JarInputStream(
|
||||
new BufferedInputStream(loaderJar.openStream()))) {
|
||||
JarEntry entry;
|
||||
while ((entry = inputStream.getNextJarEntry()) != null) {
|
||||
if (entry.getName().endsWith(".class")) {
|
||||
writeEntry(new JarArchiveEntry(entry),
|
||||
new InputStreamEntryWriter(inputStream, false));
|
||||
}
|
||||
}
|
||||
}
|
||||
inputStream.close();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -157,7 +157,9 @@ public class JarFile extends java.util.jar.JarFile {
|
||||
Manifest manifest = (this.manifest == null ? null : this.manifest.get());
|
||||
if (manifest == null) {
|
||||
if (this.type == JarFileType.NESTED_DIRECTORY) {
|
||||
manifest = new JarFile(this.getRootJarFile()).getManifest();
|
||||
try (JarFile rootJarFile = new JarFile(this.getRootJarFile())) {
|
||||
manifest = rootJarFile.getManifest();
|
||||
}
|
||||
}
|
||||
else {
|
||||
try (InputStream inputStream = getInputStream(MANIFEST_NAME,
|
||||
|
@ -127,8 +127,9 @@ public class RunMojo extends AbstractRunMojo {
|
||||
private boolean checkForDevtools() {
|
||||
try {
|
||||
URL[] urls = getClassPathUrls();
|
||||
URLClassLoader classLoader = new URLClassLoader(urls);
|
||||
return (classLoader.findResource(RESTARTER_CLASS_LOCATION) != null);
|
||||
try (URLClassLoader classLoader = new URLClassLoader(urls)) {
|
||||
return (classLoader.findResource(RESTARTER_CLASS_LOCATION) != null);
|
||||
}
|
||||
}
|
||||
catch (Exception ex) {
|
||||
return false;
|
||||
|
Loading…
Reference in New Issue
Block a user