spring-framework / org.springframework.jdbc.datasource.embedded / EmbeddedDatabaseBuilder

EmbeddedDatabaseBuilder

open class EmbeddedDatabaseBuilder

A builder that provides a convenient API for constructing an embedded database. Usage Example

 EmbeddedDatabase db = new EmbeddedDatabaseBuilder() .generateUniqueName(true) .setType(H2) .setScriptEncoding("UTF-8") .ignoreFailedDrops(true) .addScript("schema.sql") .addScripts("user_data.sql", "country_data.sql") .build(); // perform actions against the db (EmbeddedDatabase extends javax.sql.DataSource) db.shutdown(); 

Author
Keith Donald

Author
Juergen Hoeller

Author
Dave Syer

Author
Sam Brannen

Since
3.0

See Also
org.springframework.jdbc.datasource.init.ScriptUtilsorg.springframework.jdbc.datasource.init.ResourceDatabasePopulatororg.springframework.jdbc.datasource.init.DatabasePopulatorUtils

Constructors

<init>

EmbeddedDatabaseBuilder()

Create a new embedded database builder with a DefaultResourceLoader.

EmbeddedDatabaseBuilder(resourceLoader: ResourceLoader)

Create a new embedded database builder with the given ResourceLoader.

Functions

addDefaultScripts

open fun addDefaultScripts(): EmbeddedDatabaseBuilder

Add default SQL scripts to execute to populate the database.

The default scripts are "schema.sql" to create the database schema and "data.sql" to populate the database with data.

addScript

open fun addScript(script: String): EmbeddedDatabaseBuilder

Add an SQL script to execute to initialize or populate the database.

addScripts

open fun addScripts(vararg scripts: String): EmbeddedDatabaseBuilder

Add multiple SQL scripts to execute to initialize or populate the database.

build

open fun build(): EmbeddedDatabase

Build the embedded database.

continueOnError

open fun continueOnError(flag: Boolean): EmbeddedDatabaseBuilder

Specify that all failures which occur while executing SQL scripts should be logged but should not cause a failure.

Defaults to false.

generateUniqueName

open fun generateUniqueName(flag: Boolean): EmbeddedDatabaseBuilder

Specify whether a unique ID should be generated and used as the database name.

If the configuration for this builder is reused across multiple application contexts within a single JVM, this flag should be enabled (i.e., set to true) in order to ensure that each application context gets its own embedded database.

Enabling this flag overrides any explicit name set via #setName.

ignoreFailedDrops

open fun ignoreFailedDrops(flag: Boolean): EmbeddedDatabaseBuilder

Specify that a failed SQL DROP statement within an executed script can be ignored.

This is useful for a database whose SQL dialect does not support an IF EXISTS clause in a DROP statement.

The default is false so that building will fail fast if a script starts with a DROP statement.

setBlockCommentEndDelimiter

open fun setBlockCommentEndDelimiter(blockCommentEndDelimiter: String): EmbeddedDatabaseBuilder

Specify the end delimiter for block comments in all SQL scripts.

Defaults to "*/".

setBlockCommentStartDelimiter

open fun setBlockCommentStartDelimiter(blockCommentStartDelimiter: String): EmbeddedDatabaseBuilder

Specify the start delimiter for block comments in all SQL scripts.

Defaults to "/*".

setCommentPrefix

open fun setCommentPrefix(commentPrefix: String): EmbeddedDatabaseBuilder

Specify the single-line comment prefix used in all SQL scripts.

Defaults to "--".

setDataSourceFactory

open fun setDataSourceFactory(dataSourceFactory: DataSourceFactory): EmbeddedDatabaseBuilder

Set the factory to use to create the DataSource instance that connects to the embedded database.

Defaults to SimpleDriverDataSourceFactory but can be overridden, for example to introduce connection pooling.

setName

open fun setName(databaseName: String): EmbeddedDatabaseBuilder

Set the name of the embedded database.

Defaults to EmbeddedDatabaseFactory#DEFAULT_DATABASE_NAME if not called.

Will be overridden if the generateUniqueName flag has been set to true.

setScriptEncoding

open fun setScriptEncoding(scriptEncoding: String): EmbeddedDatabaseBuilder

Specify the character encoding used in all SQL scripts, if different from the platform encoding.

setSeparator

open fun setSeparator(separator: String): EmbeddedDatabaseBuilder

Specify the statement separator used in all SQL scripts, if a custom one.

Defaults to ";" if not specified and falls back to "\n" as a last resort; may be set to ScriptUtils#EOF_STATEMENT_SEPARATOR to signal that each script contains a single statement without a separator.

setType

open fun setType(databaseType: EmbeddedDatabaseType): EmbeddedDatabaseBuilder

Set the type of embedded database.

Defaults to HSQL if not called.