首页  

H2数据库使用     所属分类 H2 浏览量 1394
Connection Modes

Embedded mode (local connections using JDBC)
Server mode (remote connections using JDBC or ODBC over TCP/IP) 
Mixed mode (local and remote connections at the same time)


内存模式
jdbc:h2:mem:testdb
不指定DBName,以私有方式启动,只允许一个连接

嵌入式
jdbc:h2:file:~/h2/DBName
第一次连接自动创建数据库

服务模式
web server  支持使用浏览器访问H2 Console
TCP server  支持客户端/服务器端的连接方式
PG server  支持PostgreSQL客户端

使用用户主目录
jdbc:h2:tcp://localhost/~/test  
使用绝对路径
jdbc:h2:tcp://localhost//data/test 


连接参数 参考 pdf Database URL Overview


连接字符串参数
DB_CLOSE_DELAY  
MODE=MySQL    兼容模式,兼容多种数据库,DB2、Derby、HSQLDB、MSSQLServer、MySQL、Oracle、PostgreSQL
AUTO_RECONNECT=TRUE  连接丢失后自动重新连接
AUTO_SERVER=TRUE   允许开启多个连接 
TRACE_LEVEL_SYSTEM_OUT  TRACE_LEVEL_FILE 
输出跟踪日志到控制台或文件, 取值0为OFF,1为ERROR(默认值),2为INFO,3为DEBUG

SET TRACE_MAX_FILE_SIZE  跟踪日志文件的大小,默认16M

Opening a Database Only if it Already Exists
The connection only succeeds when the database already exists. 
jdbc:h2:/data/sample;IFEXISTS=TRUE


Automatic Mixed Mode
Multiple processes can access the same database without having to start the server manually. 
This feature doesn't work with in-memory databases.

jdbc:h2:/data/test;AUTO_SERVER=TRUE

非混合模式只允许一个进程打开数据库

Exception in thread "main" org.h2.jdbc.JdbcSQLNonTransientConnectionException: Database may be already in use: null. 
Possible solutions: close all other connection(s); use the server mode [90020-200]

Caused by: java.lang.IllegalStateException: The file is locked: nio:/Users/dugang/h2/test.mv.db [1.4.200/7]


Page Size
The page size for new databases is 2 KB (2048), 
unless the page size is set explicitly in the database URL using PAGE_SIZE= when the database is created. 
The page size of existing databases can not be changed, so this property needs to be set when the database is created.

Page Size 默认 2 KB ,不可修改

Using the Trace Options



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.

连接时打开数据库

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

-web 启动支持H2 Console的服务
-webPort $port 服务启动端口,默认8082
-browser 启动H2 Console web管理页面
-tcp 使用TCP server模式启动
-pg 使用PG server模式启动

-tcpAllowOthers  -pgAllowOthers -webAllowOthers

java -cp h2*.jar org.h2.tools.Server -tcp -tcpPort 9300


java -cp h2-1.4.200.jar org.h2.tools.Server -tcp -tcpPort 9300
TCP server running at tcp://10.57.240.26:9300 (only local connections)

java -cp h2-1.4.200.jar org.h2.tools.Server -tcp -tcpAllowOthers  -tcpPort 9300
TCP server running at tcp://10.57.240.26:9300 (others can connect)

-tcpAllowOthers  允许从其他机器连接

jdbc:h2:~/h2/test
jdbc:h2:~/h2/test;AUTO_SERVER=TRUE
jdbc:h2:tcp://localhost:9300/~/h2/test;AUTO_SERVER=TRUE



Starting the TCP Server within an Application

import org.h2.tools.Server;
...
// start the TCP Server
Server server = Server.createTcpServer(args).start();
...
// stop the TCP Server
server.stop();



基于web H2 client 

https://gitee.com/dyyx/demos/tree/master/h2demo

上一篇     下一篇
H2 limits

列式存储引擎Parquet和ORC比较

parquet基本原理

ORC文件格式

flink基础

装逼词汇大全