R2DBC 无法使用 h2 协议创建连接
Posted
技术标签:
【中文标题】R2DBC 无法使用 h2 协议创建连接【英文标题】:R2DBC unable to create connection with h2 protocol 【发布时间】:2020-09-28 07:49:58 【问题描述】:我正在尝试利用 H2 进行测试,并在 src/test/resources/application.yaml
中具有以下配置:
spring:
r2dbc:
url: r2dbc:h2:file://testdb
我有一个用@SpringBootTest
注释的空测试,但是当我运行它时出现以下错误:
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [io.r2dbc.pool.ConnectionPool]: Factory method 'connectionFactory' threw exception; nested exception is java.lang.IllegalStateException: Unable to create a ConnectionFactory for 'ConnectionFactoryOptionsoptions=driver=h2, protocol=file, host=testdb'. Available drivers: [ pool, postgresql, h2 ]
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185)
at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:651)
... 131 common frames omitted
Caused by: java.lang.IllegalStateException: Unable to create a ConnectionFactory for 'ConnectionFactoryOptionsoptions=driver=h2, protocol=file, host=testdb'. Available drivers: [ pool, postgresql, h2 ]
at io.r2dbc.spi.ConnectionFactories.get(ConnectionFactories.java:145)
at org.springframework.boot.autoconfigure.r2dbc.ConnectionFactoryBuilder.build(ConnectionFactoryBuilder.java:125)
at org.springframework.boot.autoconfigure.r2dbc.ConnectionFactoryConfigurations.createConnectionFactory(ConnectionFactoryConfigurations.java:56)
at org.springframework.boot.autoconfigure.r2dbc.ConnectionFactoryConfigurations$Pool.connectionFactory(ConnectionFactoryConfigurations.java:68)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154)
... 132 common frames omitted
这里是来自pom.xml
的相关依赖:
<dependency>
<groupId>io.r2dbc</groupId>
<artifactId>r2dbc-h2</artifactId>
<version>0.8.4.RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.200</version>
<scope>test</scope>
</dependency>
【问题讨论】:
嗨@Jit B,你能解决这个问题吗?我有同样的。谢谢! 【参考方案1】:网址:r2dbc:h2:file:///./data/h2db/testdb
【讨论】:
以上是关于R2DBC 无法使用 h2 协议创建连接的主要内容,如果未能解决你的问题,请参考以下文章
使用Spring Reactive(R2DBC)与MSSQL连接,创建bean时出错
使用 inMemory 数据库时出现 R2dbc H2 问题
无法使用带有 Spring Data 反应式存储库的 Oracle R2DBC 驱动程序执行任何查询