深刻理解h2和r2dbc-h2

簡介

本文將會介紹R2DBC的H2實現r2dbc-h2的使用方法和要注意的事項。一塊兒來看看吧。java

H2數據庫簡介

什麼是H2數據庫呢?spring

H2是一個Java SQL database,它是一個開源的數據庫,運行起來很是快。sql

H2流行的緣由是它既能夠當作一個獨立的服務器,也能夠以一個嵌套的服務運行,而且支持純內存形式運行。shell

H2的jar包很是小,只有2M大小,因此很是適合作嵌套式數據庫。數據庫

若是做爲嵌入式數據庫,則須要將h2*.jar添加到classpath中。服務器

下面是一個簡單的創建H2鏈接的代碼:app

import java.sql.*;
public class Test {
    public static void main(String[] a)
            throws Exception {
        Connection conn = DriverManager.
            getConnection("jdbc:h2:~/test", "sa", "");
        // add application code here
        conn.close();
    }
}

若是給定地址的數據庫並不存在,ide

同時H2還提供了一個簡單的管理界面,使用下面的命令就能夠啓動H2管理界面:ui

java -jar h2*.jar

默認狀況下訪問http://localhost:8082就能夠訪問到管理界面:url

r2dbc-h2

r2dbc-h2是r2dbc spi的一種實現。一樣的使用r2dbc-h2也提供了兩種h2的模式,一種是文件系統,一種是內存。

同時還提供了事務支持,prepared statements和batch statements等特性的支持。

r2dbc-h2的Maven依賴

要想使用r2dbc-h2,咱們須要添加以下依賴:

<dependency>
  <groupId>io.r2dbc</groupId>
  <artifactId>r2dbc-h2</artifactId>
  <version>${version}</version>
</dependency>

若是你體驗snapshot版本,能夠添加下面的依賴:

<dependency>
  <groupId>io.r2dbc</groupId>
  <artifactId>r2dbc-h2</artifactId>
  <version>${version}.BUILD-SNAPSHOT</version>
</dependency>

<repository>
  <id>spring-libs-snapshot</id>
  <name>Spring Snapshot Repository</name>
  <url>https://repo.spring.io/libs-snapshot</url>
</repository>

創建鏈接

h2有兩種鏈接方式,file和內存,咱們分別看一下都是怎麼創建鏈接的:

ConnectionFactory connectionFactory = ConnectionFactories.get("r2dbc:h2:mem:///testdb");

Publisher<? extends Connection> connectionPublisher = connectionFactory.create();
ConnectionFactory connectionFactory = ConnectionFactories.get("r2dbc:h2:file//my/relative/path");

Publisher<? extends Connection> connectionPublisher = connectionFactory.create();

咱們還能夠經過ConnectionFactoryOptions來建立更加詳細的鏈接信息:

ConnectionFactoryOptions options = builder()
    .option(DRIVER, "h2")
    .option(PROTOCOL, "...")  // file, mem
    .option(HOST, "…")
    .option(USER, "…")
    .option(PASSWORD, "…")
    .option(DATABASE, "…")
    .build();

ConnectionFactory connectionFactory = ConnectionFactories.get(options);

Publisher<? extends Connection> connectionPublisher = connectionFactory.create();

// Alternative: Creating a Mono using Project Reactor
Mono<Connection> connectionMono = Mono.from(connectionFactory.create());

上面的例子中,咱們使用到了driver,protocol, host,username,password和database這幾個選項,除此以外H2ConnectionOption中定義了其餘可使用的Option:

public enum H2ConnectionOption {

    /**
     * FILE|SOCKET|NO
     */
    FILE_LOCK,

    /**
     * TRUE|FALSE
     */
    IFEXISTS,

    /**
     * Seconds to stay open or {@literal -1} to to keep in-memory DB open as long as the virtual machine is alive.
     */
    DB_CLOSE_DELAY,

    /**
     * TRUE|FALSE
     */
    DB_CLOSE_ON_EXIT,

    /**
     * DML or DDL commands on startup, use "\\;" to chain multiple commands
     */
    INIT,

    /**
     * 0..3 (0=OFF, 1=ERROR, 2=INFO, 3=DEBUG)
     */
    TRACE_LEVEL_FILE,

    /**
     *  Megabytes (to override the 16mb default, e.g. 64)
     */
    TRACE_MAX_FILE_SIZE,

    /**
     * 0..3 (0=OFF, 1=ERROR, 2=INFO, 3=DEBUG)
     */
    TRACE_LEVEL_SYSTEM_OUT,

    LOG,

    /**
     * TRUE|FALSE
     */
    IGNORE_UNKNOWN_SETTINGS,

    /**
     * r|rw|rws|rwd (r=read, rw=read/write)
     */
    ACCESS_MODE_DATA,

    /**
     * DB2|Derby|HSQLDB|MSSQLServer|MySQL|Oracle|PostgreSQL|Ignite
     */
    MODE,

    /**
     *  TRUE|FALSE
     */
    AUTO_SERVER,

    /**
     * A port number
     */
    AUTO_SERVER_PORT,

    /**
     * Bytes (e.g. 512)
     */
    PAGE_SIZE,

    /**
     * Number of threads (e.g. 4)
     */
    MULTI_THREADED,

    /**
     * TQ|SOFT_LRU
     */
    CACHE_TYPE,

    /**
     * TRUE|FALSE
     */
    PASSWORD_HASH;
}

固然還有最直接的database選項:

r2dbc:h2:file//../relative/file/name
r2dbc:h2:file///absolute/file/name
r2dbc:h2:mem:///testdb

咱們還能夠經過H2特有的代碼H2ConnectionFactory來建立:

H2ConnectionFactory connectionFactory = new H2ConnectionFactory(H2ConnectionConfiguration.builder()
    .inMemory("...")
    .option(H2ConnectionOption.DB_CLOSE_DELAY, "-1")
    .build());

Mono<Connection> connection = connectionFactory.create();
CloseableConnectionFactory connectionFactory = H2ConnectionFactory.inMemory("testdb");

Mono<Connection> connection = connectionFactory.create();

參數綁定

在使用prepare statement的時候,咱們須要進行參數綁定:

connection
    .createStatement("INSERT INTO person (id, first_name, last_name) VALUES ($1, $2, $3)")
    .bind("$1", 1)
    .bind("$2", "Walter")
    .bind("$3", "White")
    .execute()

除了$符號綁定以外,還支持index綁定,以下所示:

Statement statement = connection.createStatement("SELECT title FROM books WHERE author = $1 and publisher = $2");
statement.bind(0, "John Doe");
statement.bind(1, "Happy Books LLC");

批處理

咱們來看下r2dbc-h2是怎麼來進行批處理的:

Batch batch = connection.createBatch();
Publisher<? extends Result> publisher = batch.add("SELECT title, author FROM books")
    .add("INSERT INTO books VALUES('John Doe', 'HappyBooks LLC')")
    .execute();

事務和Savepoint

r2dbc還支持事務和savepoint,咱們能夠在事務中rollback到特定的savepoint。具體的代碼以下:

Publisher<Void> begin = connection.beginTransaction();

Publisher<Void> insert1 = connection.createStatement("INSERT INTO books VALUES ('John Doe')").execute();

Publisher<Void> savepoint = connection.createSavepoint("savepoint");

Publisher<Void> insert2 = connection.createStatement("INSERT INTO books VALUES ('Jane Doe')").execute();


Publisher<Void> partialRollback = connection.rollbackTransactionToSavepoint("savepoint");


Publisher<Void> commit = connection.commit();
本文做者:flydean程序那些事

本文連接:http://www.flydean.com/r2dbc-h2-in-depth/

本文來源:flydean的博客

歡迎關注個人公衆號:「程序那些事」最通俗的解讀,最深入的乾貨,最簡潔的教程,衆多你不知道的小技巧等你來發現!

相關文章
相關標籤/搜索