Top Description Fields Constructors Methods
org.springframework.jdbc.datasource.embedded

public Class EmbeddedDatabaseBuilder

extends Object
Class Inheritance
Imports
javax.sql.DataSource, org.springframework.core.io.DefaultResourceLoader, .ResourceLoader, org.springframework.jdbc.datasource.init.ResourceDatabasePopulator, .ScriptUtils, org.springframework.util.Assert

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();
Authors
Keith Donald, Juergen Hoeller, Dave Syer, Sam Brannen
Since
3.0
See Also
org.springframework.jdbc.datasource.init.ScriptUtils, org.springframework.jdbc.datasource.init.ResourceDatabasePopulator, org.springframework.jdbc.datasource.init.DatabasePopulatorUtils

Field Summary

Modifier and TypeField and Description
private final EmbeddedDatabaseFactory
private final ResourceDatabasePopulator
private final ResourceLoader

Constructor Summary

AccessConstructor and Description
public
EmbeddedDatabaseBuilder()

Create a new embedded database builder with a DefaultResourceLoader.

public
EmbeddedDatabaseBuilder(ResourceLoader
the ResourceLoader to delegate to
resourceLoader
)

Create a new embedded database builder with the given ResourceLoader.

Method Summary

Modifier and TypeMethod and Description
public EmbeddedDatabaseBuilder

Returns:

this, to facilitate method chaining
addDefaultScripts
()

Add default SQL scripts to execute to populate the database.

public EmbeddedDatabaseBuilder

Returns:

this, to facilitate method chaining
addScript
(String
the script to execute
script
)

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

public EmbeddedDatabaseBuilder

Returns:

this, to facilitate method chaining
addScripts
(String...
the scripts to execute
scripts
)

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

public EmbeddedDatabase

Returns:

the embedded database
build
()

Build the embedded database.

public EmbeddedDatabaseBuilder

Returns:

this, to facilitate method chaining
continueOnError
(boolean
true if script execution should continue on error
flag
)

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

public EmbeddedDatabaseBuilder

Returns:

this, to facilitate method chaining
generateUniqueName
(boolean
true if a unique database name should be generated
flag
)

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

public EmbeddedDatabaseBuilder

Returns:

this, to facilitate method chaining
ignoreFailedDrops
(boolean
true if failed drop statements should be ignored
flag
)

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

public EmbeddedDatabaseBuilder

Returns:

this, to facilitate method chaining
setBlockCommentEndDelimiter
(String
the end delimiter for block comments
blockCommentEndDelimiter
)

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

public EmbeddedDatabaseBuilder

Returns:

this, to facilitate method chaining
setBlockCommentStartDelimiter
(String
the start delimiter for block comments
blockCommentStartDelimiter
)

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

public EmbeddedDatabaseBuilder

Returns:

this, to facilitate method chaining
setCommentPrefix
(String
the prefix for single-line comments
commentPrefix
)

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

public EmbeddedDatabaseBuilder

Returns:

this, to facilitate method chaining
setCommentPrefixes
(String...
the prefixes for single-line comments
commentPrefixes
)

Specify the prefixes that identify single-line comments within all SQL scripts.

public EmbeddedDatabaseBuilder

Returns:

this, to facilitate method chaining
setDataSourceFactory
(DataSourceFactory dataSourceFactory)

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

public EmbeddedDatabaseBuilder

Returns:

this, to facilitate method chaining
setName
(String
the name of the embedded database to build
databaseName
)

Set the name of the embedded database.

public EmbeddedDatabaseBuilder

Returns:

this, to facilitate method chaining
setScriptEncoding
(String
the encoding used in scripts
scriptEncoding
)

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

public EmbeddedDatabaseBuilder

Returns:

this, to facilitate method chaining
setSeparator
(String
the statement separator
separator
)

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

public EmbeddedDatabaseBuilder

Returns:

this, to facilitate method chaining
setType
(EmbeddedDatabaseType
the type of embedded database to build
databaseType
)

Set the type of embedded database.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

databaseFactoryback to summary
private final EmbeddedDatabaseFactory databaseFactory
databasePopulatorback to summary
private final ResourceDatabasePopulator databasePopulator
resourceLoaderback to summary
private final ResourceLoader resourceLoader

Constructor Detail

EmbeddedDatabaseBuilderback to summary
public EmbeddedDatabaseBuilder()

Create a new embedded database builder with a DefaultResourceLoader.

EmbeddedDatabaseBuilderback to summary
public EmbeddedDatabaseBuilder(ResourceLoader resourceLoader)

Create a new embedded database builder with the given ResourceLoader.

Parameters
resourceLoader:ResourceLoader

the ResourceLoader to delegate to

Method Detail

addDefaultScriptsback to summary
public EmbeddedDatabaseBuilder addDefaultScripts()

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.

Returns:EmbeddedDatabaseBuilder

this, to facilitate method chaining

addScriptback to summary
public EmbeddedDatabaseBuilder addScript(String script)

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

Parameters
script:String

the script to execute

Returns:EmbeddedDatabaseBuilder

this, to facilitate method chaining

addScriptsback to summary
public EmbeddedDatabaseBuilder addScripts(String... scripts)

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

Parameters
scripts:String[]

the scripts to execute

Returns:EmbeddedDatabaseBuilder

this, to facilitate method chaining

Since
4.0.3
buildback to summary
public EmbeddedDatabase build()

Build the embedded database.

Returns:EmbeddedDatabase

the embedded database

continueOnErrorback to summary
public EmbeddedDatabaseBuilder continueOnError(boolean flag)

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

Defaults to false.

Parameters
flag:boolean

true if script execution should continue on error

Returns:EmbeddedDatabaseBuilder

this, to facilitate method chaining

Since
4.0.3
generateUniqueNameback to summary
public EmbeddedDatabaseBuilder generateUniqueName(boolean flag)

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.

Parameters
flag:boolean

true if a unique database name should be generated

Returns:EmbeddedDatabaseBuilder

this, to facilitate method chaining

Since
4.2
See Also
setName
ignoreFailedDropsback to summary
public EmbeddedDatabaseBuilder ignoreFailedDrops(boolean flag)

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.

Parameters
flag:boolean

true if failed drop statements should be ignored

Returns:EmbeddedDatabaseBuilder

this, to facilitate method chaining

Since
4.0.3
setBlockCommentEndDelimiterback to summary
public EmbeddedDatabaseBuilder setBlockCommentEndDelimiter(String blockCommentEndDelimiter)

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

Defaults to "*/".

Parameters
blockCommentEndDelimiter:String

the end delimiter for block comments

Returns:EmbeddedDatabaseBuilder

this, to facilitate method chaining

Since
4.0.3
See Also
setBlockCommentStartDelimiter
setBlockCommentStartDelimiterback to summary
public EmbeddedDatabaseBuilder setBlockCommentStartDelimiter(String blockCommentStartDelimiter)

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

Defaults to "/*".

Parameters
blockCommentStartDelimiter:String

the start delimiter for block comments

Returns:EmbeddedDatabaseBuilder

this, to facilitate method chaining

Since
4.0.3
See Also
setBlockCommentEndDelimiter
setCommentPrefixback to summary
public EmbeddedDatabaseBuilder setCommentPrefix(String commentPrefix)

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

Defaults to "--".

Parameters
commentPrefix:String

the prefix for single-line comments

Returns:EmbeddedDatabaseBuilder

this, to facilitate method chaining

Since
4.0.3
See Also
setCommentPrefixes(String...)
setCommentPrefixesback to summary
public EmbeddedDatabaseBuilder setCommentPrefixes(String... commentPrefixes)

Specify the prefixes that identify single-line comments within all SQL scripts.

Defaults to ["--"].

Parameters
commentPrefixes:String[]

the prefixes for single-line comments

Returns:EmbeddedDatabaseBuilder

this, to facilitate method chaining

Since
5.2
setDataSourceFactoryback to summary
public EmbeddedDatabaseBuilder setDataSourceFactory(DataSourceFactory dataSourceFactory)

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.

Returns:EmbeddedDatabaseBuilder

this, to facilitate method chaining

Since
4.0.3
setNameback to summary
public EmbeddedDatabaseBuilder setName(String databaseName)

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.

Parameters
databaseName:String

the name of the embedded database to build

Returns:EmbeddedDatabaseBuilder

this, to facilitate method chaining

See Also
generateUniqueName
setScriptEncodingback to summary
public EmbeddedDatabaseBuilder setScriptEncoding(String scriptEncoding)

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

Parameters
scriptEncoding:String

the encoding used in scripts

Returns:EmbeddedDatabaseBuilder

this, to facilitate method chaining

Since
4.0.3
setSeparatorback to summary
public EmbeddedDatabaseBuilder setSeparator(String separator)

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.

Parameters
separator:String

the statement separator

Returns:EmbeddedDatabaseBuilder

this, to facilitate method chaining

Since
4.0.3
setTypeback to summary
public EmbeddedDatabaseBuilder setType(EmbeddedDatabaseType databaseType)

Set the type of embedded database.

Defaults to HSQL if not called.

Parameters
databaseType:EmbeddedDatabaseType

the type of embedded database to build

Returns:EmbeddedDatabaseBuilder

this, to facilitate method chaining