3


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!

Carla
  • 3,064
  • 8
  • 36
  • 65

1 Answers1

1

Yes, you can certainly do that.

Embedding H2 will work OK in JVM mode.

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.

Guillaume Smet
  • 9,921
  • 22
  • 29