Skip to content

Commit bc9ad56

Browse files
author
Keith Donald
committed
eliminated dependency on jdbc core
1 parent c0c9501 commit bc9ad56

File tree

11 files changed

+470
-135
lines changed

11 files changed

+470
-135
lines changed

eclipse-code-formatter.prefs

Lines changed: 267 additions & 0 deletions
Large diffs are not rendered by default.

org.springframework.jdbc/.settings/org.eclipse.jdt.core.prefs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#Thu Dec 18 06:35:47 PST 2008
1+
#Wed May 06 02:42:56 EDT 2009
22
eclipse.preferences.version=1
33
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
44
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6
@@ -57,15 +57,15 @@ org.eclipse.jdt.core.formatter.comment.clear_blank_lines_in_block_comment=false
5757
org.eclipse.jdt.core.formatter.comment.clear_blank_lines_in_javadoc_comment=false
5858
org.eclipse.jdt.core.formatter.comment.format_block_comments=true
5959
org.eclipse.jdt.core.formatter.comment.format_header=false
60-
org.eclipse.jdt.core.formatter.comment.format_html=true
60+
org.eclipse.jdt.core.formatter.comment.format_html=false
6161
org.eclipse.jdt.core.formatter.comment.format_javadoc_comments=true
6262
org.eclipse.jdt.core.formatter.comment.format_line_comments=true
6363
org.eclipse.jdt.core.formatter.comment.format_source_code=true
64-
org.eclipse.jdt.core.formatter.comment.indent_parameter_description=true
65-
org.eclipse.jdt.core.formatter.comment.indent_root_tags=true
66-
org.eclipse.jdt.core.formatter.comment.insert_new_line_before_root_tags=insert
67-
org.eclipse.jdt.core.formatter.comment.insert_new_line_for_parameter=insert
68-
org.eclipse.jdt.core.formatter.comment.line_length=80
64+
org.eclipse.jdt.core.formatter.comment.indent_parameter_description=false
65+
org.eclipse.jdt.core.formatter.comment.indent_root_tags=false
66+
org.eclipse.jdt.core.formatter.comment.insert_new_line_before_root_tags=do not insert
67+
org.eclipse.jdt.core.formatter.comment.insert_new_line_for_parameter=do not insert
68+
org.eclipse.jdt.core.formatter.comment.line_length=120
6969
org.eclipse.jdt.core.formatter.compact_else_if=true
7070
org.eclipse.jdt.core.formatter.continuation_indentation=2
7171
org.eclipse.jdt.core.formatter.continuation_indentation_for_array_initializer=2
@@ -256,7 +256,7 @@ org.eclipse.jdt.core.formatter.keep_else_statement_on_same_line=false
256256
org.eclipse.jdt.core.formatter.keep_empty_array_initializer_on_one_line=false
257257
org.eclipse.jdt.core.formatter.keep_imple_if_on_one_line=false
258258
org.eclipse.jdt.core.formatter.keep_then_statement_on_same_line=false
259-
org.eclipse.jdt.core.formatter.lineSplit=80
259+
org.eclipse.jdt.core.formatter.lineSplit=120
260260
org.eclipse.jdt.core.formatter.never_indent_block_comments_on_first_column=false
261261
org.eclipse.jdt.core.formatter.never_indent_line_comments_on_first_column=false
262262
org.eclipse.jdt.core.formatter.number_of_blank_lines_at_beginning_of_method_body=0
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#Thu Dec 18 06:35:47 PST 2008
1+
#Wed May 06 02:42:56 EDT 2009
22
eclipse.preferences.version=1
3-
formatter_profile=org.eclipse.jdt.ui.default.eclipse_profile
3+
formatter_profile=_Spring
44
formatter_settings_version=11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package org.springframework.jdbc.datasource.embedded;
2+
3+
import org.springframework.core.io.support.EncodedResource;
4+
5+
public class CannotReadScriptException extends RuntimeException {
6+
7+
public CannotReadScriptException(EncodedResource resource, Throwable cause) {
8+
super("Cannot read SQL script from " + resource, cause);
9+
}
10+
}

org.springframework.jdbc/src/main/java/org/springframework/jdbc/datasource/embedded/DatabasePopulator.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
*/
1616
package org.springframework.jdbc.datasource.embedded;
1717

18-
import org.springframework.dao.DataAccessException;
19-
import org.springframework.jdbc.core.JdbcTemplate;
18+
import java.sql.Connection;
19+
import java.sql.SQLException;
2020

2121
/**
2222
* Strategy for populating a database with data.
@@ -27,8 +27,8 @@ public interface DatabasePopulator {
2727

2828
/**
2929
* Populate the database using the JDBC-based data access template provided.
30-
* @param template the data access template to use to populate the db; already configured and ready to use
31-
* @throws DataAccessException if an unrecoverable data access exception occurs during database population
30+
* @param connection the JDBC connection to use to populate the db; already configured and ready to use
31+
* @throws SQLException if an unrecoverable data access exception occurs during database population
3232
*/
33-
void populate(JdbcTemplate template);
33+
void populate(Connection connection) throws SQLException;
3434
}

org.springframework.jdbc/src/main/java/org/springframework/jdbc/datasource/embedded/EmbeddedDatabaseFactory.java

Lines changed: 40 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -23,24 +23,20 @@
2323

2424
import org.apache.commons.logging.Log;
2525
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;
3126
import 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
*/
4642
public 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
}

org.springframework.jdbc/src/main/java/org/springframework/jdbc/datasource/embedded/HsqlEmbeddedDatabaseConfigurer.java

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,20 @@
1515
*/
1616
package org.springframework.jdbc.datasource.embedded;
1717

18+
import java.sql.Connection;
19+
import java.sql.SQLException;
20+
import java.sql.Statement;
21+
1822
import javax.sql.DataSource;
1923

20-
import org.springframework.jdbc.core.JdbcTemplate;
24+
import org.apache.commons.logging.Log;
25+
import org.apache.commons.logging.LogFactory;
2126
import org.springframework.util.ClassUtils;
2227

2328
public class HsqlEmbeddedDatabaseConfigurer implements EmbeddedDatabaseConfigurer {
2429

30+
private static final Log logger = LogFactory.getLog(HsqlEmbeddedDatabaseConfigurer.class);
31+
2532
private static HsqlEmbeddedDatabaseConfigurer INSTANCE;
2633

2734
public static synchronized HsqlEmbeddedDatabaseConfigurer getInstance() throws ClassNotFoundException {
@@ -40,7 +47,18 @@ public void configureConnectionProperties(ConnectionProperties properties, Strin
4047
}
4148

4249
public void shutdown(DataSource dataSource) {
43-
new JdbcTemplate(dataSource).execute("SHUTDOWN");
50+
Connection connection = JdbcUtils.getConnection(dataSource);
51+
Statement stmt = null;
52+
try {
53+
stmt = connection.createStatement();
54+
stmt.execute("SHUTDOWN");
55+
} catch (SQLException e) {
56+
if (logger.isWarnEnabled()) {
57+
logger.warn("Could not shutdown in-memory HSQL database", e);
58+
}
59+
} finally {
60+
JdbcUtils.closeStatement(stmt);
61+
}
4462
}
4563

4664
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package org.springframework.jdbc.datasource.embedded;
2+
3+
import java.sql.Connection;
4+
import java.sql.SQLException;
5+
import java.sql.Statement;
6+
7+
import javax.sql.DataSource;
8+
9+
import org.apache.commons.logging.Log;
10+
import org.apache.commons.logging.LogFactory;
11+
import org.springframework.jdbc.CannotGetJdbcConnectionException;
12+
13+
/**
14+
* Helper JDBC utilities used by other classes in this package. There is some duplication here with JdbcUtils in
15+
* jdbc.support package. We may want to consider simply using that. Package private for now.
16+
* @author Keith Donald
17+
*/
18+
final class JdbcUtils {
19+
20+
private static Log logger = LogFactory.getLog(EmbeddedDatabaseFactory.class);
21+
22+
private JdbcUtils() {
23+
24+
}
25+
26+
public static Connection getConnection(DataSource dataSource) {
27+
try {
28+
return dataSource.getConnection();
29+
} catch (SQLException ex) {
30+
throw new CannotGetJdbcConnectionException("Could not get JDBC Connection", ex);
31+
}
32+
}
33+
34+
public static void closeConnection(Connection connection) {
35+
if (connection != null) {
36+
try {
37+
connection.close();
38+
} catch (SQLException ex) {
39+
logger.debug("Could not close JDBC Connection", ex);
40+
} catch (Throwable ex) {
41+
// We don't trust the JDBC driver: It might throw RuntimeException or Error.
42+
logger.debug("Unexpected exception on closing JDBC Connection", ex);
43+
}
44+
}
45+
}
46+
47+
public static void closeStatement(Statement stmt) {
48+
if (stmt != null) {
49+
try {
50+
stmt.close();
51+
} catch (SQLException ex) {
52+
logger.debug("Could not close JDBC Statement", ex);
53+
} catch (Throwable ex) {
54+
// We don't trust the JDBC driver: It might throw RuntimeException or Error.
55+
logger.debug("Unexpected exception on closing JDBC Statement", ex);
56+
}
57+
}
58+
}
59+
}

0 commit comments

Comments
 (0)