polish / code review of data source embedded and init

This commit is contained in:
Keith Donald 2009-10-30 19:01:48 +00:00
parent 913bc03a3b
commit 3a3edb53f6
8 changed files with 53 additions and 42 deletions

View File

@ -18,6 +18,7 @@ package org.springframework.jdbc.datasource.embedded;
import java.sql.Driver; import java.sql.Driver;
/** /**
* DataSourceFactory helper that allows essential JDBC connection properties to be configured consistently, * DataSourceFactory helper that allows essential JDBC connection properties to be configured consistently,
* independent of the actual DataSource implementation. * independent of the actual DataSource implementation.
@ -32,7 +33,7 @@ public interface ConnectionProperties {
* Set the JDBC driver to use to connect to the database. * Set the JDBC driver to use to connect to the database.
* @param driverClass the jdbc driver class * @param driverClass the jdbc driver class
*/ */
void setDriverClass(Class driverClass); void setDriverClass(Class<? extends Driver> driverClass);
/** /**
* Sets the JDBC connection URL of the database. * Sets the JDBC connection URL of the database.

View File

@ -120,7 +120,7 @@ public class EmbeddedDatabaseBuilder {
* @param clazz the class to load relative to * @param clazz the class to load relative to
* @return the embedded database builder * @return the embedded database builder
*/ */
public static EmbeddedDatabaseBuilder relativeTo(Class clazz) { public static EmbeddedDatabaseBuilder relativeTo(Class<?> clazz) {
return new EmbeddedDatabaseBuilder(new ClassRelativeResourceLoader(clazz)); return new EmbeddedDatabaseBuilder(new ClassRelativeResourceLoader(clazz));
} }

View File

@ -25,6 +25,13 @@ package org.springframework.jdbc.datasource.embedded;
*/ */
public enum EmbeddedDatabaseType { 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
} }

View File

@ -16,6 +16,8 @@
package org.springframework.jdbc.datasource.embedded; package org.springframework.jdbc.datasource.embedded;
import java.sql.Driver;
import org.springframework.util.ClassUtils; import org.springframework.util.ClassUtils;
/** /**
@ -30,22 +32,23 @@ final class H2EmbeddedDatabaseConfigurer extends AbstractEmbeddedDatabaseConfigu
private static H2EmbeddedDatabaseConfigurer INSTANCE; private static H2EmbeddedDatabaseConfigurer INSTANCE;
private final Class<?> driverClass; private final Class<? extends Driver> driverClass;
/** /**
* Get the singleton {@link H2EmbeddedDatabaseConfigurer} instance. * Get the singleton {@link H2EmbeddedDatabaseConfigurer} instance.
* @return the configurer * @return the configurer
* @throws ClassNotFoundException if H2 is not on the classpath * @throws ClassNotFoundException if H2 is not on the classpath
*/ */
@SuppressWarnings("unchecked")
public static synchronized H2EmbeddedDatabaseConfigurer getInstance() throws ClassNotFoundException { public static synchronized H2EmbeddedDatabaseConfigurer getInstance() throws ClassNotFoundException {
if (INSTANCE == null) { if (INSTANCE == null) {
INSTANCE = new H2EmbeddedDatabaseConfigurer( INSTANCE = new H2EmbeddedDatabaseConfigurer(
ClassUtils.forName("org.h2.Driver", H2EmbeddedDatabaseConfigurer.class.getClassLoader())); (Class<? extends Driver>) ClassUtils.forName("org.h2.Driver", H2EmbeddedDatabaseConfigurer.class.getClassLoader()));
} }
return INSTANCE; return INSTANCE;
} }
private H2EmbeddedDatabaseConfigurer(Class<?> driverClass) { private H2EmbeddedDatabaseConfigurer(Class<? extends Driver> driverClass) {
this.driverClass = driverClass; this.driverClass = driverClass;
} }

View File

@ -16,6 +16,8 @@
package org.springframework.jdbc.datasource.embedded; package org.springframework.jdbc.datasource.embedded;
import java.sql.Driver;
import org.springframework.util.ClassUtils; import org.springframework.util.ClassUtils;
/** /**
@ -30,22 +32,23 @@ final class HsqlEmbeddedDatabaseConfigurer extends AbstractEmbeddedDatabaseConfi
private static HsqlEmbeddedDatabaseConfigurer INSTANCE; private static HsqlEmbeddedDatabaseConfigurer INSTANCE;
private final Class<?> driverClass; private final Class<? extends Driver> driverClass;
/** /**
* Get the singleton {@link HsqlEmbeddedDatabaseConfigurer} instance. * Get the singleton {@link HsqlEmbeddedDatabaseConfigurer} instance.
* @return the configurer * @return the configurer
* @throws ClassNotFoundException if HSQL is not on the classpath * @throws ClassNotFoundException if HSQL is not on the classpath
*/ */
@SuppressWarnings("unchecked")
public static synchronized HsqlEmbeddedDatabaseConfigurer getInstance() throws ClassNotFoundException { public static synchronized HsqlEmbeddedDatabaseConfigurer getInstance() throws ClassNotFoundException {
if (INSTANCE == null) { if (INSTANCE == null) {
INSTANCE = new HsqlEmbeddedDatabaseConfigurer( INSTANCE = new HsqlEmbeddedDatabaseConfigurer(
ClassUtils.forName("org.hsqldb.jdbcDriver", HsqlEmbeddedDatabaseConfigurer.class.getClassLoader())); (Class<? extends Driver>) ClassUtils.forName("org.hsqldb.jdbcDriver", HsqlEmbeddedDatabaseConfigurer.class.getClassLoader()));
} }
return INSTANCE; return INSTANCE;
} }
private HsqlEmbeddedDatabaseConfigurer(Class<?> driverClass) { private HsqlEmbeddedDatabaseConfigurer(Class<? extends Driver> driverClass) {
this.driverClass = driverClass; this.driverClass = driverClass;
} }

View File

@ -17,10 +17,10 @@
package org.springframework.jdbc.datasource.embedded; package org.springframework.jdbc.datasource.embedded;
import java.sql.Driver; import java.sql.Driver;
import javax.sql.DataSource; import javax.sql.DataSource;
import org.springframework.jdbc.datasource.SimpleDriverDataSource; import org.springframework.jdbc.datasource.SimpleDriverDataSource;
import org.springframework.util.Assert;
/** /**
* Creates a {@link SimpleDriverDataSource}. * Creates a {@link SimpleDriverDataSource}.
@ -35,17 +35,18 @@ final class SimpleDriverDataSourceFactory implements DataSourceFactory {
public ConnectionProperties getConnectionProperties() { public ConnectionProperties getConnectionProperties() {
return new ConnectionProperties() { return new ConnectionProperties() {
@SuppressWarnings("unchecked") public void setDriverClass(Class<? extends Driver> driverClass) {
public void setDriverClass(Class driverClass) { dataSource.setDriverClass(driverClass);
Assert.isAssignable(Driver.class, driverClass);
dataSource.setDriverClass((Class<? extends Driver>) driverClass);
} }
public void setUrl(String url) { public void setUrl(String url) {
dataSource.setUrl(url); dataSource.setUrl(url);
} }
public void setUsername(String username) { public void setUsername(String username) {
dataSource.setUsername(username); dataSource.setUsername(username);
} }
public void setPassword(String password) { public void setPassword(String password) {
dataSource.setPassword(password); dataSource.setPassword(password);
} }

View File

@ -20,7 +20,7 @@ import java.sql.Connection;
import java.sql.SQLException; import java.sql.SQLException;
/** /**
* Strategy used to populate an embedded database during initialization. * Strategy used to populate a database during initialization.
* *
* @author Keith Donald * @author Keith Donald
* @since 3.0 * @since 3.0
@ -29,7 +29,7 @@ import java.sql.SQLException;
public interface DatabasePopulator { 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 * @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 * @throws SQLException if an unrecoverable data access exception occurs during database population
*/ */

View File

@ -50,10 +50,10 @@ public class ResourceDatabasePopulator implements DatabasePopulator {
private String sqlScriptEncoding; private String sqlScriptEncoding;
private boolean ignoreFailedDrops = false;
private boolean continueOnError = false; private boolean continueOnError = false;
private boolean ignoreFailedDrops = false;
/** /**
* Add a script to execute to populate the database. * Add a script to execute to populate the database.
* @param script the path to a SQL script * @param script the path to a SQL script
@ -70,29 +70,6 @@ public class ResourceDatabasePopulator implements DatabasePopulator {
this.scripts = Arrays.asList(scripts); this.scripts = Arrays.asList(scripts);
} }
/**
* 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) {
this.continueOnError = continueOnError;
}
/**
* Flag to indicate that a failed SQL <code>DROP</code> statement can be ignored.
* This is useful for non-embedded databases whose SQL dialect does not support
* an <code>IF EXISTS</code> clause in a <code>DROP</code>. 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 <code>DROP</code>.
*
* @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. * 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 * Note setting this property has no effect on added scripts that are already
@ -103,9 +80,28 @@ public class ResourceDatabasePopulator implements DatabasePopulator {
this.sqlScriptEncoding = sqlScriptEncoding; this.sqlScriptEncoding = sqlScriptEncoding;
} }
/**
* 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) {
this.continueOnError = continueOnError;
}
/**
* Flag to indicate that a failed SQL <code>DROP</code> statement can be ignored.
* This is useful for non-embedded databases whose SQL dialect does not support an <code>IF EXISTS</code> clause in a <code>DROP</code>.
* The default is false so that if it the populator runs accidentally, it will failfast when the script starts with a <code>DROP</code>.
* @param ignoreFailedDrops the flag value to set
*/
public void setIgnoreFailedDrops(boolean ignoreFailedDrops) {
this.ignoreFailedDrops = ignoreFailedDrops;
}
public void populate(Connection connection) throws SQLException { public void populate(Connection connection) throws SQLException {
for (Resource script : this.scripts) { for (Resource script : this.scripts) {
executeSqlScript(connection, applyEncodingIfNecessary(script), continueOnError, ignoreFailedDrops); executeSqlScript(connection, applyEncodingIfNecessary(script), this.continueOnError, this.ignoreFailedDrops);
} }
} }