1. Introduction
1.绪论
In this article, we’ll learn how to configure the Spring Boot application to use the embedded H2 database and then see where H2’s embedded database stores the data.
在本文中,我们将学习如何配置Spring Boot应用程序以使用嵌入式H2数据库,然后看看H2的嵌入式数据库在哪里存储数据。
H2 database is a lightweight and open-source database with no commercial support at this point. We can use it in various modes:
H2数据库是一个轻量级的开源数据库,目前还没有商业支持。我们可以在各种模式下使用它。
- server mode – for remote connections using JDBC or ODBC over TCP/IP
- embedded mode – for local connections that use JDBC
- mixed-mode – this means that we can use H2 for both local and remote connections
H2 can be configured to run as an in-memory database, but it can also be persistent, e.g., its data will be stored on disk. For the purpose of this tutorial, we’ll be working with the H2 database in embedded mode with enabled persistence so we’ll have data on the disk.
H2 可以被配置为作为内存数据库运行,但它也可以是持久性的,例如,其数据将存储在磁盘上。在本教程中,我们将在启用了持久性的嵌入式模式下使用 H2 数据库,因此我们将在磁盘上获得数据。
2. Embedded H2 Database
2.嵌入式H2数据库
If we want to use the H2 database, we’ll need to add the h2 and spring-boot-starter-data-jpa Maven dependencies to our pom.xml file:
如果我们想使用H2数据库,就需要在pom.xml文件中添加h2和spring-boot-starter-data-jpa的Maven依赖项。
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<versionId>1.4.200</versionId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<versionId>2.3.4.RELEASE</versionId>
</dependency>
3. H2’s Embedded Persistence Mode
3.H2的嵌入式持久性模式
We already mentioned that H2 could use a file system to store database data. The biggest advantage of this approach comparing to the in-memory one is that database data is not lost after the application restarts.
我们已经提到,H2可以使用文件系统来存储数据库数据。这种方法与内存方法相比,最大的优点是数据库数据在应用程序重新启动后不会丢失。
We’re able to configure storage mode through the spring.datasource.url property in our application.properties file. This way, we’re able to set the H2 database to use the in-memory approach by adding the mem parameter in the data source URL, followed by database name:
我们能够通过spring.datasource.url文件中的application.properties属性来配置存储模式。这样,我们就能通过在数据源URL中添加mem参数,然后是数据库名称,将H2数据库设置为使用内存方式。
spring.datasource.url=jdbc:h2:mem:demodb
spring.datasource.url=jdbc:h2:mem:demodb
If we use the file-based persistence mode, we’ll set one of the available options for disk locations instead of the mem parameter. In the next section, we’ll discuss what these options are.
如果我们使用基于文件的持久化模式,我们将设置磁盘位置的一个可用选项,而不是mem参数。在下一节,我们将讨论这些选项是什么。
Let’s see which files does the H2 database create:
让我们看看H2数据库会创建哪些文件。
- demodb.mv.db – unlike the others, this file is always created and it contains data, transaction log, and indexes
- demodb.lock.db – it is a database lock file and H2 recreates it when the database is in use
- demodb.trace.db – this file contains trace information
- demodb.123.temp.db – used for handling blobs or huge result sets
- demodb.newFile – H2 uses this file for database compaction and it contains a new database store file
- demodb.oldFile – H2 also uses this file for database compaction and it contains old database store file
4. H2’s Embedded Database Storage Location
4.H2的嵌入式数据库存储位置
H2 is very flexible concerning the storage of database files. At this moment, we can configure its storage directory to:
关于数据库文件的存储,H2是非常灵活的。此刻,我们可以将其存储目录配置为。
- directory on disk
- current user directory
- current project directory or working directory
4.1. Directory on Disk
4.1.磁盘上的目录
We can set a specific directory location where our database files will be stored:
我们可以设置一个特定的目录位置,我们的数据库文件将存放在那里。
spring.datasource.url=jdbc:h2:file:C:/data/demodb
Notice that in this connection string, the last chunk refers to the database name. Also, even if we miss the file keyword in this data source connection URL, H2 will manage it and create files in the provided location.
请注意,在这个连接字符串中,最后一块是指数据库名称。另外,即使我们错过了这个数据源连接URL中的文件关键字,H2也会管理它并在提供的位置上创建文件。
4.2. Current User Directory
4.2. 当前用户目录
In case we want to store database files in the current user directory, we’ll use the data source URL that contains a tilde (~) after the file keyword:
如果我们想在当前用户目录下存储数据库文件,我们将使用在file关键字后面包含一个tilde(~)的数据源URL。
spring.datasource.url=jdbc:h2:file:~/demodb
For example, in Windows systems, this directory will be C:/Users/<current user>.
例如,在Windows系统中,这个目录将是 C:/Users/<当前用户>。
To store database files in the subdirectory of the current user directory:
将数据库文件存储在当前用户目录的子目录下。
spring.datasource.url=jdbc:h2:file:~/subdirectory/demodb
Notice that if the subdirectory does not exist, it will be created automatically.
注意,如果子目录不存在,它将被自动创建。
4.3. Current Working Directory
4.3.当前工作目录
The current working directory is one where the application is started, and it’s referenced as a dot (.) in the data source URL. If we want database files there, we’ll configure it as follows:
当前工作目录是应用程序启动的地方,它在数据源URL中以点(.)的形式被引用。如果我们想在那里得到数据库文件,我们将按如下方式配置。
spring.datasource.url=jdbc:h2:file:./demodb
To store database files in the subdirectory of the current working directory:
将数据库文件存储在当前工作目录的子目录下。
spring.datasource.url=jdbc:h2:file:./subdirectory/demodb
Notice that if the subdirectory does not exist, it will be created automatically.
注意,如果子目录不存在,它将被自动创建。
5. Conclusion
5.总结
In this short tutorial, we discussed some aspects of the H2 database and showed where H2’s embedded database stores the data. We also learned how to configure the location of the database files.
在这个简短的教程中,我们讨论了H2数据库的一些方面,并展示了H2的嵌入式数据库存储数据的地方。我们还学习了如何配置数据库文件的位置。
The complete code sample is available over on GitHub.
完整的代码样本可在GitHub上获得。