Skip to content

Commit

Permalink
Merge pull request spring-projects#87 from olivergierke/SPR-9457
Browse files Browse the repository at this point in the history
* SPR-9457:
  Use transactional connection during db population
  • Loading branch information
cbeams committed Jun 4, 2012
2 parents c471bdd + 49c9a2a commit 67d5a12
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2011 the original author or authors.
* Copyright 2002-2012 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -17,16 +17,18 @@
package org.springframework.jdbc.datasource.init;

import java.sql.Connection;
import java.sql.SQLException;

import javax.sql.DataSource;

import org.springframework.dao.DataAccessResourceFailureException;
import org.springframework.jdbc.datasource.DataSourceUtils;
import org.springframework.util.Assert;

/**
* Utility methods for executing a DatabasePopulator.
*
* @author Juergen Hoeller
* @author Oliver Gierke
* @since 3.1
*/
public abstract class DatabasePopulatorUtils {
Expand All @@ -40,16 +42,13 @@ public static void execute(DatabasePopulator populator, DataSource dataSource) {
Assert.notNull(populator, "DatabasePopulator must be provided");
Assert.notNull(dataSource, "DataSource must be provided");
try {
Connection connection = dataSource.getConnection();
Connection connection = DataSourceUtils.getConnection(dataSource);
try {
populator.populate(connection);
}
finally {
try {
connection.close();
}
catch (SQLException ex) {
// ignore
if (connection != null) {
DataSourceUtils.releaseConnection(connection, dataSource);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2010 the original author or authors.
* Copyright 2002-2012 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -19,17 +19,24 @@
import static org.junit.Assert.assertEquals;

import java.sql.Connection;
import java.sql.SQLException;

import org.easymock.EasyMock;

import org.junit.After;
import org.junit.Test;

import org.springframework.core.io.ClassRelativeResourceLoader;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceUtils;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabase;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
import org.springframework.transaction.support.TransactionSynchronizationManager;

/**
* @author Dave Syer
* @author Sam Brannen
* @author Oliver Gierke
*/
public class DatabasePopulatorTests {

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

@After
public void shutDown() {

if (TransactionSynchronizationManager.isSynchronizationActive()) {
TransactionSynchronizationManager.clear();
TransactionSynchronizationManager.unbindResource(db);
}

db.shutdown();
}

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

/**
* @see SPR-9457
*/
@Test
public void usesBoundConnectionIfAvailable() throws SQLException {

TransactionSynchronizationManager.initSynchronization();
Connection connection = DataSourceUtils.getConnection(db);

DatabasePopulator populator = EasyMock.createMock(DatabasePopulator.class);
populator.populate(connection);
EasyMock.expectLastCall();
EasyMock.replay(populator);

DatabasePopulatorUtils.execute(populator, db);

EasyMock.verify(populator);
}
}

0 comments on commit 67d5a12

Please sign in to comment.