H2 数据库的使用

最近项目用到 H2 Database 数据库,搞来搞去还不是太熟,也踩了一些坑,写小博客记录下。

如果纰漏或错误,还望评论指正,谢谢。

一、内嵌模式使用

这种模式官方里说的还是比较简单:Embedding H2 in an Application

  • Add the h2*.jar to the classpath (H2 does not have any dependencies)
  • Use the JDBC driver class: org.h2.Driver
  • The database URL jdbc:h2:~/test opens the database test in your user home directory
  • A new database is automatically created

翻译下就是:

  • h2*.jar 添加到项目目录下(H2 没有其他任何依赖)
  • JDBC 驱动类使用:org.h2.Driver
  • 数据库 URL 使用 jdbc:h2:~/test,这样会打开用户目录下的 test 数据库
  • 这个新的数据库文件会自动创建,指以上的 test

说明:

用户目录是指当前系登录用户的用户目录,Linux 下就是命令 cd ~ 进去的目录;Windows下一般就是 C:\Users\xxxxx

二、Server 模式使用

除了 内嵌模式 使用,还可以 Server 模式

不管是 Windows 还是 Linux,都建议去 官网下载 Platform-Independent Zip 这个平台独立的压缩包,解压就可以了。

解压后的目录,详见官网说明:Directory Structure

2.1 使用 Server 模式

2.1.1 Windows 下启动

双击 h2/bin/h2.bat 脚本文件就可以启动了,启动后浏览器会自动打开 http://localhost:8082,这是因为它启动了一个 The H2 Console Application 程序,使得我可以通过浏览器来访问数据库。

2.1.2 Linux 下启动
# cd h2/bin
# nohup java -cp h2*.jar org.h2.tools.Server &

说明:先进入到 H2 的 bin 目录下,然后 Server 启动模式。官网文档里 Using the Server 用的命令是 java -cp h2*.jar org.h2.tools.Server 前后的 nohup & 是我自己加的,为的是进程不占用我们的 SHH 出入,而且进程不会印因为 SHH 连接断开而退出。

启动时还可以指定参数:

# java -cp h2*.jar org.h2.tools.Server -?

比如允许远程 Web 访问和远程 TCP 访问(出于安全原因,默认只允许本机访问):

# java -cp h2-1.4.194.jar org.h2.tools.Server -webAllowOthers -tcpAllowOthers

其他参数还有 -tcpPort -webPort 等等,不细说。

2.1.3 启动 Server 模式都干了啥?

如果我们 Linux 下运行启动 Server 模式的命令

# java -cp h2*.jar org.h2.tools.Server

会发现程序会输出以下内容:

TCP server running at tcp://10.27.174.227:9092 (only local connections)
PG server running at pg://10.27.174.227:5435 (only local connections)
Web Console server running at http://10.27.174.227:8082 (others can connect)

H2 启动了三个 Server:

  • 一个 TCP Server,用于 client/server connections 连接。端口9092
  • 一个 PG server,用于 PostgreSQL clients 客户端程序。端口5435
  • 一个 Web Server,用于 H2 Console 程序。端口8082

上面的 IP 地址是本期局域网IP,每台机器的都不同。咦?默认是允许远程 Web 访问??

2.1.4 启动 Server 模式是否打开了数据库

答案是否定的:

The servers can be started in different ways, one is using the Server tool.
Starting the server doesn't open a database - databases are opened as soon as a client connects.

使用 Server tool 启动并没有打开数据库,数据库在客户端连接之后才立即打开

2.1.5 夸进程关闭 TCP Server

官网文档 Stopping a TCP Server from Another Process 有说到:

运行命令关闭:

# java org.h2.tools.Server -tcpShutdown tcp://localhost:9092

应用程序里写代码关闭:

org.h2.tools.Server.shutdownTcpServer("tcp://localhost:9094");

以上方法 仅关闭 TCP Server。还有一段注意事项:

If other server were started in the same process, they will continue to run. 
To avoid recovery when the databases are opened the next time, all connections to the databases should be closed before calling this method.

关闭远程 Server:To stop a remote server, remote connections must be enabled on the server,关闭远程 Server 需要远程 Server 启用了该支持才行。

对关闭 TCP Server 进行保护:Shutting down a TCP server can be protected using the option -tcpPassword (the same password must be used to start and stop the TCP server).,启动时指定了 -tcpPassword 那么关闭 TCP Server 时也要同样的密码才能关闭。

三、应用连接至 H2 Database

H2 数据库可以内嵌仅 Java 应用程序,也可以以 Server 模式运行。取决于 H2 数据库的运行方式,需要使用不同的数据库连接地址(JDBC connection URL)来访问

内嵌模式(Embedded into your application)

jdbc:h2:~/test

数据将被保存到 test 文件,该文件在你的用户目录下。

Server模式(Client-Server mode)

jdbc:h2:tcp://localhost/~/test

同上:数据将被保存到 test 文件,该文件在你的用户目录下。

内存模式(In-Memory database)

jdbc:h2:mem:test

另外,H2 还可以配置成运行在内存中,意味着数据不会保存到磁盘,应用程序结束,内存中的数据的丢失。

以上那么多篇幅都在说一些“运维”的事情,现在说说我们的应用怎么连接并使用 H2 数据库,这里已 Spring Boot 项目为例。

Maven pom.xml 添加 H2 依赖:

<dependency>
    <groupId>com.h2database</groupId>
    <artifactId>h2</artifactId>
    <scope>runtime</scope>
</dependency>

内嵌模式 Spring Boot 的 application.properties 文件:

spring.datasource.driver-class-name=org.h2.Driver
spring.datasource.url=jdbc:h2:~/test
spring.datasource.username=sa

Server模式 Spring Boot 的 application.properties 文件:

spring.datasource.driver-class-name=org.h2.Driver
spring.datasource.url=jdbc:h2:tcp://localhost/~/test
spring.datasource.username=sa

四、其他想说的

4.1 数据库文件

数据库文件都是保存到用户目录下的。不管是内嵌模式还是Server模式,只要 JDBC connection URL 上写的数据库名字是一样的,那么都是使用同一个数据库文件。

4.2 内嵌模式的 H2 Web Server 进程

内嵌模式的 h2 与应用程序是运行在同一个进程中,比如 Spring Boot 默认使用8080端口,那么访问 H2 Web Server 也是8080端口,因为 Spring Boot 自动配置,为 H2 Web Server 配置了一个 Handler,所以访问地址变成了:

http://localhost:8080/h2-console

相比之下,Server 模式默认的 Web Server 端口是8082,则 H2 Web Server 访问地址是:

http://localhost:8082/

五、附录

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值