Using embedded databases

For prototyping and test environments, it is a good idea to use Java-based embedded databases to quickly start up the project and configure easily. They are lightweight and easily testable. Spring supports the HSQL, H2, and Derby database engines for that purpose natively. Here is a sample DataSource configuration for an embedded HSQL database:

@Bean
DataSource getHsqlDatasource() {
    return new 
        EmbeddedDatabaseBuilder().setType(EmbeddedDatabaseType.HSQL)
         .addScript("db-scripts/hsql/db-schema.sql")
         .addScript("db-scripts/hsql/data.sql")
         .addScript("db-scripts/hsql/storedprocs.sql")
         .addScript("db-scripts/hsql/functions.sql")
         .setSeparator("/").build();
}

The XML version of this would look like the following code:

<jdbc:embedded-database ...

Get Spring MVC: Designing Real-World Web Applications now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.