Skip to content

Commit 67d5a12

Browse files
committed
Merge pull request #87 from olivergierke/SPR-9457
* SPR-9457: Use transactional connection during db population
2 parents c471bdd + 49c9a2a commit 67d5a12

File tree

2 files changed

+39
-9
lines changed

2 files changed

+39
-9
lines changed

Diff for: spring-jdbc/src/main/java/org/springframework/jdbc/datasource/init/DatabasePopulatorUtils.java

+7-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2011 the original author or authors.
2+
* Copyright 2002-2012 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,16 +17,18 @@
1717
package org.springframework.jdbc.datasource.init;
1818

1919
import java.sql.Connection;
20-
import java.sql.SQLException;
20+
2121
import javax.sql.DataSource;
2222

2323
import org.springframework.dao.DataAccessResourceFailureException;
24+
import org.springframework.jdbc.datasource.DataSourceUtils;
2425
import org.springframework.util.Assert;
2526

2627
/**
2728
* Utility methods for executing a DatabasePopulator.
2829
*
2930
* @author Juergen Hoeller
31+
* @author Oliver Gierke
3032
* @since 3.1
3133
*/
3234
public abstract class DatabasePopulatorUtils {
@@ -40,16 +42,13 @@ public static void execute(DatabasePopulator populator, DataSource dataSource) {
4042
Assert.notNull(populator, "DatabasePopulator must be provided");
4143
Assert.notNull(dataSource, "DataSource must be provided");
4244
try {
43-
Connection connection = dataSource.getConnection();
45+
Connection connection = DataSourceUtils.getConnection(dataSource);
4446
try {
4547
populator.populate(connection);
4648
}
4749
finally {
48-
try {
49-
connection.close();
50-
}
51-
catch (SQLException ex) {
52-
// ignore
50+
if (connection != null) {
51+
DataSourceUtils.releaseConnection(connection, dataSource);
5352
}
5453
}
5554
}

Diff for: spring-jdbc/src/test/java/org/springframework/jdbc/datasource/init/DatabasePopulatorTests.java

+32-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2010 the original author or authors.
2+
* Copyright 2002-2012 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,17 +19,24 @@
1919
import static org.junit.Assert.assertEquals;
2020

2121
import java.sql.Connection;
22+
import java.sql.SQLException;
23+
24+
import org.easymock.EasyMock;
2225

2326
import org.junit.After;
2427
import org.junit.Test;
28+
2529
import org.springframework.core.io.ClassRelativeResourceLoader;
2630
import org.springframework.jdbc.core.JdbcTemplate;
31+
import org.springframework.jdbc.datasource.DataSourceUtils;
2732
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabase;
2833
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
34+
import org.springframework.transaction.support.TransactionSynchronizationManager;
2935

3036
/**
3137
* @author Dave Syer
3238
* @author Sam Brannen
39+
* @author Oliver Gierke
3340
*/
3441
public class DatabasePopulatorTests {
3542

@@ -54,6 +61,12 @@ private void assertUsersDatabaseCreated() {
5461

5562
@After
5663
public void shutDown() {
64+
65+
if (TransactionSynchronizationManager.isSynchronizationActive()) {
66+
TransactionSynchronizationManager.clear();
67+
TransactionSynchronizationManager.unbindResource(db);
68+
}
69+
5770
db.shutdown();
5871
}
5972

@@ -207,4 +220,22 @@ public void testBuildWithSelectStatements() throws Exception {
207220
assertEquals(1, jdbcTemplate.queryForInt("select COUNT(NAME) from T_TEST where NAME='Dave'"));
208221
}
209222

223+
/**
224+
* @see SPR-9457
225+
*/
226+
@Test
227+
public void usesBoundConnectionIfAvailable() throws SQLException {
228+
229+
TransactionSynchronizationManager.initSynchronization();
230+
Connection connection = DataSourceUtils.getConnection(db);
231+
232+
DatabasePopulator populator = EasyMock.createMock(DatabasePopulator.class);
233+
populator.populate(connection);
234+
EasyMock.expectLastCall();
235+
EasyMock.replay(populator);
236+
237+
DatabasePopulatorUtils.execute(populator, db);
238+
239+
EasyMock.verify(populator);
240+
}
210241
}

0 commit comments

Comments
 (0)