Expand Authors Top

If you have a few years of experience in the Java ecosystem and you’d like to share that with the community, have a look at our Contribution Guidelines.

Expanded Audience – Frontegg – Security (partner)
announcement - icon User management is very complex, when implemented properly. No surprise here.

Not having to roll all of that out manually, but instead integrating a mature, fully-fledged solution - yeah, that makes a lot of sense.
That's basically what Frontegg is - User Management for your application. It's focused on making your app scalable, secure and enjoyable for your users.
From signup to authentication, it supports simple scenarios all the way to complex and custom application logic.

Have a look:

>> Elegant User Management, Tailor-made for B2B SaaS

NPI – JPA Buddy – JPA (partner)
announcement - icon

JPA is huge! It covers nearly every aspect of communication between relational databases and the Java application and is deeply integrated into all major frameworks.

If you're using IntelliJ, JPA Buddy is super helpful. The plugin gently guides you through the subtleties of the most popular JPA implementations, visually reminds you of JPA features, generates code that follows best practices, and integrates intelligent inspections to improve your existing persistence code.

More concretely, it provides powerful tooling to generate Spring Data JPA repositories and methods, Flyway Versioned Migrations, Liquibase Differential Changelogs, DDL and SQL statements, DTO objects, and MapStruct interfaces.

Oh, and it actually generates JPA entities from an existing database and gradually update the data model as the database evolves! Yeah.

>> Become a lot more productive with JPA Buddy

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.MySQL5Dialect

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 5. This tells the JPA provider to use the MySQL 5 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.

Persistence bottom

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

>> CHECK OUT THE COURSE
Persistence footer banner
Comments are closed on this article!