实践H2

H2介绍

H2是用Java语言开发实现的轻量级数据库引擎

H2使用

H2版本:1.4.194,默认管理员(Admin)用户名为sa,密码为空串

H2有两种运行模式

  • 嵌入式(Embedded)

    数据库引擎和应用运行在同一个JVM中,作为应用的一部分,而不是独立的,不需要单独启动(H2驱动和url即可)。一般用于开发或测试阶段。

    根据数据持久化方式分为两种

    • 基于内存

      数据存放在内存中,重启则会丢失,例如:jdbc:h2:mem:testdb,数据库testdb存放在内存中。

      spring-jdbc集成的H2嵌入式数据源(SimpleDriverDataSource)参考:

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      @Bean
      public DataSource dataSource() {
        	// 默认数据库名testdb
          EmbeddedDatabaseBuilder edb = new EmbeddedDatabaseBuilder();
          // 默认HSQL
          edb.setType(EmbeddedDatabaseType.H2);
          // 默认schema.sql和data.sql
          edb.addScript("spittr/db/jpa/schema.sql");
          edb.addScript("spittr/db/jpa/test-data.sql");
          EmbeddedDatabase embeddedDatabase = edb.build();
          return embeddedDatabase;
      }
      
    • 基于文件系统

      数据持久化到文件系统中,例如:jdbc:h2[:file]:~/h2/testdb,数据库testdb数据持久化到~/h2/testdb.mv.db。

      1
      2
      3
      4
      5
      6
      7
      8
      9
        @Bean
        public DataSource dataSource() {
      	  SimpleDriverDataSource ds = new SimpleDriverDataSource();
      	  ds.setDriverClass(org.h2.Driver.class);
      	  ds.setUrl("jdbc:h2:file:/tmp/testdb");
      	  ds.setUsername("sa");
      	  ds.setPassword("");
      	  return ds;
        }
      

      两个H2引擎指向同一个文件,第二个H2引擎会报错如下:

      1
      WARN: HHH000342: Could not obtain connection to query metadata : Database may be already in use: "/tmp/testdb.mv.db". Possible solutions: close all other connection(s); use the server mode [90020-182]
      
  • 服务式(Server)

    数据库引擎独立部署,应用通过通讯协议连接到数据库。包含三种,

    • tcp

      C/S模式,一般通过jdbc连接,比如:jdbc:h2:tcp://localhost/~/h2/testdb【用户home路径】或jdbc:h2:tcp://localhost/home/angi/h2/testdb【绝对路径】,连接到localhost的H2数据库testdb,数据持久化到localhost的~/h2/testdb.mv.db或/home/angi/h2/testdb.mv.db

    • pg

      支持通过PostgreSQL客户端连接

    • web

      提供web版本的控制台(H2 Console),是连接数据库的web版本客户端,不仅仅支持H2(包括嵌入式和server),还支持其他各种数据库(通过jdbc)

      H2 Console Server

    查看H2帮助信息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
$ java -jar h2-1.4.194.jar -?
Starts the H2 Console (web-) server, as well as the TCP and PG server.
Usage: java org.h2.tools.Console <options>
When running without options, -tcp, -web, -browser and -pg are started.
Options are case sensitive. Supported options are:
[-help] or [-?]  Print the list of options
[-url]           Start a browser and connect to this URL
[-driver]        Used together with -url: the driver
[-user]          Used together with -url: the user name
[-password]      Used together with -url: the password
[-web]           Start the web server with the H2 Console
[-tool]          Start the icon or window that allows to start a browser
[-browser]       Start a browser connecting to the web server
[-tcp]           Start the TCP server
[-pg]            Start the PG server
For each Server, additional options are available;
 for details, see the Server tool.
If a service can not be started, the program
 terminates with an exit code of 1.
See also http://h2database.com/javadoc/org/h2/tools/Console.html
1
2
$ java -jar h2-1.4.194.jar -web
Web Console server running at http://localhost:8082 (only local connections)
1
2
$ java -jar h2-1.4.194.jar -tcp
TCP server running at tcp://localhost:9092 (only local connections)
1
2
$ java -jar h2-1.4.194.jar -pg
PG server running at pg://localhost:5435 (only local connections)
1
2
3
$ java -jar h2-1.4.194.jar -browser
Web Console server running at http://localhost:8082 (only local connections)
//会自动启动了浏览器访问http://localhost:8082
1
2
3
$ java -jar h2-1.4.194.jar -tool
Web Console server running at http://localhost:8082 (only local connections)
//会自动启动一个工具(系统托盘),可以用来启动浏览器(H2 Console)

H2 Console:

H2 Console