List of In-Memory Databases – 内存数据库列表

最后修改: 2017年 4月 3日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

In-memory databases rely on system memory as opposed to disk space for storage of data. Because memory access is faster than disk access, these databases are naturally faster.

内存数据库依靠系统内存,而不是磁盘空间来存储数据。由于内存访问比磁盘访问更快,这些数据库自然更快。

Of course, we can only use an in-memory database in applications and scenarios where data doesn’t need to be persisted or for the purpose of executing tests faster. They’re often run as embedded databases, which means they are created when a process starts and discarded when the process ends which is super comfortable for testing because you do not need to setup an external database.

当然,我们只能在数据不需要持久化的应用和场景中使用内存数据库,或者为了更快地执行测试。它们通常作为嵌入式数据库运行,这意味着它们在进程开始时被创建,在进程结束时被丢弃,这对于测试来说是超级舒适的,因为你不需要设置一个外部数据库。

In the following sections, we will take a look at some of the most commonly used in-memory databases for the Java environment and the configuration necessary for each of them.

在下面几节中,我们将看看Java环境中最常用的一些内存数据库,以及它们各自所需的配置

2. H2 Database

2.H2数据库

H2 is an open source database written in Java that supports standard SQL for both embedded and standalone databases. It is very fast and contained within a JAR of only around 1.5 MB.

H2是一个用Java编写的开源数据库,支持嵌入式和独立数据库的标准SQL。它的速度非常快,包含在一个只有1.5MB左右的JAR中。

2.1. Maven Dependency

2.1.Maven的依赖性

To use H2 databases in an application, we need to add the following dependency:

要在一个应用程序中使用H2数据库,我们需要添加以下依赖关系。

<dependency>
    <groupId>com.h2database</groupId>
    <artifactId>h2</artifactId>
    <version>1.4.194</version>
</dependency>

The latest version of the H2 database can be downloaded from Maven Central.

最新版本的H2数据库可以从Maven中心下载。

2.2. Configuration

2.2.配置

To connect to an H2 in-memory database, we can use a connection String with the protocol mem, followed by the database name. The driverClassName, URL, username and password properties can be placed in a .properties file to be read by our application:

要连接到H2内存数据库,我们可以使用一个连接String,协议mem,后面是数据库名称。driverClassName、URL、usernamepassword属性可以放在一个.properties文件中,由我们的应用程序读取。

driverClassName=org.h2.Driver
url=jdbc:h2:mem:myDb;DB_CLOSE_DELAY=-1
username=sa
password=sa

These properties ensure the myDb database is created automatically on startup of the application.

这些属性确保myDb数据库在应用程序启动时被自动创建。

By default, when a connection to the database is closed, the database is closed as well. If we want the database to last for as long as the JVM is running, we can specify the property DB_CLOSE_DELAY=-1

默认情况下,当与数据库的连接被关闭时,数据库也被关闭。如果我们想让数据库在JVM运行的时候一直持续下去,我们可以指定属性DB_CLOSE_DELAY=-1

If we are using the database with Hibernate, we also need to specify the Hibernate dialect:

如果我们是用Hibernate的数据库,我们还需要指定Hibernate方言。

hibernate.dialect=org.hibernate.dialect.H2Dialect

H2 database is regularly maintained and provides a more detailed documentation on h2database.com.

H2数据库定期维护,并在h2database.com上提供更详细的文档。

3. HSQLDB (HyperSQL Database)

3.HSQLDBHyperSQL数据库)

HSQLDB is an open source project, also written in Java, representing a relational database. It follows the SQL and JDBC standards and supports SQL features such as stored procedures and triggers.

HSQLDB是一个开源项目,也是用Java编写的,代表一个关系型数据库。它遵循SQL和JDBC标准,支持SQL功能,如存储过程和触发器。

It can be used in the in-memory mode, or it can be configured to use disk storage.

它可以在内存模式下使用,也可以被配置为使用磁盘存储。

3.1. Maven Dependency

3.1.Maven的依赖性

To develop an application using HSQLDB, we need the Maven dependency:

要使用HSQLDB开发一个应用程序,我们需要Maven的依赖。

<dependency>
    <groupId>org.hsqldb</groupId>
    <artifactId>hsqldb</artifactId>
    <version>2.3.4</version>
</dependency>

You can find the latest version of HSQLDB on Maven Central.

您可以在Maven中心找到最新版本的HSQLDB

3.2. Configuration

3.2.配置

The connection properties we need have the following format:

我们需要的连接属性有以下格式。

driverClassName=org.hsqldb.jdbc.JDBCDriver
url=jdbc:hsqldb:mem:myDb
username=sa
password=sa

This ensures that the database will be created automatically on startup, reside in memory for the duration of the application, and be deleted when the process ends.

这确保了数据库将在启动时自动创建,在应用期间驻留在内存中,并在进程结束时被删除。

The Hibernate dialect property for HSQLDB is:

Hibernate方言属性的HSQLDB是。

hibernate.dialect=org.hibernate.dialect.HSQLDialect

The JAR file also contains a Database Manager with a GUI. More information can be found on the hsqldb.org website.

该JAR文件还包含一个带有GUI的数据库管理器。更多信息可以在hsqldb.org网站上找到。

4. Apache Derby Database

4.Apache Derby数据库

Apache Derby is another open source project containing a relational database management system created by the Apache Software Foundation.

Apache Derby是另一个包含关系型数据库管理系统的开源项目,由Apache软件基金会创建。

Derby is based on SQL and JDBC standards and is mainly used as an embedded database, but it can also be run in client-server mode by using the Derby Network Server framework.

Derby基于SQL和JDBC标准,主要用作嵌入式数据库,但它也可以通过使用Derby网络服务器框架以客户端-服务器模式运行。

4.1. Maven Dependency

4.1.Maven的依赖性

To use a Derby database in an application, we need to add the following Maven dependency:

要在应用程序中使用Derby数据库,我们需要添加以下Maven依赖。

<dependency>
    <groupId>org.apache.derby</groupId>
    <artifactId>derby</artifactId>
    <version>10.13.1.1</version>
</dependency>

The latest version of Derby database can be found on Maven Central.

最新版本的Derby数据库可以在Maven中心找到。

4.2. Configuration

4.2.配置

The connection string uses the memory protocol:

连接字符串使用memory协议。

driverClassName=org.apache.derby.jdbc.EmbeddedDriver
url=jdbc:derby:memory:myDb;create=true
username=sa
password=sa

For the database to be created automatically on startup, we have to specify create=true in the connection string. The database is closed and dropped by default on JVM exit.

为了在启动时自动创建数据库,我们必须在连接字符串中指定create=true。默认情况下,数据库在JVM退出时被关闭和丢弃。

If using the database with Hibernate, we need to define the dialect:

如果用Hibernate使用数据库,我们需要定义方言。

hibernate.dialect=org.hibernate.dialect.DerbyDialect

You can read more about Derby database at db.apache.org/derby.

你可以在db.apache.org/derby上阅读更多关于Derby数据库的信息。

5. SQLite Database

5 SQLite数据库

SQLite is a SQL database that runs only in embedded mode, either in memory or saved as a file. It is written in the C language but can also be used with Java.

SQLite是一个SQL数据库,只在嵌入式模式下运行,要么在内存中,要么保存为文件。它是用C语言编写的,但也可与Java一起使用。

5.1. Maven Dependency

5.1.Maven的依赖性

To use an SQLite database, we need to add the JDBC driver JAR:

为了使用SQLite数据库,我们需要添加JDBC驱动JAR。

<dependency>
    <groupId>org.xerial</groupId>
    <artifactId>sqlite-jdbc</artifactId>
    <version>3.16.1</version>
</dependency>

The sqlite-jdbc dependency can be downloaded from Maven Central.

sqlite-jdbc依赖项可以从Maven中心下载。

5.2. Configuration

5.2.配置

The connection properties use the org.sqlite.JDBC driver class and the memory protocol for the connection string:

连接属性使用org.sqlite.JDBC驱动类和memory协议的连接字符串。

driverClassName=org.sqlite.JDBC
url=jdbc:sqlite:memory:myDb
username=sa
password=sa

This will create the myDb database automatically if it does not exist.

如果myDb数据库不存在,这将自动创建它。

Currently, Hibernate does not provide a dialect for SQLite, although it very likely will in the future. If you want to use SQLite with Hibernate, you have to create your HibernateDialect class.

目前,Hibernate并没有为SQLite提供方言,尽管它很有可能在未来提供。如果你想在Hibernate中使用SQLite,你必须创建你的HibernateDialect类。

To find out more about SQLite, go to sqlite.org.

要了解有关SQLite的更多信息,请访问sqlite.org

6. In-Memory Databases in Spring Boot

6.Spring Boot中的内存数据库

Spring Boot makes it especially easy to use an in-memory database – because it can create the configuration automatically for H2, HSQLDB, and Derby.

Spring Boot使使用内存数据库变得特别容易–因为它可以为H2HSQLDBDerby自动创建配置。

All we need to do to use a database of one of the three types in Spring Boot is add its dependency to the pom.xml. When the framework encounters the dependency on the classpath, it will configure the database automatically.

要在Spring Boot中使用这三种类型的数据库,我们需要做的就是在pom.xml中添加其依赖性。当框架在classpath上遇到该依赖关系时,它将自动配置数据库。

7. Conclusion

7.结论

In this article, we had a quick look at the most commonly used in-memory databases in the Java ecosystem, along with their basic configurations. Although they are useful for testing, keep in mind that in many cases they do not provide exactly the same functionality as the original standalone ones.

在这篇文章中,我们快速浏览了Java生态系统中最常用的内存数据库,以及它们的基本配置。虽然它们对测试很有用,但请记住,在许多情况下,它们提供的功能与原来的独立数据库不完全相同。

You can find the code examples used in this article over on Github.

你可以在Github上找到本文使用的代码示例over