RESOLVED - issue SPR-6668: Small Connection leak in DataSourceInitializer
http://jira.springframework.org/browse/SPR-6668
This commit is contained in:
parent
e195c39d3c
commit
e4d8651aa9
|
|
@ -15,9 +15,13 @@
|
||||||
*/
|
*/
|
||||||
package org.springframework.jdbc.datasource.init;
|
package org.springframework.jdbc.datasource.init;
|
||||||
|
|
||||||
|
import java.sql.Connection;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
|
||||||
import javax.sql.DataSource;
|
import javax.sql.DataSource;
|
||||||
|
|
||||||
import org.springframework.beans.factory.InitializingBean;
|
import org.springframework.beans.factory.InitializingBean;
|
||||||
|
import org.springframework.dao.DataAccessResourceFailureException;
|
||||||
import org.springframework.util.Assert;
|
import org.springframework.util.Assert;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -66,7 +70,23 @@ public class DataSourceInitializer implements InitializingBean {
|
||||||
if (enabled) {
|
if (enabled) {
|
||||||
Assert.state(dataSource != null, "DataSource must be provided");
|
Assert.state(dataSource != null, "DataSource must be provided");
|
||||||
Assert.state(databasePopulator != null, "DatabasePopulator 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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -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
|
@Test
|
||||||
public void testCreateWithResourcePattern() throws Exception {
|
public void testCreateWithResourcePattern() throws Exception {
|
||||||
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
|
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,9 @@ package org.springframework.jdbc.datasource.init;
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
|
import java.sql.Connection;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
|
||||||
import javax.sql.DataSource;
|
import javax.sql.DataSource;
|
||||||
|
|
||||||
import org.junit.Test;
|
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-schema-failed-drop-comments.sql"));
|
||||||
databasePopulator.addScript(resourceLoader.getResource("db-test-data.sql"));
|
databasePopulator.addScript(resourceLoader.getResource("db-test-data.sql"));
|
||||||
databasePopulator.setIgnoreFailedDrops(true);
|
databasePopulator.setIgnoreFailedDrops(true);
|
||||||
databasePopulator.populate(db.getConnection());
|
Connection connection = db.getConnection();
|
||||||
|
try {
|
||||||
|
databasePopulator.populate(connection);
|
||||||
|
} finally {
|
||||||
|
connection.close();
|
||||||
|
}
|
||||||
assertDatabaseCreated(db);
|
assertDatabaseCreated(db);
|
||||||
db.shutdown();
|
db.shutdown();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue