【问题标题】:Quarkus mongodb and oraccle in same projectQuarkus mongodb 和 oraccle 在同一个项目中
【发布时间】:2021-09-20 16:25:14
【问题描述】:

我有一个使用 quarkus 运行的项目。如果以 Dev 模式运行将使用 Mongodb,而以 Dev2 模式运行将使用 Oracle。 Mongodb 和 Oracle 中的数据库具有相同的表名、列名。我可以这样做吗?请!

【问题讨论】:

  • 为什么要在本地使用mongo而不是另一个SQL数据库,你可以使用postgresql而不是mongo,它更类似于oracle
  • 我想将 oracle 转换为 mongo,但是现在系统还没有完善,所以我想运行 2 环境
  • 这不是一个好主意,它不可能使用相同的实体来支持开发中的 mongo 和生产中的 oracle,因为您使用的是华丽的,您必须扩展不同的基类

标签: mongodb oracle quarkus quarkus-panache


【解决方案1】:

MongoDB 和 SQL 数据库不共享相同的 API,因此要执行您想要的操作,您必须创建一个接口层并在 rutime 切换实现。

例如可以创建一个接口MyService和两个实现MongoMyService和OracleMyService。

具有挑战性的是在运行时切换实现,因为 Quarkus 只允许在构建时切换 bean,因此两个 bean 都必须使用限定符(@Named 或 @Indentifier)然后在运行时注入 Instance<MyService>,然后你例如,根据配置属性以编程方式选择您需要的实例。

类似的东西(假设你的 bean 是通过 @Named 限定的):

@ConfigProperty("myserver.impl") String myServerImpl;
@Inject Instance<MyService> myServiceInstance;
private MyService myService;

void init(@Observes StartupEvent startupEvent) {
    // select the right impl at runtime
    this.myService = myServiceInstance.select(NamedLitteral.of(myServerImpl)).get();
}

正如@javier-toja 所建议的,这将更容易在 Oracle 和另一个 SQL 数据库(如 Postgres 或 H2)之间切换,因为您将能够共享相同的 impl,并且只能使用 JDBC URL 和驱动程序进行切换。

【讨论】:

  • 如何从 myservice 中的函数获取结果,mongoservice 返回 mongoentity 和 oracle 返回 jpaentity?
  • 您需要使用 DTO 将 MongoEntity 和 JpaEntity 映射到一个通用类型。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-10-01
  • 1970-01-01
  • 2016-02-05
  • 2016-06-13
  • 2011-09-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多