Using Spring 3.0 To Create An Embedded In-Memory Database In Your Java Application
Introduction
At a Spring training conference, I learned about a new feature in Spring 3.0 that makes it very easy to create an embedded database to run in your Java application. The Spring 3.0 documentation provides a good summary of why developers may want to use an embedded database:
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. [Reference 1, section 12.8.1]
By default Spring supports these databases for embedding:
- HSQL (default), http://hsqldb.org/
- H2, http://www.h2database.com/html/main.html
- Derby, http://db.apache.org/derby/
Example Application
You can download an example application that uses the new Spring 3.0 embedded database methodology to create an in-memory database as part of a Spring Java application. The project was created using Eclipse 3.5 (with the Maven 2 plugin). You should be able to import directly into Eclipse the downloaded archive file.
If you don't have Eclipse 3.5 with the Maven 2 plugin, you can unzip the archived project and view the source code in any text editor. You can also run the application if you have Maven installed by executing the following commands (be sure to navigate in your terminal (command) window to where you unzipped the archived project, you should be in the same directory as the pom.xml file)
mvn clean
mvn compile
mvn exec:java
This example application is based upon an earlier example from the Spring JDBC article I wrote.
If you want to use the Spring 3.0 jars in your own Maven application you'll need to include these dependencies (Spring 3.0 was released in December 2009) in your pom.xml.
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>3.0.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>3.0.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>3.0.0.RELEASE</version>
</dependency>
How To Create The Embedded Database
In your Spring application context XML file have the following:
<jdbc:embedded-database id="dataSource" >
<jdbc:script location="classpath:schema.sql"/>
<jdbc:script location="classpath:test-data.sql"/>
</jdbc:embedded-database>
The above code tells Spring to create an embedded database using the default HSQL database. If you wanted to use H2 or Derby then you could use the type attribute of the embedded-database tag.
The two jdbc:script tags specify the database schema that should be created and then the SQL to be run against that schema to populate the initial records.
In the example application, the Spring application context file is named applicationContext.xml and it can be found in the src/main/resources folder.
The id value above specifies the name of the data source that can be used to get a connection to to the embedded database Spring created. Like other data source objects you can have Spring inject this data source into your data access objects. For example:
<bean id="personDao" class="edu.ku.it.si.springjdbcexamples.dao.PersonDaoImplDB">
<property name="dataSource" ref="dataSource" />
</bean>
jdbc namespace
New in Spring 3.0 is the jdbc namespace. To use the jdbc namespace you'll need to include this line:
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
in your list of namespaces. You should also include the schema location:
http://www.springframework.org/schema/jdbc
http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
Summary
The new jdbc namespace and its embedded-database tag provide a very easy way to have Spring create a database that can be used during development for testing and prototyping. Developers can use various versions of the schema.sql and test-data.sql files to setup certain conditions for testing. The other advantage of the embedded in-memory database is that when the application or test is rerun the database will naturally start off in its original state.
References
- Spring 3.0 Documentation, Embedded database support, http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/ch12s08.html
- Spring 3.0 API, embedded package, http://static.springsource.org/spring/docs/3.0.0.RC1/javadoc-api/org/springframework/jdbc/datasource/embedded/package-summary.html
- Example Application (archived Eclipse project using Maven), /spring/SpringJDBCExamplesEmbeddedDatabase.zip
There are no comments for this entry.
[Add Comment]