The org.springframework.jdbc.datasource.embedded
package provides support for embedded Java database engines. Support for
HSQL, H2, and Derby is provided natively. You
can also use an extensible API to plug in new embedded database types and
DataSource
implementations.
An embedded database is useful during the development phase of a project because of its lightweight nature. Benefits include ease of configuration, quick startup time, testability, and the ability to rapidly evolve SQL during development.
If you want to expose an embedded database instance as a bean in a Spring ApplicationContext, use the embedded-database tag in the spring-jdbc namespace:
<jdbc:embedded-database id="dataSource"> <jdbc:script location="classpath:schema.sql"/> <jdbc:script location="classpath:test-data.sql"/> </jdbc:embedded-database>
The preceding configuration creates an embedded HSQL database
populated with SQL from schema.sql and testdata.sql resources in the
classpath. The database instance is made available to the Spring
container as a bean of type javax.sql.DataSource
.
This bean can then be injected into data access objects as
needed.
The EmbeddedDatabaseBuilder
class provides
a fluent API for constructing an embedded database programmatically. Use
this when you need to create an embedded database instance in a
standalone environment, such as a data access object unit test:
EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder(); EmbeddedDatabase db = builder.type(H2).script("schema.sql").script("test-data.sql").build(); // do stuff against the db (EmbeddedDatabase extends javax.sql.DataSource) db.shutdown()
Spring JDBC embedded database support can be extended in two ways:
Implement EmbeddedDatabaseConfigurer
to support a new embedded database type, such as Apache
Derby.
Implement DataSourceFactory
to
support a new DataSource implementation, such as a connection
pool, to manage embedded database connections.
You are encouraged to contribute back extensions to the Spring community at jira.springframework.org.
Spring supports HSQL 1.0.0 to 2.0.0. HSQL is the default embedded
database if no type is specified explicitly. To specify HSQL explicitly,
set
the type
attribute of the
embedded-database
tag to HSQL
. If
you are using the builder API, call the
type(EmbeddedDatabaseType)
method with
EmbeddedDatabaseType.HSQL
.
Spring supports H2 1.8.0 to 2.0.0. To enable H2, set the
type
attribute of the
embedded-database
tag to H2
. If
you are using the builder API, call the
type(EmbeddedDatabaseType)
method with
EmbeddedDatabaseType.H2
.
Spring supports Apache Derby 10.5.1.1 to 10.6.0. To enable Derby,
set the type
attribute of the
embedded-database
tag to Derby
. If
using the builder API, call the
type(EmbeddedDatabaseType)
method with
EmbeddedDatabaseType.Derby
.
Embedded databases provide a lightweight way to test data access code. The following is a data access unit test template that uses an embedded database:
public class DataAccessUnitTestTemplate { private EmbeddedDatabase db; @Before public void setUp() { // creates a HSQL in-memory db populated from classpath:schema.sql and classpath:test-data.sql db = EmbeddedDatabaseBuilder.buildDefault(); } @Test public void testDataAccess() { JdbcTemplate template = new JdbcTemplate(db); template.query(...); } @After public void tearDown() { db.shutdown(); } }