The <literal>org.springframework.jdbc.datasource.embedded</literal> package provides support for embedded Java database engines.
Support for <ulinkurl="http://www.hsqldb.org">HSQL</ulink> and <ulinkurl="http://www.h2database.com">H2</ulink> is provided natively.
There is also an extensible API for plugging in new embedded database types and <classname>DataSource</classname> implementations.
</para>
<sectionid="jdbc-why-embedded-database">
<title>Why use a embedded database?</title>
<para>
An embedded database is useful during the development phase of a project due to its lightweight nature.
Ease of configuration, quick startup time, testability, and the ability to rapidly evolve SQL during development are just some of the benefits of using an embedded database.
</para>
</section>
<sectionid="jdbc-embedded-database-xml">
<title>Creating an embedded database instance using Spring XML</title>
<para>
When you wish to expose an embedded database instance as a bean in a Spring ApplicationContext, use the embedded-database tag in the spring-jdbc namespace:
<programlistinglanguage="xml"><![CDATA[
<jdbc:embedded-databaseid="dataSource">
<jdbc:scriptlocation="classpath:schema.sql"/>
<jdbc:scriptlocation="classpath:test-data.sql"/>
</jdbc:embedded-database>]]>
</programlisting>
</para>
<para>
The configuration above 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 <classname>javax.sql.DataSource</classname>.
This bean can then be injected into data access objects as needed.
</para>
</section>
<sectionid="jdbc-embedded-database-java">
<title>
Creating an embedded database instance programatically
</title>
<para>
The <classname>EmbeddedDatabaseBuilder</classname> 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:
<programlistinglanguage="java"><![CDATA[
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()]]>
</programlisting>
</para>
</section>
<sectionid="jdbc-embedded-database-extension">
<title>Extending the embedded database support</title>
<para>
Spring Jdbc's embedded database support can be extended in two ways:
<orderedlist>
<listitem>
<para>Implement <classname>EmbeddedDatabaseConfigurer</classname> to support a new embedded database type, such as Apache Derby.</para>
</listitem>
<listitem>
<para>Implement <classname>DataSourceFactory</classname> to support a new DataSource implementation, such as a connection pool, to manage embedded database connections.</para>
</listitem>
</orderedlist>
</para>
<para>
You are encouraged to contribute back extensions to the Spring community at <ulinkurl="jira.springframework.org">jira.springframework.org</ulink>.