最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

深入理解r2dbc在mysql中的使用

 更新時(shí)間:2021年02月18日 12:23:15   作者:flydean程序那些事  
這篇文章主要介紹了深入理解r2dbc在mysql中的使用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

簡(jiǎn)介

mysql應(yīng)該是我們?cè)谌粘9ぷ髦惺褂玫降囊粋€(gè)非常普遍的數(shù)據(jù)庫(kù),雖然mysql現(xiàn)在是oracle公司的,但是它是開(kāi)源的,市場(chǎng)占有率還是非常高的。

今天我們將會(huì)介紹r2dbc在mysql中的使用。

r2dbc-mysql的maven依賴

要想使用r2dbc-mysql,我們需要添加如下的maven依賴:

<dependency>
  <groupId>dev.miku</groupId>
  <artifactId>r2dbc-mysql</artifactId>
  <version>0.8.2.RELEASE</version>
</dependency>

當(dāng)然,如果你想使用snapshot版本的話,可以這樣:

<dependency>
  <groupId>dev.miku</groupId>
  <artifactId>r2dbc-mysql</artifactId>
  <version>${r2dbc-mysql.version}.BUILD-SNAPSHOT</version>
</dependency>

<repository>
  <id>sonatype-snapshots</id>
  <name>SonaType Snapshots</name>
  <url>https://oss.sonatype.org/content/repositories/snapshots</url>
  <snapshots>
    <enabled>true</enabled>
  </snapshots>
</repository>

創(chuàng)建connectionFactory

創(chuàng)建connectionFactory的代碼實(shí)際上使用的r2dbc的標(biāo)準(zhǔn)接口,所以和之前講到的h2的創(chuàng)建代碼基本上是一樣的:

// Notice: the query string must be URL encoded
ConnectionFactory connectionFactory = ConnectionFactories.get(
  "r2dbcs:mysql://root:database-password-in-here@127.0.0.1:3306/r2dbc?" +
  "zeroDate=use_round&" +
  "sslMode=verify_identity&" +
  "useServerPrepareStatement=true&" +
  "tlsVersion=TLSv1.3%2CTLSv1.2%2CTLSv1.1&" +
  "sslCa=%2Fpath%2Fto%2Fmysql%2Fca.pem&" +
  "sslKey=%2Fpath%2Fto%2Fmysql%2Fclient-key.pem&" +
  "sslCert=%2Fpath%2Fto%2Fmysql%2Fclient-cert.pem&" +
  "sslKeyPassword=key-pem-password-in-here"
)

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

不同的是ConnectionFactories傳入的參數(shù)不同。

我們也支持unix domain socket的格式:

// Minimum configuration for unix domain socket
ConnectionFactory connectionFactory = ConnectionFactories.get("r2dbc:mysql://root@unix?unixSocket=%2Fpath%2Fto%2Fmysql.sock")

Mono<Connection> connectionMono = Mono.from(connectionFactory.create());

同樣的,我們也支持從ConnectionFactoryOptions中創(chuàng)建ConnectionFactory:

ConnectionFactoryOptions options = ConnectionFactoryOptions.builder()
  .option(DRIVER, "mysql")
  .option(HOST, "127.0.0.1")
  .option(USER, "root")
  .option(PORT, 3306) // optional, default 3306
  .option(PASSWORD, "database-password-in-here") // optional, default null, null means has no password
  .option(DATABASE, "r2dbc") // optional, default null, null means not specifying the database
  .option(CONNECT_TIMEOUT, Duration.ofSeconds(3)) // optional, default null, null means no timeout
  .option(SSL, true) // optional, default sslMode is "preferred", it will be ignore if sslMode is set
  .option(Option.valueOf("sslMode"), "verify_identity") // optional, default "preferred"
  .option(Option.valueOf("sslCa"), "/path/to/mysql/ca.pem") // required when sslMode is verify_ca or verify_identity, default null, null means has no server CA cert
  .option(Option.valueOf("sslCert"), "/path/to/mysql/client-cert.pem") // optional, default null, null means has no client cert
  .option(Option.valueOf("sslKey"), "/path/to/mysql/client-key.pem") // optional, default null, null means has no client key
  .option(Option.valueOf("sslKeyPassword"), "key-pem-password-in-here") // optional, default null, null means has no password for client key (i.e. "sslKey")
  .option(Option.valueOf("tlsVersion"), "TLSv1.3,TLSv1.2,TLSv1.1") // optional, default is auto-selected by the server
  .option(Option.valueOf("sslHostnameVerifier"), "com.example.demo.MyVerifier") // optional, default is null, null means use standard verifier
  .option(Option.valueOf("sslContextBuilderCustomizer"), "com.example.demo.MyCustomizer") // optional, default is no-op customizer
  .option(Option.valueOf("zeroDate"), "use_null") // optional, default "use_null"
  .option(Option.valueOf("useServerPrepareStatement"), true) // optional, default false
  .option(Option.valueOf("tcpKeepAlive"), true) // optional, default false
  .option(Option.valueOf("tcpNoDelay"), true) // optional, default false
  .option(Option.valueOf("autodetectExtensions"), false) // optional, default false
  .build();
ConnectionFactory connectionFactory = ConnectionFactories.get(options);

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

或者下面的unix domain socket格式:

// Minimum configuration for unix domain socket
ConnectionFactoryOptions options = ConnectionFactoryOptions.builder()
  .option(DRIVER, "mysql")
  .option(Option.valueOf("unixSocket"), "/path/to/mysql.sock")
  .option(USER, "root")
  .build();
ConnectionFactory connectionFactory = ConnectionFactories.get(options);

Mono<Connection> connectionMono = Mono.from(connectionFactory.create());

使用MySqlConnectionFactory創(chuàng)建connection

上面的例子中,我們使用的是通用的r2dbc api來(lái)創(chuàng)建connection,同樣的,我們也可以使用特有的MySqlConnectionFactory來(lái)創(chuàng)建connection:

MySqlConnectionConfiguration configuration = MySqlConnectionConfiguration.builder()
  .host("127.0.0.1")
  .user("root")
  .port(3306) // optional, default 3306
  .password("database-password-in-here") // optional, default null, null means has no password
  .database("r2dbc") // optional, default null, null means not specifying the database
  .serverZoneId(ZoneId.of("Continent/City")) // optional, default null, null means query server time zone when connection init
  .connectTimeout(Duration.ofSeconds(3)) // optional, default null, null means no timeout
  .sslMode(SslMode.VERIFY_IDENTITY) // optional, default SslMode.PREFERRED
  .sslCa("/path/to/mysql/ca.pem") // required when sslMode is VERIFY_CA or VERIFY_IDENTITY, default null, null means has no server CA cert
  .sslCert("/path/to/mysql/client-cert.pem") // optional, default has no client SSL certificate
  .sslKey("/path/to/mysql/client-key.pem") // optional, default has no client SSL key
  .sslKeyPassword("key-pem-password-in-here") // optional, default has no client SSL key password
  .tlsVersion(TlsVersions.TLS1_3, TlsVersions.TLS1_2, TlsVersions.TLS1_1) // optional, default is auto-selected by the server
  .sslHostnameVerifier(MyVerifier.INSTANCE) // optional, default is null, null means use standard verifier
  .sslContextBuilderCustomizer(MyCustomizer.INSTANCE) // optional, default is no-op customizer
  .zeroDateOption(ZeroDateOption.USE_NULL) // optional, default ZeroDateOption.USE_NULL
  .useServerPrepareStatement() // Use server-preparing statements, default use client-preparing statements
  .tcpKeepAlive(true) // optional, controls TCP Keep Alive, default is false
  .tcpNoDelay(true) // optional, controls TCP No Delay, default is false
  .autodetectExtensions(false) // optional, controls extension auto-detect, default is true
  .extendWith(MyExtension.INSTANCE) // optional, manual extend an extension into extensions, default using auto-detect
  .build();
ConnectionFactory connectionFactory = MySqlConnectionFactory.from(configuration);

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

或者下面的unix domain socket方式:

// Minimum configuration for unix domain socket
MySqlConnectionConfiguration configuration = MySqlConnectionConfiguration.builder()
  .unixSocket("/path/to/mysql.sock")
  .user("root")
  .build();
ConnectionFactory connectionFactory = MySqlConnectionFactory.from(configuration);

Mono<Connection> connectionMono = Mono.from(connectionFactory.create());

執(zhí)行statement

首先看一個(gè)簡(jiǎn)單的不帶參數(shù)的statement:

connection.createStatement("INSERT INTO `person` (`first_name`, `last_name`) VALUES ('who', 'how')")
  .execute(); // return a Publisher include one Result

然后看一個(gè)帶參數(shù)的statement:

connection.createStatement("INSERT INTO `person` (`birth`, `nickname`, `show_name`) VALUES (?, ?name, ?name)")
  .bind(0, LocalDateTime.of(2019, 6, 25, 12, 12, 12))
  .bind("name", "Some one") // Not one-to-one binding, call twice of native index-bindings, or call once of name-bindings.
  .add()
  .bind(0, LocalDateTime.of(2009, 6, 25, 12, 12, 12))
  .bind(1, "My Nickname")
  .bind(2, "Naming show")
  .returnGeneratedValues("generated_id")
  .execute(); // return a Publisher include two Results.

注意,如果參數(shù)是null的話,可以使用bindNull來(lái)進(jìn)行null值的綁定。

接下來(lái)我們看一個(gè)批量執(zhí)行的操作:

connection.createBatch()
  .add("INSERT INTO `person` (`first_name`, `last_name`) VALUES ('who', 'how')")
  .add("UPDATE `earth` SET `count` = `count` + 1 WHERE `id` = 'human'")
  .execute(); // return a Publisher include two Results.

執(zhí)行事務(wù)

我們看一個(gè)執(zhí)行事務(wù)的例子:

connection.beginTransaction()
  .then(Mono.from(connection.createStatement("INSERT INTO `person` (`first_name`, `last_name`) VALUES ('who', 'how')").execute()))
  .flatMap(Result::getRowsUpdated)
  .thenMany(connection.createStatement("INSERT INTO `person` (`birth`, `nickname`, `show_name`) VALUES (?, ?name, ?name)")
    .bind(0, LocalDateTime.of(2019, 6, 25, 12, 12, 12))
    .bind("name", "Some one")
    .add()
    .bind(0, LocalDateTime.of(2009, 6, 25, 12, 12, 12))
    .bind(1, "My Nickname")
    .bind(2, "Naming show")
    .returnGeneratedValues("generated_id")
    .execute())
  .flatMap(Result::getRowsUpdated)
  .then(connection.commitTransaction());

使用線程池

為了提升數(shù)據(jù)庫(kù)的執(zhí)行效率,減少建立連接的開(kāi)銷,一般數(shù)據(jù)庫(kù)連接都會(huì)有連接池的概念,同樣的r2dbc也有一個(gè)叫做r2dbc-pool的連接池。

r2dbc-pool的依賴:

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

如果你想使用snapshot版本,也可以這樣指定:

<dependency>
 <groupId>io.r2dbc</groupId>
 <artifactId>r2dbc-pool</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>

看一下怎么指定數(shù)據(jù)庫(kù)連接池:

ConnectionFactory connectionFactory = ConnectionFactories.get("r2dbc:pool:<my-driver>://<host>:<port>/<database>[?maxIdleTime=PT60S[&…]");

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

可以看到,我們只需要在連接URL上面添加pool這個(gè)driver即可。

同樣的,我們也可以通過(guò)ConnectionFactoryOptions來(lái)創(chuàng)建:

ConnectionFactory connectionFactory = ConnectionFactories.get(ConnectionFactoryOptions.builder()
  .option(DRIVER, "pool")
  .option(PROTOCOL, "postgresql") // driver identifier, PROTOCOL is delegated as DRIVER by the pool.
  .option(HOST, "…")
  .option(PORT, "…") 
  .option(USER, "…")
  .option(PASSWORD, "…")
  .option(DATABASE, "…")
  .build());

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

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

最后, 你也可以直接通過(guò)創(chuàng)建ConnectionPoolConfiguration來(lái)使用線程池:

ConnectionFactory connectionFactory = …;

ConnectionPoolConfiguration configuration = ConnectionPoolConfiguration.builder(connectionFactory)
  .maxIdleTime(Duration.ofMillis(1000))
  .maxSize(20)
  .build();

ConnectionPool pool = new ConnectionPool(configuration);
 

Mono<Connection> connectionMono = pool.create();

// later

Connection connection = …;
Mono<Void> release = connection.close(); // released the connection back to the pool

// application shutdown
pool.dispose();

到此這篇關(guān)于深入理解r2dbc在mysql中的使用的文章就介紹到這了,更多相關(guān)mysql r2dbc 內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • mysql不包含模糊查詢問(wèn)題

    mysql不包含模糊查詢問(wèn)題

    這篇文章主要介紹了mysql不包含模糊查詢問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-08-08
  • mysql命令行如何操作

    mysql命令行如何操作

    這篇文章主要介紹了mysql命令行如何操作,還為大家分享了mysql添加環(huán)境變量的方法,感興趣的小伙伴們可以參考一下
    2015-11-11
  • getdata table表格數(shù)據(jù)join mysql方法

    getdata table表格數(shù)據(jù)join mysql方法

    今天小編就為大家分享一篇關(guān)于getdata table表格數(shù)據(jù)join mysql方法,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧
    2019-02-02
  • MySQL8.0?Command?Line?Client輸入密碼后出現(xiàn)閃退現(xiàn)象的原因以及解決方法總結(jié)

    MySQL8.0?Command?Line?Client輸入密碼后出現(xiàn)閃退現(xiàn)象的原因以及解決方法總結(jié)

    我們?cè)诎惭bMYSQL數(shù)據(jù)庫(kù)時(shí),經(jīng)常會(huì)出現(xiàn)一些問(wèn)題,下面這篇文章主要給大家介紹了關(guān)于MySQL8.0?Command?Line?Client輸入密碼后出現(xiàn)閃退現(xiàn)象的原因以及解決方法的相關(guān)資料,需要的朋友可以參考下
    2023-03-03
  • MySQL安裝與創(chuàng)建用戶操作(新手入門指南)

    MySQL安裝與創(chuàng)建用戶操作(新手入門指南)

    這篇文章主要為大家介紹了MySQL安裝與創(chuàng)建用戶的使用講解是非常適合小白新手的入門學(xué)習(xí),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-05-05
  • CentOS8下MySQL 8.0安裝部署的方法

    CentOS8下MySQL 8.0安裝部署的方法

    這篇文章主要介紹了CentOS 8下 MySQL 8.0 安裝部署的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-11-11
  • MySQL中order?by排序時(shí)數(shù)據(jù)存在null則排序在最前面的方法

    MySQL中order?by排序時(shí)數(shù)據(jù)存在null則排序在最前面的方法

    order by排序是最常用的功能,但是排序有時(shí)會(huì)遇到數(shù)據(jù)為空null的情況,這樣排序就會(huì)亂了,這篇文章主要給大家介紹了關(guān)于MySQL中order?by排序時(shí)數(shù)據(jù)存在null則排序在最前面的相關(guān)資料,需要的朋友可以參考下
    2024-06-06
  • Mysql主從同步Last_IO_Errno:1236錯(cuò)誤解決方法

    Mysql主從同步Last_IO_Errno:1236錯(cuò)誤解決方法

    最近遇到Mysql主從同步的Last_IO_Errno:1236錯(cuò)誤問(wèn)題,然后在網(wǎng)上查找相關(guān)解決方案,這里分享給大家,供參考。
    2017-10-10
  • MySQL的線程池原理學(xué)習(xí)教程

    MySQL的線程池原理學(xué)習(xí)教程

    這篇文章主要介紹了MySQL的線程池原理學(xué)習(xí)教程,包括線程池的函數(shù)調(diào)用與關(guān)鍵接口等重要知識(shí),非常推薦!需要的朋友可以參考下
    2015-11-11
  • Mysql5.7.18版本(二進(jìn)制包安裝)自定義安裝路徑教程詳解

    Mysql5.7.18版本(二進(jìn)制包安裝)自定義安裝路徑教程詳解

    這篇文章主要介紹了Mysql5.7.18版本(二進(jìn)制包安裝)自定義安裝路徑教程詳解,需要的朋友可以參考下
    2017-07-07

最新評(píng)論

曲阳县| 宜昌市| 青冈县| 都匀市| 古交市| 赫章县| 延安市| 稻城县| 馆陶县| 思茅市| 资兴市| 石景山区| 辛集市| 临猗县| 临猗县| 浦城县| 阿尔山市| 仁怀市| 封丘县| 宁河县| 龙南县| 洛浦县| 乌拉特后旗| 临颍县| 宜宾市| 光泽县| 浮梁县| 金寨县| 秭归县| 霍山县| 内丘县| 肃宁县| 丰原市| 手游| 苍山县| 株洲市| 兴宁市| 石台县| 平定县| 清徐县| 嘉鱼县|