1. Overview

In this tutorial, we’ll learn how to start a Spring Boot application without having a running database.

By default, if we have a Spring Boot application that contains Spring Data JPA, then the application will automatically look to create a database connection. However, it may be required to avoid this in situations where a database is not available when the application is started.

2. Setup

We’ll work with a simple Spring Boot application that uses MySQL. Let’s look at the steps to set up the application.

2.1. Dependencies

Let’s add the Spring Data JPA starter and MySql Connector dependencies to the pom.xml file:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <scope>runtime</scope>
</dependency>

This adds JPA, MySQL Connector, and Hibernate to the classpath.

In addition to this, we want a task to keep running when the application is started. For this, let’s add the Web starter to the pom.xml file:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

This starts a web server on port 8080 and keeps the application running.

2.2. Properties

There are some mandatory properties we need in the application.properties file to set before starting the application:

spring.datasource.url=jdbc:mysql://localhost:3306/myDb
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQLDialect

Let’s understand the properties we set:

  • spring.datasource.url: the URL of the server and the name of the database.
  • spring.datasource.driver-class-name: the driver class name. The MySQL connector provides this driver.
  • spring.jpa.properties.hibernate.dialect: We have set this to MySQL. This tells the JPA provider to use the MySQL dialect.

In addition to this, we need to set the username and password required to connect to the database:

spring.datasource.username=root
spring.datasource.password=root

2.3. Start the Application

If we start the application, we’ll see the following error:

HHH000342: Could not obtain connection to query metadata
com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.

This is because we do not have a database server running at the specified URL. However, the default behavior of the application is to perform these two operations:

  • JPA tries to connect to the database server and fetches the metadata
  • Hibernate will try to create a database if it doesn’t exist. This is due to the property spring.jpa.hibernate.ddl-auto set to create by default.

3. Running Without A Database

To continue without a database, we need to fix the default behavior by overriding the two properties mentioned above.

First, let’s disable the metadata fetch:

spring.jpa.properties.hibernate.temp.use_jdbc_metadata_defaults=false

Then, we disable the automatic database creation:

spring.jpa.hibernate.ddl-auto=none

By setting this property, we have disabled the creation of a database. Therefore the application has no reason to create a connection.

Unlike earlier, now, when we start the application, it starts without any errors. Unless an operation requires interaction with the database, a connection will not be initiated.

4. Conclusion

In this article, we’ve learned how to start a Spring Boot application without requiring a running database.

We looked at the default behavior of the application that looks for a database connection. Then we fixed the default behavior by overriding two properties.

As always, the code examples used in this article can be found over on GitHub.

Course – LSD (cat=Persistence)

Get started with Spring Data JPA through the reference Learn Spring Data JPA course:

>> CHECK OUT THE COURSE
res – Persistence (eBook) (cat=Persistence)
Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.