带有 H2 数据库的 Quarkus JPA [英] Quarkus JPA with H2 database
问题描述
我正在寻找一种从 Quarkus 应用程序内部启动 H2 数据库的方法.我知道可以使用 @QuarkusTestResource(H2DatabaseTestResource.class)用于编写测试,但是我需要在主应用程序中使用 H2.使用生命周期方法启动/停止 H2 数据库是一个好策略吗?
I'm looking for a way to start H2 Database from inside Quarkus application. I know it's possible to use @QuarkusTestResource(H2DatabaseTestResource.class)
for writing tests, however I'd need to use H2 in the main application. Is it a good strategy to use lifecycle methods to start/stop the H2 Database?
@ApplicationScoped
public class ApplicationLifecycle {
Server server;
void onStart(@Observes StartupEvent event) {
server = Server.createTcpServer("-tcpPort", "9092", "-tcpAllowOthers").start();
}
void onStop(@Observes ShutdownEvent event) {
server.stop;
}
}
还有更好的选择吗?谢谢!
Is there any better option? Thanks!
推荐答案
是的,你当然可以这样做.
Yes, you can certainly do that.
嵌入 H2 在 JVM 模式下可以正常工作.
Embedding H2 will work OK in JVM mode.
但有一点很重要:我们还没有完成在本机模式下运行嵌入式 H2 数据库的工作.因此,这样做可能会阻止您编译为本机应用程序.
But one important point: we haven't done the work on making running an embedded H2 database work in native mode. So doing that will probably prevent you from compiling to a native application.
这篇关于带有 H2 数据库的 Quarkus JPA的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!