Class EmbeddedDatabaseBuilder
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();
- Since:
- 3.0
- Author:
- Keith Donald, Juergen Hoeller, Dave Syer, Sam Brannen
- See Also:
-
Constructor Summary
ConstructorDescriptionCreate a new embedded database builder with aDefaultResourceLoader
.EmbeddedDatabaseBuilder
(ResourceLoader resourceLoader) Create a new embedded database builder with the givenResourceLoader
. -
Method Summary
Modifier and TypeMethodDescriptionAdd default SQL scripts to execute to populate the database.Add an SQL script to execute to initialize or populate the database.addScripts
(String... scripts) Add multiple SQL scripts to execute to initialize or populate the database.build()
Build the embedded database.continueOnError
(boolean flag) Specify that all failures which occur while executing SQL scripts should be logged but should not cause a failure.generateUniqueName
(boolean flag) Specify whether a unique ID should be generated and used as the database name.ignoreFailedDrops
(boolean flag) Specify that a failed SQLDROP
statement within an executed script can be ignored.setBlockCommentEndDelimiter
(String blockCommentEndDelimiter) Specify the end delimiter for block comments in all SQL scripts.setBlockCommentStartDelimiter
(String blockCommentStartDelimiter) Specify the start delimiter for block comments in all SQL scripts.setCommentPrefix
(String commentPrefix) Specify the single-line comment prefix used in all SQL scripts.setCommentPrefixes
(String... commentPrefixes) Specify the prefixes that identify single-line comments within all SQL scripts.setDataSourceFactory
(DataSourceFactory dataSourceFactory) Set the factory to use to create theDataSource
instance that connects to the embedded database.Set the name of the embedded database.setScriptEncoding
(String scriptEncoding) Specify the character encoding used in all SQL scripts, if different from the platform encoding.setSeparator
(String separator) Specify the statement separator used in all SQL scripts, if a custom one.setType
(EmbeddedDatabaseType databaseType) Set the type of embedded database.
-
Constructor Details
-
EmbeddedDatabaseBuilder
public EmbeddedDatabaseBuilder()Create a new embedded database builder with aDefaultResourceLoader
. -
EmbeddedDatabaseBuilder
Create a new embedded database builder with the givenResourceLoader
.- Parameters:
resourceLoader
- theResourceLoader
to delegate to
-
-
Method Details
-
generateUniqueName
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(java.lang.String)
.- Parameters:
flag
-true
if a unique database name should be generated- Returns:
this
, to facilitate method chaining- Since:
- 4.2
- See Also:
-
setName
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 totrue
.- Parameters:
databaseName
- the name of the embedded database to build- Returns:
this
, to facilitate method chaining- See Also:
-
setType
Set the type of embedded database.Defaults to HSQL if not called.
- Parameters:
databaseType
- the type of embedded database to build- Returns:
this
, to facilitate method chaining
-
setDataSourceFactory
Set the factory to use to create theDataSource
instance that connects to the embedded database.Defaults to
SimpleDriverDataSourceFactory
but can be overridden, for example to introduce connection pooling.- Returns:
this
, to facilitate method chaining- Since:
- 4.0.3
-
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:
this
, to facilitate method chaining
-
addScript
Add an SQL script to execute to initialize or populate the database.- Parameters:
script
- the script to execute- Returns:
this
, to facilitate method chaining
-
addScripts
Add multiple SQL scripts to execute to initialize or populate the database.- Parameters:
scripts
- the scripts to execute- Returns:
this
, to facilitate method chaining- Since:
- 4.0.3
-
setScriptEncoding
Specify the character encoding used in all SQL scripts, if different from the platform encoding.- Parameters:
scriptEncoding
- the encoding used in scripts- Returns:
this
, to facilitate method chaining- Since:
- 4.0.3
-
setSeparator
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 toScriptUtils.EOF_STATEMENT_SEPARATOR
to signal that each script contains a single statement without a separator.- Parameters:
separator
- the statement separator- Returns:
this
, to facilitate method chaining- Since:
- 4.0.3
-
setCommentPrefix
Specify the single-line comment prefix used in all SQL scripts.Defaults to
"--"
.- Parameters:
commentPrefix
- the prefix for single-line comments- Returns:
this
, to facilitate method chaining- Since:
- 4.0.3
- See Also:
-
setCommentPrefixes
Specify the prefixes that identify single-line comments within all SQL scripts.Defaults to
["--"]
.- Parameters:
commentPrefixes
- the prefixes for single-line comments- Returns:
this
, to facilitate method chaining- Since:
- 5.2
-
setBlockCommentStartDelimiter
Specify the start delimiter for block comments in all SQL scripts.Defaults to
"/*"
.- Parameters:
blockCommentStartDelimiter
- the start delimiter for block comments- Returns:
this
, to facilitate method chaining- Since:
- 4.0.3
- See Also:
-
setBlockCommentEndDelimiter
Specify the end delimiter for block comments in all SQL scripts.Defaults to
"*/"
.- Parameters:
blockCommentEndDelimiter
- the end delimiter for block comments- Returns:
this
, to facilitate method chaining- Since:
- 4.0.3
- See Also:
-
continueOnError
Specify that all failures which occur while executing SQL scripts should be logged but should not cause a failure.Defaults to
false
.- Parameters:
flag
-true
if script execution should continue on error- Returns:
this
, to facilitate method chaining- Since:
- 4.0.3
-
ignoreFailedDrops
Specify that a failed SQLDROP
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 aDROP
statement.The default is
false
so thatbuilding
will fail fast if a script starts with aDROP
statement.- Parameters:
flag
-true
if failed drop statements should be ignored- Returns:
this
, to facilitate method chaining- Since:
- 4.0.3
-
build
Build the embedded database.- Returns:
- the embedded database
-