H2 应用指南
H2 应用指南
概述
H2 是一个开源的嵌入式数据库引擎,采用 java 语言编写,不受平台的限制。同时 H2 提供了一个十分方便的 web 控制台用于操作和管理数据库内容。H2 还提供兼容模式,可以兼容一些主流的数据库,因此采用 H2 作为开发期的数据库非常方便。
使用说明
H2 控制台应用
H2 允许用户通过浏览器接口方式访问 SQL 数据库。
- 进入官方下载地址,选择合适版本,下载并安装到本地。
- 启动方式:在 bin 目录下,双击 jar 包;执行
java -jar h2*.jar
;执行脚本:h2.bat
或h2.sh
。 - 在浏览器中访问:
http://localhost:8082
,应该可以看到下图中的页面:

点击 Connect ,可以进入操作界面:

操作界面十分简单,不一一细说。
嵌入式应用
JDBC API
Connection conn = DriverManager.
getConnection("jdbc:h2:~/test");
conn.close();
连接池
import org.h2.jdbcx.JdbcConnectionPool;
JdbcConnectionPool cp = JdbcConnectionPool.
create("jdbc:h2:~/test", "sa", "sa");
Connection conn = cp.getConnection();
conn.close(); cp.dispose();
Maven
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.197</version>
</dependency>
详见:Maven 2
Hibernate
hibernate.cfg.xml (or use the HSQLDialect):
<property name="dialect">
org.hibernate.dialect.H2Dialect
</property>
详见:Hibernate
TopLink 和 Glassfish
Datasource class: org.h2.jdbcx.JdbcDataSource
oracle.toplink.essentials.platform.database.H2Platform
运行方式
嵌入式
数据库持久化存储为单个文件。
连接字符串:\~/.h2/DBName
表示数据库文件的存储位置,如果第一次连接则会自动创建数据库。
jdbc:h2:\~/test
- 'test' 在用户根目录下jdbc:h2:/data/test
- 'test' 在 /data 目录下jdbc:h2:test
- 'test' 在当前工作目录
内存式
数据库只在内存中运行,关闭连接后数据库将被清空,适合测试环境
连接字符串:jdbc:h2:mem:DBName;DB_CLOSE_DELAY=-1
如果不指定 DBName,则以私有方式启动,只允许一个连接。
jdbc:h2:mem:test
- 一个进程中有多个连接jdbc:h2:mem:
- 未命名的私有库,一个连接
服务模式
H2 支持三种服务模式:
- web server:此种运行方式支持使用浏览器访问 H2 Console
- TCP server:支持客户端/服务器端的连接方式
- PG server:支持 PostgreSQL 客户端
启动 tcp 服务连接字符串示例:
jdbc:h2:tcp://localhost/\~/test
- 用户根目录jdbc:h2:tcp://localhost//data/test
- 绝对路径
启动服务
执行 java -cp *.jar org.h2.tools.Server
执行如下命令,获取选项列表及默认值
java -cp h2*.jar org.h2.tools.Server -?
常见的选项如下:
- -web:启动支持 H2 Console 的服务
- -webPort
<port>
:服务启动端口,默认为 8082 - -browser:启动 H2 Console web 管理页面
- -tcp:使用 TCP server 模式启动
- -pg:使用 PG server 模式启动
设置
jdbc:h2:..;MODE=MySQL
兼容模式(或 HSQLDB 等)jdbc:h2:..;TRACE_LEVEL_FILE=3
记录到*.trace.db
连接字符串参数
DB_CLOSE_DELAY
- 要求最后一个正在连接的连接断开后,不要关闭数据库MODE=MySQL
- 兼容模式,H2 兼容多种数据库,该值可以为:DB2、Derby、HSQLDB、MSSQLServer、MySQL、Oracle、PostgreSQLAUTO_RECONNECT=TRUE
- 连接丢失后自动重新连接AUTO_SERVER=TRUE
- 启动自动混合模式,允许开启多个连接,该参数不支持在内存中运行模式TRACE_LEVEL_SYSTEM_OUT
、TRACE_LEVEL_FILE
- 输出跟踪日志到控制台或文件, 取值 0 为 OFF,1 为 ERROR(默认值),2 为 INFO,3 为 DEBUGSET TRACE_MAX_FILE_SIZE mb
- 设置跟踪日志文件的大小,默认为 16M
maven 方式
此外,使用 maven 也可以启动 H2 服务。添加以下插件
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>java</goal>
</goals>
</execution>
</executions>
<configuration>
<mainClass>org.h2.tools.Server</mainClass>
<arguments>
<argument>-web</argument>
<argument>-webPort</argument>
<argument>8090</argument>
<argument>-browser</argument>
</arguments>
</configuration>
</plugin>
在命令行中执行如下命令启动 H2 Console
mvn exec:java
或者建立一个 bat 文件
@echo off
call mvn exec:java
pause
此操作相当于执行了如下命令:
java -jar h2-1.3.168.jar -web -webPort 8090 -browser
Spring 整合 H2
(1)添加依赖
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.194</version>
</dependency>
(2)spring 配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/jdbc
http://www.springframework.org/schema/jdbc/spring-jdbc.xsd">
<!--配置数据源-->
<bean id="dataSource" class="org.h2.jdbcx.JdbcConnectionPool"
destroy-method="dispose">
<constructor-arg>
<bean class="org.h2.jdbcx.JdbcDataSource">
<!-- 内存模式 -->
<property name="URL" value="jdbc:h2:mem:test"/>
<!-- 文件模式 -->
<!-- <property name="URL" value="jdbc:h2:testRestDB" /> -->
<property name="user" value="root"/>
<property name="password" value="root"/>
</bean>
</constructor-arg>
</bean>
<!-- JDBC模板 -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<constructor-arg ref="dataSource"/>
</bean>
<bean id="myJdbcTemplate" class="org.zp.notes.spring.jdbc.MyJdbcTemplate">
<property name="jdbcTemplate" ref="jdbcTemplate"/>
</bean>
<!-- 初始化数据表结构 -->
<jdbc:initialize-database data-source="dataSource" ignore-failures="ALL">
<jdbc:script location="classpath:sql/h2/create_table_student.sql"/>
</jdbc:initialize-database>
</beans>
H2 SQL
SELECT

INSERT

UPDATE

DELETE

BACKUP

EXPLAIN

7、MERGE
RUNSCRIPT
运行 sql 脚本文件

SCRIPT
根据数据库创建 sql 脚本

SHOW

ALTER
ALTER INDEX RENAME

ALTER SCHEMA RENAME

ALTER SEQUENCE

ALTER TABLE

增加约束

修改列

删除列

删除序列

ALTER USER
修改用户名

修改用户密码

ALTER VIEW

COMMENT

CREATE CONSTANT

CREATE INDEX

CREATE ROLE

CREATE SCHEMA

CREATE SEQUENCE

CREATE TABLE

CREATE TRIGGER

CREATE USER

CREATE VIEW

DROP

GRANT RIGHT
给 schema 授权授权

给 schema 授权给 schema 授权

复制角色的权限

REVOKE RIGHT
移除授权

移除角色具有的权限

ROLLBACK
从某个还原点(savepoint)回滚

回滚事务

创建 savepoint

数据类型

INT Type

集群
H2 支持两台服务器运行两个数据库成为集群,两个数据库互为备份,如果一个服务器失效,另一个服务器仍然可以工作。另外只有服务模式支持集群配置。
H2 可以通过 CreateCluster 工具创建集群,示例步骤如下(在在一台服务器上模拟两个数据库组成集群):
- 创建目录
- 创建两个服务器工作的目录
- 启动 tcp 服务
- 执行如下命令分别在 9101、9102 端口启动两个使用 tcp 服务模式的数据库
- 使用 CreateCluster 工具创建集群
- 如果两个数据库不存在,该命令将会自动创建数据库。如果一个数据库失效,可以先删除坏的数据库文件,重新启动数据库,然后重新运行 CreateCluster 工具
- 连接数据库现在可以使用如下连接字符串连接集群数据库
- 监控集群运行状态
- 可以使用如下命令查看配置的集群服务器是否都在运行
- 限制
- H2 的集群并不支持针对事务的负载均衡,所以很多操作会使两个数据库产生不一致的结果
- 执行如下操作时请小心:
- 自动增长列和标识列不支持集群,当插入数据时,序列值需要手动创建不支持 SET AUTOCOMMIT FALSE 语句;
- 如果需要设置成为不自动提交,可以执行方法 Connection.setAutoCommit(false)
参考资料
🚪 传送
◾ 💧 钝悟的 IT 知识图谱 ◾