Spring Data JPA – Run an App Without a Database – Spring Data JPA – 在没有数据库的情况下运行一个应用程序

最后修改: 2022年 6月 16日

1. Overview


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

在本教程中,我们将学习如何在没有运行数据库的情况下启动一个Spring Boot应用程序。

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.

默认情况下,如果我们的Spring Boot应用程序包含Spring Data JPA,那么该应用程序将自动寻找创建数据库连接。但是,在应用程序启动时数据库不可用的情况下,可能需要避免这样做。

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.

我们将使用一个使用MySQL的简单Spring Boot应用程序。让我们来看看设置该应用程序的步骤。

2.1. Dependencies

2.1. 依赖性

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

让我们将Spring Data JPA starterMySql Connector依赖关系添加到pom.xml文件中。



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

这将JPA、MySQL Connector和Hibernate添加到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:

除此之外,我们还希望在应用程序启动时,有一个任务能够持续运行。为此,让我们在pom.xml文件中添加Web starter


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:



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:



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:



Then, we disable the automatic database creation:



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.

在这篇文章中,我们已经学会了如何在不需要运行数据库的情况下启动Spring Boot应用程序。

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.