From e4d8651aa94cf8611b4096ae37bfa7b5953d0e84 Mon Sep 17 00:00:00 2001 From: David Syer Date: Wed, 13 Jan 2010 11:00:41 +0000 Subject: [PATCH] RESOLVED - issue SPR-6668: Small Connection leak in DataSourceInitializer http://jira.springframework.org/browse/SPR-6668 --- .../init/DataSourceInitializer.java | 22 ++++++++++++++++++- .../config/JdbcNamespaceIntegrationTest.java | 12 ++++++++++ .../init/DatabasePopulatorTests.java | 10 ++++++++- 3 files changed, 42 insertions(+), 2 deletions(-) diff --git a/org.springframework.jdbc/src/main/java/org/springframework/jdbc/datasource/init/DataSourceInitializer.java b/org.springframework.jdbc/src/main/java/org/springframework/jdbc/datasource/init/DataSourceInitializer.java index f90d32a155..e927847826 100644 --- a/org.springframework.jdbc/src/main/java/org/springframework/jdbc/datasource/init/DataSourceInitializer.java +++ b/org.springframework.jdbc/src/main/java/org/springframework/jdbc/datasource/init/DataSourceInitializer.java @@ -15,9 +15,13 @@ */ package org.springframework.jdbc.datasource.init; +import java.sql.Connection; +import java.sql.SQLException; + import javax.sql.DataSource; import org.springframework.beans.factory.InitializingBean; +import org.springframework.dao.DataAccessResourceFailureException; import org.springframework.util.Assert; /** @@ -66,7 +70,23 @@ public class DataSourceInitializer implements InitializingBean { if (enabled) { Assert.state(dataSource != null, "DataSource must be provided"); Assert.state(databasePopulator != null, "DatabasePopulator must be provided"); - databasePopulator.populate(dataSource.getConnection()); + try { + Connection connection = this.dataSource.getConnection(); + try { + this.databasePopulator.populate(connection); + } + finally { + try { + connection.close(); + } + catch (SQLException ex) { + // ignore + } + } + } + catch (SQLException ex) { + throw new DataAccessResourceFailureException("Failed to populate database", ex); + } } } } \ No newline at end of file diff --git a/org.springframework.jdbc/src/test/java/org/springframework/jdbc/config/JdbcNamespaceIntegrationTest.java b/org.springframework.jdbc/src/test/java/org/springframework/jdbc/config/JdbcNamespaceIntegrationTest.java index d3a9e02618..22d189a9ad 100644 --- a/org.springframework.jdbc/src/test/java/org/springframework/jdbc/config/JdbcNamespaceIntegrationTest.java +++ b/org.springframework.jdbc/src/test/java/org/springframework/jdbc/config/JdbcNamespaceIntegrationTest.java @@ -23,6 +23,18 @@ public class JdbcNamespaceIntegrationTest { } } + @Test + public void testCreateEmbeddedDatabaseAgain() throws Exception { + // If Derby isn't cleaned up properly this will fail... + ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( + "org/springframework/jdbc/config/jdbc-config.xml"); + try { + assertCorrectSetup(context.getBean("derbyDataSource", DataSource.class)); + } finally { + context.close(); + } + } + @Test public void testCreateWithResourcePattern() throws Exception { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( diff --git a/org.springframework.jdbc/src/test/java/org/springframework/jdbc/datasource/init/DatabasePopulatorTests.java b/org.springframework.jdbc/src/test/java/org/springframework/jdbc/datasource/init/DatabasePopulatorTests.java index 8cb2bd5bab..d79ed20dc2 100644 --- a/org.springframework.jdbc/src/test/java/org/springframework/jdbc/datasource/init/DatabasePopulatorTests.java +++ b/org.springframework.jdbc/src/test/java/org/springframework/jdbc/datasource/init/DatabasePopulatorTests.java @@ -2,6 +2,9 @@ package org.springframework.jdbc.datasource.init; import static org.junit.Assert.assertEquals; +import java.sql.Connection; +import java.sql.SQLException; + import javax.sql.DataSource; import org.junit.Test; @@ -21,7 +24,12 @@ public class DatabasePopulatorTests { databasePopulator.addScript(resourceLoader.getResource("db-schema-failed-drop-comments.sql")); databasePopulator.addScript(resourceLoader.getResource("db-test-data.sql")); databasePopulator.setIgnoreFailedDrops(true); - databasePopulator.populate(db.getConnection()); + Connection connection = db.getConnection(); + try { + databasePopulator.populate(connection); + } finally { + connection.close(); + } assertDatabaseCreated(db); db.shutdown(); }