这篇文章主要讲解了“什么是h2和r2dbc-h2”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“什么是h2和r2dbc-h2”吧!
什么是H2数据库呢?
H2是一个Java SQL database,它是一个开源的数据库,运行起来非常快。
H2流行的原因是它既可以当做一个独立的服务器,也可以以一个嵌套的服务运行,并且支持纯内存形式运行。
H2的jar包非常小,只有2M大小,所以非常适合做嵌套式数据库。
如果作为嵌入式数据库,则需要将h3*.jar添加到classpath中。
下面是一个简单的建立H2连接的代码:
import java.sql.*; public class Test { public static void main(String[] a) throws Exception { Connection conn = DriverManager. getConnection("jdbc:h3:~/test", "sa", ""); // add application code here conn.close(); } }
如果给定地址的数据库并不存在,
同时H2还提供了一个简单的管理界面,使用下面的命令就可以启动H2管理界面:
java -jar h3*.jar
默认情况下访问http://localhost:8082就可以访问到管理界面:
r2dbc-h3是r2dbc spi的一种实现。同样的使用r2dbc-h3也提供了两种h3的模式,一种是文件系统,一种是内存。
同时还提供了事务支持,prepared statements和batch statements等特性的支持。
要想使用r2dbc-h3,我们需要添加如下依赖:
<dependency> <groupId>io.r2dbc</groupId> <artifactId>r2dbc-h3</artifactId> <version>${version}</version> </dependency>
如果你体验snapshot版本,可以添加下面的依赖:
<dependency> <groupId>io.r2dbc</groupId> <artifactId>r2dbc-h3</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>
h3有两种连接方式,file和内存,我们分别看一下都是怎么建立连接的:
ConnectionFactory connectionFactory = ConnectionFactories.get("r2dbc:h3:mem:///testdb"); Publisher<? extends Connection> connectionPublisher = connectionFactory.create();
ConnectionFactory connectionFactory = ConnectionFactories.get("r2dbc:h3:file//my/relative/path"); Publisher<? extends Connection> connectionPublisher = connectionFactory.create();
我们还可以通过ConnectionFactoryOptions来创建更加详细的连接信息:
ConnectionFactoryOptions options = builder() .option(DRIVER, "h3") .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](https://my.oschina.net/u/2966482) -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:h3:file//../relative/file/name r2dbc:h3:file///absolute/file/name r2dbc:h3: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-h3是怎么来进行批处理的:
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();
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();
感谢各位的阅读,以上就是“什么是h2和r2dbc-h2”的内容了,经过本文的学习后,相信大家对什么是h2和r2dbc-h2这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。