polish / code review of data source embedded and init
This commit is contained in:
parent
913bc03a3b
commit
3a3edb53f6
|
|
@ -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<? extends Driver> driverClass);
|
||||
|
||||
/**
|
||||
* Sets the JDBC connection URL of the database.
|
||||
|
|
|
|||
|
|
@ -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));
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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<? extends Driver> 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<? extends Driver>) ClassUtils.forName("org.h2.Driver", H2EmbeddedDatabaseConfigurer.class.getClassLoader()));
|
||||
}
|
||||
return INSTANCE;
|
||||
}
|
||||
|
||||
private H2EmbeddedDatabaseConfigurer(Class<?> driverClass) {
|
||||
private H2EmbeddedDatabaseConfigurer(Class<? extends Driver> driverClass) {
|
||||
this.driverClass = driverClass;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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<? extends Driver> 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<? extends Driver>) ClassUtils.forName("org.hsqldb.jdbcDriver", HsqlEmbeddedDatabaseConfigurer.class.getClassLoader()));
|
||||
}
|
||||
return INSTANCE;
|
||||
}
|
||||
|
||||
private HsqlEmbeddedDatabaseConfigurer(Class<?> driverClass) {
|
||||
private HsqlEmbeddedDatabaseConfigurer(Class<? extends Driver> driverClass) {
|
||||
this.driverClass = driverClass;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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<? extends Driver>) driverClass);
|
||||
public void setDriverClass(Class<? extends Driver> 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);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
*/
|
||||
|
|
|
|||
|
|
@ -50,10 +50,10 @@ public class ResourceDatabasePopulator implements DatabasePopulator {
|
|||
|
||||
private String sqlScriptEncoding;
|
||||
|
||||
private boolean ignoreFailedDrops = false;
|
||||
|
||||
private boolean continueOnError = false;
|
||||
|
||||
private boolean ignoreFailedDrops = false;
|
||||
|
||||
/**
|
||||
* Add a script to execute to populate the database.
|
||||
* @param script the path to a SQL script
|
||||
|
|
@ -70,29 +70,6 @@ public class ResourceDatabasePopulator implements DatabasePopulator {
|
|||
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.
|
||||
* 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;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 {
|
||||
for (Resource script : this.scripts) {
|
||||
executeSqlScript(connection, applyEncodingIfNecessary(script), continueOnError, ignoreFailedDrops);
|
||||
executeSqlScript(connection, applyEncodingIfNecessary(script), this.continueOnError, this.ignoreFailedDrops);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue