Search code examples
javajpaderbyspring-boot

Spring Boot Directory-Based Database


I would like to use spring-boot's repository feature to connect to a directory-based Derby database. After much struggling, I realized that the derby DB that I keep connecting to is only an in-memory database. Is there a way for me to specify that the database should be stored on the file system instead of memory? I would like my database to be persisted (and able to be accessed) after my JVM is killed.

I have tried specifying the Derby connection URL to use a directory-based database using the following property in application.properties:

spring.datasource.url=jdbc:derby:directory;create=true

My property did get read and used, but the database still didn't persist data between JVM starups and shutdowns. It seems like a Derby DB directory structure gets created at the location specified by my "derby.system.home" system property, but when I restarted my JVM, it did not contain any of the data stored while in the old JVM.

Thanks in advance!


Solution

  • I assume you're using Hibernate. If so, the configuration of spring.jpa.hibernate.ddl-auto is important. When you're using an embedded database, such as Derby, Boot defaults this to create-drop. The drop part of that will be causing Hibernate to drop all the database tables when you shut down your app.

    You should change this configuration to better suit your needs. Using none and creating the schema using a schema.sql file, for example.

    You can find more information about Boot's support for database initialisation in the docs.