2323
2424import org .apache .commons .logging .Log ;
2525import org .apache .commons .logging .LogFactory ;
26- import org .springframework .jdbc .core .JdbcTemplate ;
27- import org .springframework .jdbc .datasource .DataSourceTransactionManager ;
28- import org .springframework .transaction .TransactionStatus ;
29- import org .springframework .transaction .support .TransactionCallbackWithoutResult ;
30- import org .springframework .transaction .support .TransactionTemplate ;
3126import org .springframework .util .Assert ;
3227
3328/**
34- * Returns a {@link EmbeddedDatabase} instance pre-populated with test data.
35- * When the database is returned, callers are guaranteed that the database schema and test data will have already been loaded.
29+ * Returns a {@link EmbeddedDatabase} instance pre-populated with test data. When the database is returned, callers are
30+ * guaranteed that the database schema and test data will have already been loaded.
31+ * <p>
32+ * Can be configured:<br>
33+ * Call {@link #setDatabaseName(String)} to change the name of the database.<br>
34+ * Call {@link #setDatabaseType(EmbeddedDatabaseType)} to set the database type if you wish to use one of the supported types.<br>
35+ * Call {@link #setDatabaseConfigurer(EmbeddedDatabaseConfigurer)} to set a configuration strategy for your own embedded database type.<br>
36+ * Call {@link #setDatabasePopulator(DatabasePopulator)} to change the algorithm used to populate the database.<br>
37+ * Call {@link #setDataSourceFactory(DataSourceFactory)} to change the type of DataSource used to connect to the database.<br>
38+ * Call {@link #getDatabase()} to get the {@link EmbeddedDatabase} instance.<br>
3639 *
37- * Can be configured.
38- * Call {@link #setDatabaseName(String)} to change the name of the database.
39- * Call {@link #setDatabaseType(EmbeddedDatabaseType)} to set the database type if you wish to use one of the supported types.
40- * Call {@link #setDatabaseConfigurer(EmbeddedDatabaseConfigurer)} to set a configuration strategy for your own embedded database type.
41- * Call {@link #setDatabasePopulator(DatabasePopulator)} to change the algorithm used to populate the database.
42- * Call {@link #setDataSourceFactory(DataSourceFactory)} to change the type of DataSource used to connect to the database.
43- * Call {@link #getDatabase()} to get the {@link EmbeddedDatabase} instance.
4440 * @author Keith Donald
4541 */
4642public class EmbeddedDatabaseFactory {
@@ -50,36 +46,34 @@ public class EmbeddedDatabaseFactory {
5046 private String databaseName ;
5147
5248 private DataSourceFactory dataSourceFactory ;
53-
49+
5450 private EmbeddedDatabaseConfigurer databaseConfigurer ;
5551
5652 private DatabasePopulator databasePopulator ;
5753
5854 private DataSource dataSource ;
59-
55+
6056 /**
61- * Creates a default {@link EmbeddedDatabaseFactory}.
62- * Calling {@link #getDatabase()} will create a embedded HSQL database of name 'testdb'.
57+ * Creates a default {@link EmbeddedDatabaseFactory}. Calling {@link #getDatabase()} will create a embedded HSQL
58+ * database of name 'testdb'.
6359 */
6460 public EmbeddedDatabaseFactory () {
6561 setDatabaseName ("testdb" );
6662 setDatabaseType (EmbeddedDatabaseType .HSQL );
6763 setDataSourceFactory (new SimpleDriverDataSourceFactory ());
6864 }
69-
65+
7066 /**
71- * Sets the name of the database.
72- * Defaults to 'testdb'.
67+ * Sets the name of the database. Defaults to 'testdb'.
7368 * @param name of the test database
7469 */
7570 public void setDatabaseName (String name ) {
7671 Assert .notNull (name , "The testDatabaseName is required" );
7772 databaseName = name ;
7873 }
79-
74+
8075 /**
81- * Sets the type of embedded database to use.
82- * Call this when you wish to configure one of the pre-supported types.
76+ * Sets the type of embedded database to use. Call this when you wish to configure one of the pre-supported types.
8377 * Defaults to HSQL.
8478 * @param type the test database type
8579 */
@@ -88,27 +82,26 @@ public void setDatabaseType(EmbeddedDatabaseType type) {
8882 }
8983
9084 /**
91- * Sets the strategy that will be used to configure the embedded database instance.
92- * Call this when you wish to use an embedded database type not already supported.
85+ * Sets the strategy that will be used to configure the embedded database instance. Call this when you wish to use
86+ * an embedded database type not already supported.
9387 * @param configurer the embedded database configurer
9488 */
9589 public void setDatabaseConfigurer (EmbeddedDatabaseConfigurer configurer ) {
9690 this .databaseConfigurer = configurer ;
9791 }
98-
92+
9993 /**
100- * Sets the strategy that will be used to populate the embedded database.
101- * Defaults to null.
102- * @param populator the database populator
94+ * Sets the strategy that will be used to populate the embedded database. Defaults to null.
95+ * @param populator the database populator
10396 */
10497 public void setDatabasePopulator (DatabasePopulator populator ) {
10598 Assert .notNull (populator , "The DatabasePopulator is required" );
10699 databasePopulator = populator ;
107100 }
108101
109102 /**
110- * Sets the factory to use to create the DataSource instance that connects to the embedded database.
111- * Defaults to {@link SimpleDriverDataSourceFactory}.
103+ * Sets the factory to use to create the DataSource instance that connects to the embedded database. Defaults to
104+ * {@link SimpleDriverDataSourceFactory}.
112105 * @param dataSourceFactory the data source factory
113106 */
114107 public void setDataSourceFactory (DataSourceFactory dataSourceFactory ) {
@@ -129,7 +122,7 @@ public EmbeddedDatabase getDatabase() {
129122 }
130123
131124 // subclassing hooks
132-
125+
133126 protected void initDataSource () {
134127 // create the embedded database source first
135128 if (logger .isInfoEnabled ()) {
@@ -146,26 +139,27 @@ protected void initDataSource() {
146139 protected DataSource getDataSource () {
147140 return dataSource ;
148141 }
149-
142+
150143 protected void shutdownDataSource () {
151144 if (dataSource != null ) {
152145 databaseConfigurer .shutdown (dataSource );
153146 dataSource = null ;
154147 }
155148 }
156-
149+
157150 // internal helper methods
158151
159152 private void populateDatabase () {
160- TransactionTemplate template = new TransactionTemplate (new DataSourceTransactionManager (dataSource ));
161- template .execute (new TransactionCallbackWithoutResult () {
162- @ Override
163- protected void doInTransactionWithoutResult (TransactionStatus status ) {
164- databasePopulator .populate (new JdbcTemplate (dataSource ));
165- }
166- });
153+ Connection connection = JdbcUtils .getConnection (dataSource );
154+ try {
155+ databasePopulator .populate (connection );
156+ } catch (SQLException e ) {
157+ throw new RuntimeException ("SQLException occurred populating embedded database" , e );
158+ } finally {
159+ JdbcUtils .closeConnection (connection );
160+ }
167161 }
168-
162+
169163 private class EmbeddedDataSourceProxy implements EmbeddedDatabase {
170164 private DataSource dataSource ;
171165
@@ -177,8 +171,7 @@ public Connection getConnection() throws SQLException {
177171 return dataSource .getConnection ();
178172 }
179173
180- public Connection getConnection (String username , String password )
181- throws SQLException {
174+ public Connection getConnection (String username , String password ) throws SQLException {
182175 return dataSource .getConnection (username , password );
183176 }
184177
@@ -205,11 +198,11 @@ public boolean isWrapperFor(Class<?> iface) throws SQLException {
205198 public <T > T unwrap (Class <T > iface ) throws SQLException {
206199 return dataSource .unwrap (iface );
207200 }
208-
201+
209202 public void shutdown () {
210203 shutdownDataSource ();
211204 }
212205
213206 }
214-
207+
215208}
0 commit comments