diff --git a/org.springframework.jdbc/src/main/java/org/springframework/jdbc/datasource/embedded/ConnectionProperties.java b/org.springframework.jdbc/src/main/java/org/springframework/jdbc/datasource/embedded/ConnectionProperties.java index a28d7dee1c6..b97bec09676 100644 --- a/org.springframework.jdbc/src/main/java/org/springframework/jdbc/datasource/embedded/ConnectionProperties.java +++ b/org.springframework.jdbc/src/main/java/org/springframework/jdbc/datasource/embedded/ConnectionProperties.java @@ -18,6 +18,7 @@ package org.springframework.jdbc.datasource.embedded; import java.sql.Driver; + /** * DataSourceFactory helper that allows essential JDBC connection properties to be configured consistently, * independent of the actual DataSource implementation. @@ -32,7 +33,7 @@ public interface ConnectionProperties { * Set the JDBC driver to use to connect to the database. * @param driverClass the jdbc driver class */ - void setDriverClass(Class driverClass); + void setDriverClass(Class driverClass); /** * Sets the JDBC connection URL of the database. diff --git a/org.springframework.jdbc/src/main/java/org/springframework/jdbc/datasource/embedded/EmbeddedDatabaseBuilder.java b/org.springframework.jdbc/src/main/java/org/springframework/jdbc/datasource/embedded/EmbeddedDatabaseBuilder.java index 5870e01d5cd..5b0e224294c 100644 --- a/org.springframework.jdbc/src/main/java/org/springframework/jdbc/datasource/embedded/EmbeddedDatabaseBuilder.java +++ b/org.springframework.jdbc/src/main/java/org/springframework/jdbc/datasource/embedded/EmbeddedDatabaseBuilder.java @@ -120,7 +120,7 @@ public class EmbeddedDatabaseBuilder { * @param clazz the class to load relative to * @return the embedded database builder */ - public static EmbeddedDatabaseBuilder relativeTo(Class clazz) { + public static EmbeddedDatabaseBuilder relativeTo(Class clazz) { return new EmbeddedDatabaseBuilder(new ClassRelativeResourceLoader(clazz)); } diff --git a/org.springframework.jdbc/src/main/java/org/springframework/jdbc/datasource/embedded/EmbeddedDatabaseType.java b/org.springframework.jdbc/src/main/java/org/springframework/jdbc/datasource/embedded/EmbeddedDatabaseType.java index 0a2a7650f68..d70884c32f9 100644 --- a/org.springframework.jdbc/src/main/java/org/springframework/jdbc/datasource/embedded/EmbeddedDatabaseType.java +++ b/org.springframework.jdbc/src/main/java/org/springframework/jdbc/datasource/embedded/EmbeddedDatabaseType.java @@ -25,6 +25,13 @@ package org.springframework.jdbc.datasource.embedded; */ public enum EmbeddedDatabaseType { - HSQL, H2, DERBY + /** The Hypersonic Embedded Java SQL Database (http://hsqldb.org) */ + HSQL, + + /** The H2 Embedded Java SQL Database Engine (http://h2database.com) */ + H2, + + /** The Apache Derby Embedded SQL Database (http://db.apache.org/derby) */ + DERBY } diff --git a/org.springframework.jdbc/src/main/java/org/springframework/jdbc/datasource/embedded/H2EmbeddedDatabaseConfigurer.java b/org.springframework.jdbc/src/main/java/org/springframework/jdbc/datasource/embedded/H2EmbeddedDatabaseConfigurer.java index c0935524eaf..d7fc50bf7af 100644 --- a/org.springframework.jdbc/src/main/java/org/springframework/jdbc/datasource/embedded/H2EmbeddedDatabaseConfigurer.java +++ b/org.springframework.jdbc/src/main/java/org/springframework/jdbc/datasource/embedded/H2EmbeddedDatabaseConfigurer.java @@ -16,6 +16,8 @@ package org.springframework.jdbc.datasource.embedded; +import java.sql.Driver; + import org.springframework.util.ClassUtils; /** @@ -30,22 +32,23 @@ final class H2EmbeddedDatabaseConfigurer extends AbstractEmbeddedDatabaseConfigu private static H2EmbeddedDatabaseConfigurer INSTANCE; - private final Class driverClass; + private final Class driverClass; /** * Get the singleton {@link H2EmbeddedDatabaseConfigurer} instance. * @return the configurer * @throws ClassNotFoundException if H2 is not on the classpath */ + @SuppressWarnings("unchecked") public static synchronized H2EmbeddedDatabaseConfigurer getInstance() throws ClassNotFoundException { if (INSTANCE == null) { INSTANCE = new H2EmbeddedDatabaseConfigurer( - ClassUtils.forName("org.h2.Driver", H2EmbeddedDatabaseConfigurer.class.getClassLoader())); + (Class) ClassUtils.forName("org.h2.Driver", H2EmbeddedDatabaseConfigurer.class.getClassLoader())); } return INSTANCE; } - private H2EmbeddedDatabaseConfigurer(Class driverClass) { + private H2EmbeddedDatabaseConfigurer(Class driverClass) { this.driverClass = driverClass; } diff --git a/org.springframework.jdbc/src/main/java/org/springframework/jdbc/datasource/embedded/HsqlEmbeddedDatabaseConfigurer.java b/org.springframework.jdbc/src/main/java/org/springframework/jdbc/datasource/embedded/HsqlEmbeddedDatabaseConfigurer.java index 89e6a4d6a68..298cf37db00 100644 --- a/org.springframework.jdbc/src/main/java/org/springframework/jdbc/datasource/embedded/HsqlEmbeddedDatabaseConfigurer.java +++ b/org.springframework.jdbc/src/main/java/org/springframework/jdbc/datasource/embedded/HsqlEmbeddedDatabaseConfigurer.java @@ -16,6 +16,8 @@ package org.springframework.jdbc.datasource.embedded; +import java.sql.Driver; + import org.springframework.util.ClassUtils; /** @@ -30,22 +32,23 @@ final class HsqlEmbeddedDatabaseConfigurer extends AbstractEmbeddedDatabaseConfi private static HsqlEmbeddedDatabaseConfigurer INSTANCE; - private final Class driverClass; + private final Class driverClass; /** * Get the singleton {@link HsqlEmbeddedDatabaseConfigurer} instance. * @return the configurer * @throws ClassNotFoundException if HSQL is not on the classpath */ + @SuppressWarnings("unchecked") public static synchronized HsqlEmbeddedDatabaseConfigurer getInstance() throws ClassNotFoundException { if (INSTANCE == null) { INSTANCE = new HsqlEmbeddedDatabaseConfigurer( - ClassUtils.forName("org.hsqldb.jdbcDriver", HsqlEmbeddedDatabaseConfigurer.class.getClassLoader())); + (Class) ClassUtils.forName("org.hsqldb.jdbcDriver", HsqlEmbeddedDatabaseConfigurer.class.getClassLoader())); } return INSTANCE; } - private HsqlEmbeddedDatabaseConfigurer(Class driverClass) { + private HsqlEmbeddedDatabaseConfigurer(Class driverClass) { this.driverClass = driverClass; } diff --git a/org.springframework.jdbc/src/main/java/org/springframework/jdbc/datasource/embedded/SimpleDriverDataSourceFactory.java b/org.springframework.jdbc/src/main/java/org/springframework/jdbc/datasource/embedded/SimpleDriverDataSourceFactory.java index 89d83823be6..4fdb38e8cdb 100644 --- a/org.springframework.jdbc/src/main/java/org/springframework/jdbc/datasource/embedded/SimpleDriverDataSourceFactory.java +++ b/org.springframework.jdbc/src/main/java/org/springframework/jdbc/datasource/embedded/SimpleDriverDataSourceFactory.java @@ -17,10 +17,10 @@ package org.springframework.jdbc.datasource.embedded; import java.sql.Driver; + import javax.sql.DataSource; import org.springframework.jdbc.datasource.SimpleDriverDataSource; -import org.springframework.util.Assert; /** * Creates a {@link SimpleDriverDataSource}. @@ -35,17 +35,18 @@ final class SimpleDriverDataSourceFactory implements DataSourceFactory { public ConnectionProperties getConnectionProperties() { return new ConnectionProperties() { - @SuppressWarnings("unchecked") - public void setDriverClass(Class driverClass) { - Assert.isAssignable(Driver.class, driverClass); - dataSource.setDriverClass((Class) driverClass); + public void setDriverClass(Class driverClass) { + dataSource.setDriverClass(driverClass); } + public void setUrl(String url) { dataSource.setUrl(url); } + public void setUsername(String username) { dataSource.setUsername(username); } + public void setPassword(String password) { dataSource.setPassword(password); } diff --git a/org.springframework.jdbc/src/main/java/org/springframework/jdbc/datasource/init/DatabasePopulator.java b/org.springframework.jdbc/src/main/java/org/springframework/jdbc/datasource/init/DatabasePopulator.java index 5f7881ca319..7f4102cb64c 100644 --- a/org.springframework.jdbc/src/main/java/org/springframework/jdbc/datasource/init/DatabasePopulator.java +++ b/org.springframework.jdbc/src/main/java/org/springframework/jdbc/datasource/init/DatabasePopulator.java @@ -20,7 +20,7 @@ import java.sql.Connection; import java.sql.SQLException; /** - * Strategy used to populate an embedded database during initialization. + * Strategy used to populate a database during initialization. * * @author Keith Donald * @since 3.0 @@ -29,7 +29,7 @@ import java.sql.SQLException; public interface DatabasePopulator { /** - * Populate the database using the JDBC-based data access template provided. + * Populate the database using the JDBC connection provided. * @param connection the JDBC connection to use to populate the db; already configured and ready to use * @throws SQLException if an unrecoverable data access exception occurs during database population */ diff --git a/org.springframework.jdbc/src/main/java/org/springframework/jdbc/datasource/init/ResourceDatabasePopulator.java b/org.springframework.jdbc/src/main/java/org/springframework/jdbc/datasource/init/ResourceDatabasePopulator.java index 8813850a64b..c015e9bf8fd 100644 --- a/org.springframework.jdbc/src/main/java/org/springframework/jdbc/datasource/init/ResourceDatabasePopulator.java +++ b/org.springframework.jdbc/src/main/java/org/springframework/jdbc/datasource/init/ResourceDatabasePopulator.java @@ -49,11 +49,11 @@ public class ResourceDatabasePopulator implements DatabasePopulator { private List scripts = new ArrayList(); private String sqlScriptEncoding; + + private boolean continueOnError = false; private boolean ignoreFailedDrops = false; - private boolean continueOnError = false; - /** * Add a script to execute to populate the database. * @param script the path to a SQL script @@ -69,11 +69,20 @@ public class ResourceDatabasePopulator implements DatabasePopulator { public void setScripts(Resource[] scripts) { this.scripts = Arrays.asList(scripts); } - + + /** + * Specify the encoding for SQL scripts, if different from the platform encoding. + * Note setting this property has no effect on added scripts that are already + * {@link EncodedResource encoded resources}. + * @see #addScript(Resource) + */ + public void setSqlScriptEncoding(String sqlScriptEncoding) { + this.sqlScriptEncoding = sqlScriptEncoding; + } + /** - * Flag to indicate that all failures in SQL should be logged but not cause a - * failure. Defaults to false. - * + * Flag to indicate that all failures in SQL should be logged but not cause a failure. + * Defaults to false. * @param continueOnError the flag value to set */ public void setContinueOnError(boolean continueOnError) { @@ -82,30 +91,17 @@ public class ResourceDatabasePopulator implements DatabasePopulator { /** * Flag to indicate that a failed SQL DROP statement can be ignored. - * This is useful for non-embedded databases whose SQL dialect does not support - * an IF EXISTS clause in a DROP. The default is false - * so that if it the populator runs accidentally against an existing database it - * will fail fast when the script starts with a DROP. - * + * This is useful for non-embedded databases whose SQL dialect does not support an IF EXISTS clause in a DROP. + * The default is false so that if it the populator runs accidentally, it will failfast when the script starts with a DROP. * @param ignoreFailedDrops the flag value to set */ public void setIgnoreFailedDrops(boolean ignoreFailedDrops) { this.ignoreFailedDrops = ignoreFailedDrops; } - - /** - * Specify the encoding for SQL scripts, if different from the platform encoding. - * Note setting this property has no effect on added scripts that are already - * {@link EncodedResource encoded resources}. - * @see #addScript(Resource) - */ - public void setSqlScriptEncoding(String sqlScriptEncoding) { - this.sqlScriptEncoding = sqlScriptEncoding; - } - + public void populate(Connection connection) throws SQLException { for (Resource script : this.scripts) { - executeSqlScript(connection, applyEncodingIfNecessary(script), continueOnError, ignoreFailedDrops); + executeSqlScript(connection, applyEncodingIfNecessary(script), this.continueOnError, this.ignoreFailedDrops); } }