Browse Source

RESOLVED - issue SPR-6668: Small Connection leak in DataSourceInitializer

http://jira.springframework.org/browse/SPR-6668
pull/23217/head
David Syer 15 years ago
parent
commit
e4d8651aa9
  1. 22
      org.springframework.jdbc/src/main/java/org/springframework/jdbc/datasource/init/DataSourceInitializer.java
  2. 12
      org.springframework.jdbc/src/test/java/org/springframework/jdbc/config/JdbcNamespaceIntegrationTest.java
  3. 10
      org.springframework.jdbc/src/test/java/org/springframework/jdbc/datasource/init/DatabasePopulatorTests.java

22
org.springframework.jdbc/src/main/java/org/springframework/jdbc/datasource/init/DataSourceInitializer.java

@ -15,9 +15,13 @@ @@ -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 { @@ -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);
}
}
}
}

12
org.springframework.jdbc/src/test/java/org/springframework/jdbc/config/JdbcNamespaceIntegrationTest.java

@ -23,6 +23,18 @@ public class JdbcNamespaceIntegrationTest { @@ -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(

10
org.springframework.jdbc/src/test/java/org/springframework/jdbc/datasource/init/DatabasePopulatorTests.java

@ -2,6 +2,9 @@ package org.springframework.jdbc.datasource.init; @@ -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 { @@ -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();
}

Loading…
Cancel
Save