23
23
24
24
import org .apache .commons .logging .Log ;
25
25
import 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 ;
31
26
import org .springframework .util .Assert ;
32
27
33
28
/**
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>
36
39
*
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.
44
40
* @author Keith Donald
45
41
*/
46
42
public class EmbeddedDatabaseFactory {
@@ -50,36 +46,34 @@ public class EmbeddedDatabaseFactory {
50
46
private String databaseName ;
51
47
52
48
private DataSourceFactory dataSourceFactory ;
53
-
49
+
54
50
private EmbeddedDatabaseConfigurer databaseConfigurer ;
55
51
56
52
private DatabasePopulator databasePopulator ;
57
53
58
54
private DataSource dataSource ;
59
-
55
+
60
56
/**
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'.
63
59
*/
64
60
public EmbeddedDatabaseFactory () {
65
61
setDatabaseName ("testdb" );
66
62
setDatabaseType (EmbeddedDatabaseType .HSQL );
67
63
setDataSourceFactory (new SimpleDriverDataSourceFactory ());
68
64
}
69
-
65
+
70
66
/**
71
- * Sets the name of the database.
72
- * Defaults to 'testdb'.
67
+ * Sets the name of the database. Defaults to 'testdb'.
73
68
* @param name of the test database
74
69
*/
75
70
public void setDatabaseName (String name ) {
76
71
Assert .notNull (name , "The testDatabaseName is required" );
77
72
databaseName = name ;
78
73
}
79
-
74
+
80
75
/**
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.
83
77
* Defaults to HSQL.
84
78
* @param type the test database type
85
79
*/
@@ -88,27 +82,26 @@ public void setDatabaseType(EmbeddedDatabaseType type) {
88
82
}
89
83
90
84
/**
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.
93
87
* @param configurer the embedded database configurer
94
88
*/
95
89
public void setDatabaseConfigurer (EmbeddedDatabaseConfigurer configurer ) {
96
90
this .databaseConfigurer = configurer ;
97
91
}
98
-
92
+
99
93
/**
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
103
96
*/
104
97
public void setDatabasePopulator (DatabasePopulator populator ) {
105
98
Assert .notNull (populator , "The DatabasePopulator is required" );
106
99
databasePopulator = populator ;
107
100
}
108
101
109
102
/**
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}.
112
105
* @param dataSourceFactory the data source factory
113
106
*/
114
107
public void setDataSourceFactory (DataSourceFactory dataSourceFactory ) {
@@ -129,7 +122,7 @@ public EmbeddedDatabase getDatabase() {
129
122
}
130
123
131
124
// subclassing hooks
132
-
125
+
133
126
protected void initDataSource () {
134
127
// create the embedded database source first
135
128
if (logger .isInfoEnabled ()) {
@@ -146,26 +139,27 @@ protected void initDataSource() {
146
139
protected DataSource getDataSource () {
147
140
return dataSource ;
148
141
}
149
-
142
+
150
143
protected void shutdownDataSource () {
151
144
if (dataSource != null ) {
152
145
databaseConfigurer .shutdown (dataSource );
153
146
dataSource = null ;
154
147
}
155
148
}
156
-
149
+
157
150
// internal helper methods
158
151
159
152
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
+ }
167
161
}
168
-
162
+
169
163
private class EmbeddedDataSourceProxy implements EmbeddedDatabase {
170
164
private DataSource dataSource ;
171
165
@@ -177,8 +171,7 @@ public Connection getConnection() throws SQLException {
177
171
return dataSource .getConnection ();
178
172
}
179
173
180
- public Connection getConnection (String username , String password )
181
- throws SQLException {
174
+ public Connection getConnection (String username , String password ) throws SQLException {
182
175
return dataSource .getConnection (username , password );
183
176
}
184
177
@@ -205,11 +198,11 @@ public boolean isWrapperFor(Class<?> iface) throws SQLException {
205
198
public <T > T unwrap (Class <T > iface ) throws SQLException {
206
199
return dataSource .unwrap (iface );
207
200
}
208
-
201
+
209
202
public void shutdown () {
210
203
shutdownDataSource ();
211
204
}
212
205
213
206
}
214
-
207
+
215
208
}
0 commit comments