H2 Database IDEA 源码 DEBUG 环境搭建
基于最新的 version-2.3.230 拉取分支。
git remote add h2 https://github.com/h2database/h2database.git
git fetch h2
git checkout -b version-2.3.230 version-2.3.230
使用
# 启动
java -jar h2*.jar
# H2 shell 方式使用
java -cp h2-*.jar org.h2.tools.Shell
h2 shell
启动类 org.h2.tools.Shell
# 配置启动参数
-url "jdbc:h2:~/test" -user "sa" -password ""
测试 case
create table + insert
Welcome to H2 Shell 2.3.230 (2024-07-15)
Exit with Ctrl+C
[Enter] jdbc:h2:~/test
URL jdbc:h2:~/test
[Enter] org.h2.Driver
Driver org.h2.Driver
[Enter]
User sa
Password Password ><
Type the same password again to confirm database creation.
Password><Password ><
Connected
Commands are case insensitive; SQL statements end with ';'
help or ? Display this help
list Toggle result list / stack trace mode
maxwidth Set maximum column width (default is 100)
autocommit Enable or disable autocommit
history Show the last 20 statements
quit or exit Close the connection and exit
sql> show tables;
TABLE_NAME | TABLE_SCHEMA
(0 rows, 197 ms)
sql> create table test(id int);
(Update count: 0, 5 ms)
sql> show tables;
TABLE_NAME | TABLE_SCHEMA
TEST | PUBLIC
(1 row, 4 ms)
sql> insert into test values(1);
(Update count: 1, 4 ms)
sql> select * from test;
ID
1
(1 row, 1 ms)
sql>
事务操作
sql> select * from test;
ID
1
1
(2 rows, 2 ms)
sql> begin;
(Update count: 0, 1 ms)
sql> delete from test;
(Update count: 2, 1 ms)
sql> rollback;
(Update count: 0, 2 ms)
sql> select * from test;
ID
1
1
(2 rows, 2 ms)
H2架构
根据官方文档介绍,从上到下,各层如下所示:
JDBC driver.
JDBC 驱动程序
Connection/session management.
连接/会话管理
SQL Parser.
SQL 解析器
Command execution and planning.
命令执行和计划
Table/Index/Constraints.
表/索引/约束
Undo log, redo log, and transactions layer.
undo log、redo log 和 事务层
B-tree engine and page-based storage allocation.
B 树引擎 & 基于页的存储分配
Filesystem abstraction.
文件系统抽象