🏠Spring BootDerby Embedded Database for Spring Boot

Derby Embedded Database for Spring Boot

Spring Boot Spring Framework

In this post, We will see how we can use Apache Derby embedded database with Spring Boot applications with an example.

Introduction

Apache Derby is a pure java implementation of a relational database. Also, it complies to JDBC and RDBMS standards. For this reason, we can easily embed Derby into applications.

Derby Dependencies for Spring Boot

To include derby into the project, All you have to do is to add the Derby and JPA Starter dependencies from Spring Boot.

<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>org.apache.derby</groupId> <artifactId>derby</artifactId> </dependency>
Code language: HTML, XML (xml)

Notice that we did not specify any versions as spring boot takes care of all of its dependencies through its spring-boot-dependencies BOM.

Spring Boot configuration

Spring Boot takes care of most of the configuration. For instance, it generates an memory based derby connection string, an user with name “sa” and an empty password. However, You can override this using the following properties.

spring.datasource.url=jdbc:derby:memory:local;create=true spring.datasource.username=derbyuser spring.datasource.password=password
Code language: Properties (properties)

You can run the database in directory mode so that you can store the data in filesystem. This way, you can persist the data over application restarts.

For example, The following setting would setup a database directory in the application working directory.

spring.datasource.url=jdbc:derby:directory:mydbdirectory;create=true
Code language: Properties (properties)

Unlike h2 database, Spring Boot doesn’t support any web based console for Derby.

Initializing Schema

All embedded databases would require initializing Schema. This is because the embedded databases are not persistent and would lose data on restart. To achieve this, Spring Boot expects a schema.sql and data.sql file in classpath.

If you have different file names, you can use the following properties.

spring.datasource.schema=initial-schema.sql spring.datasource.data=initial-data.sql
Code language: Properties (properties)

You can also disable initialization by adjusting the mode.

spring.datasource.initialization-mode=never
Code language: Properties (properties)

Let’s test out out setup. Here is a sample database entity and the data to be loaded for it.

@Entity public class UserEntity { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Integer id; private String firstName; private String lastName; }
Code language: Java (java)
insert into user_entity (id,first_name,last_name) values (1,'Homer','Simpson');<br>insert into user_entity (id,first_name,last_name) values (2,'Bart','Simpson');<br>insert into user_entity (id,first_name,last_name) values (3,'Lisa','Simpson');
Code language: PHP (php)

And here is the API call for looking up the initialized data.

@GetMapping("/test") public List<UserEntity> getUsers(){ return userRepository.findAll(); }
Code language: Java (java)

When starting the application, you may see the following warning.

Caused by: org.apache.derby.iapi.error.StandardException: ‘DROP TABLE’ cannot be performed on ‘USER_ENTITY’ because it does not exist.

It is because the derby instance is refreshed every time and it doesn’t have any table to be dropped. However, spring boot thinks it needs to clear the tables. As this is just a warning, you can ignore this stack trace.

Conclusion

To summarize, We learned how to include derby into a Spring boot application with an example. The sample code for this guide can be found in this github repository.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *