Created
June 17, 2025 09:43
-
-
Save stephankoelle/84b6fc216b53eeda222d62eb37937c24 to your computer and use it in GitHub Desktop.
Start h2 server in a quarkus app, to connect also from a SQL tool (for example intellij) to the dev database
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import io.quarkus.runtime.ShutdownEvent; | |
| import io.quarkus.runtime.StartupEvent; | |
| import jakarta.inject.Inject; | |
| import org.h2.tools.Server; | |
| import jakarta.enterprise.context.ApplicationScoped; | |
| import jakarta.enterprise.event.Observes; | |
| import org.jboss.logging.Logger; | |
| import java.sql.SQLException; | |
| @ApplicationScoped | |
| public class H2ServerStarter { | |
| private Server server; | |
| @Inject | |
| Logger log; | |
| void onStart(@Observes StartupEvent ev) { | |
| try { | |
| if (server == null || !server.isRunning(false)) { | |
| server = Server.createTcpServer("-tcpAllowOthers", "-tcpPort", "9092").start(); | |
| log.info("H2 server started on port 9092."); | |
| } else { | |
| log.info("H2 server is already running on port 9092."); | |
| } | |
| } catch (SQLException e) { | |
| throw new RuntimeException("Failed to start H2 server.", e); | |
| } | |
| } | |
| void onStop(@Observes ShutdownEvent ev) { | |
| if (server != null && server.isRunning(false)) { | |
| server.stop(); | |
| log.info("H2 server stopped."); | |
| } | |
| } | |
| } | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| connect to: | |
| jdbc:h2:tcp://localhost:9092/file:/ ... FULL path ..../data/flow-deploy-app |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment