1. Overview
1.概述
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
2.设置
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 starter和MySql Connector依赖关系添加到pom.xml文件中。
<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.
这将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。
<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.
这将在8080端口启动一个网络服务器,并保持应用程序的运行。
2.2. Properties
2.2.属性
There are some mandatory properties we need in the application.properties file to set before starting the application:
在启动应用程序之前,我们需要在application.properties文件中设置一些强制属性。
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
2.3.启动应用程序
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:
这是因为我们没有在指定的URL上运行数据库服务器。然而,应用程序的默认行为是执行这两项操作。
- 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
3.在没有数据库的情况下运行
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
4.总结
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.
一如既往,本文中所使用的代码示例可以在GitHub上找到over。