From 90dfbcd7b3ea65c941564952f551030e1b2a67b2 Mon Sep 17 00:00:00 2001 From: Rob Bygrave Date: Mon, 11 Jun 2018 00:02:14 +1200 Subject: [PATCH] #1412 - Fix Resource leaks for properties & yaml files etc - DefaultContainer.checkDataSource() The resource was already being closed properly, changing to use try with resources and note that this now throws PersistenceException if we fail to close() on the connection (which I think is an ok change). --- .../server/core/DefaultContainer.java | 16 ++-------------- 1 file changed, 2 insertions(+), 14 deletions(-) diff --git a/src/main/java/io/ebeaninternal/server/core/DefaultContainer.java b/src/main/java/io/ebeaninternal/server/core/DefaultContainer.java index e1e923dea..09ce89349 100644 --- a/src/main/java/io/ebeaninternal/server/core/DefaultContainer.java +++ b/src/main/java/io/ebeaninternal/server/core/DefaultContainer.java @@ -10,7 +10,6 @@ import io.ebean.config.TenantMode; import io.ebean.config.UnderscoreNamingConvention; import io.ebean.config.dbplatform.DatabasePlatform; import io.ebean.config.dbplatform.h2.H2Platform; -import io.ebean.config.properties.PropertiesLoader; import io.ebean.service.SpiContainer; import io.ebeaninternal.api.SpiBackgroundExecutor; import io.ebeaninternal.api.SpiContainerBootup; @@ -374,25 +373,14 @@ public class DefaultContainer implements SpiContainer { throw new RuntimeException("DataSource not set?"); } - Connection c = null; - try { - c = serverConfig.getDataSource().getConnection(); - if (!serverConfig.isAutoCommitMode() && c.getAutoCommit()) { + try (Connection connection = serverConfig.getDataSource().getConnection()) { + if (!serverConfig.isAutoCommitMode() && connection.getAutoCommit()) { logger.warn("DataSource [{}] has autoCommit defaulting to true!", serverConfig.getName()); } return true; } catch (SQLException ex) { throw new PersistenceException(ex); - - } finally { - if (c != null) { - try { - c.close(); - } catch (SQLException ex) { - logger.error("Error closing connection", ex); - } - } } }