gpt4 book ai didi

java - 使用 Flyway 在 Quarkus 上进行响应式 hibernate

转载 作者:行者123 更新时间:2023-12-04 12:34:07 25 4
gpt4 key购买 nike

在尝试将 Quarkus Flyway 扩展与 Quarkus Reactive Hibernate & RESTEasy 一起使用时,我遇到了问题。启动我的应用程序时,我收到以下错误:

[io.qu.ru.Application] (Quarkus Main Thread) Failed to start application (with profile dev): java.lang.IllegalStateException: Booting an Hibernate Reactive serviceregistry on a non-reactive RecordedState!
at io.quarkus.hibernate.reactive.runtime.boot.registry.PreconfiguredReactiveServiceRegistryBuilder.checkIsReactive(PreconfiguredReactiveServiceRegistryBuilder.java:76)
at io.quarkus.hibernate.reactive.runtime.boot.registry.PreconfiguredReactiveServiceRegistryBuilder.<init>(PreconfiguredReactiveServiceRegistryBuilder.java:66)
at io.quarkus.hibernate.reactive.runtime.FastBootHibernateReactivePersistenceProvider.rewireMetadataAndExtractServiceRegistry(FastBootHibernateReactivePersistenceProvider.java:177)
at io.quarkus.hibernate.reactive.runtime.FastBootHibernateReactivePersistenceProvider.getEntityManagerFactoryBuilderOrNull(FastBootHibernateReactivePersistenceProvider.java:156)
at io.quarkus.hibernate.reactive.runtime.FastBootHibernateReactivePersistenceProvider.createEntityManagerFactory(FastBootHibernateReactivePersistenceProvider.java:82)
以下是相关的 Quarkus 配置:
quarkus:
datasource:
db-kind: "postgresql"
username: "sarah"
password: "connor"
jdbc:
~: true
url: "jdbc:postgresql://localhost:5432/mybase"
reactive:
~: true
url: "postgresql://localhost:5432/mybase"
以及相关的依赖项:
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-hibernate-reactive-panache</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-reactive-pg-client</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-flyway</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-jdbc-postgresql</artifactId>
</dependency>
使用 ~: false 禁用 JDBC 配置避免了异常,但随后应用程序不会在开始时启动 Flyway 迁移。在这种情况下,我会看到以下消息:
[io.qu.ag.de.AgroalProcessor] (build-39) The Agroal dependency is present but no JDBC datasources have been defined.
我发现在一些 Quarkus 问题上确实不可能同时运行响应式(Reactive)和阻塞式数据库连接,但是有没有办法让 Flyway 与 一起工作。响应式(Reactive) 夸克应用 ?

最佳答案

目前,它们似乎确实不能同时支持阻塞 JDBC 和响应式(Reactive) sql 客户端。
一种解决方法是为 Quarkus 运行时禁用 JDBC 并编写您自己的包装器来执行 Flyway 迁移。
以下解决方法基于其对应的 GitHub issue .
Flyway 包装器在应用程序启动时运行:

@ApplicationScoped
public class RunFlyway {

@ConfigProperty(name = "myapp.flyway.migrate")
boolean runMigration;

@ConfigProperty(name = "quarkus.datasource.reactive.url")
String datasourceUrl;
@ConfigProperty(name = "quarkus.datasource.username")
String datasourceUsername;
@ConfigProperty(name = "quarkus.datasource.password")
String datasourcePassword;

public void runFlywayMigration(@Observes StartupEvent event) {
if (runMigration) {
Flyway flyway = Flyway.configure().dataSource("jdbc:" + datasourceUrl, datasourceUsername, datasourcePassword).load();
flyway.migrate();
}
}
}
pom.xml:
<!-- DB -->
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-reactive-pg-client</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-hibernate-reactive</artifactId>
</dependency>

<!-- Flyway -->
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-flyway</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-jdbc-postgresql</artifactId>
</dependency>
应用程序.yml:
myapp:
flyway:
migrate: true
quarkus:
datasource:
db-kind: postgresql
username: myuser
password: mypassword
jdbc: false
reactive:
url: postgresql://localhost:5432/mydb

关于java - 使用 Flyway 在 Quarkus 上进行响应式 hibernate ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66268122/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com