I use the H2 database in my development mode with Quarkus. When I need to access the Database with a client as DBeaver, I start the H2 in Mixed mode, like this:
quarkus.datasource.jdbc.url=jdbc:h2:~/h2/oracle_test;MODE=Oracle;AUTO_SERVER=TRUE
These days, I discovered that if I start Quarkus without passing the quarkus.datasource.jdbc.url
property in the application.properties
, like:
quarkus.datasource.db-kind=h2
quarkus.datasource.username=test
quarkus.datasource.password=test
Quarkus is automatically starting the H2 database in Server mode for me:
[INFO] H2 database started in TCP server mode; server status: TCP server running at tcp://127.0.1.1:40003 (only local connections)
In this scenario, I was able to connect to H2 Server at localhost:40003 with DBeaver.
I've used the @ConfigProperty to discover what Quarkus is passing to this parameter:
@ConfigProperty(name = "quarkus.datasource.jdbc.url")
In my test, it was jdbc:h2:tcp://localhost:40003/mem:default;DB_CLOSE_DELAY=-1
I've tried to pass this in my properties:
quarkus.datasource.db-kind=h2
quarkus.datasource.jdbc.driver=org.h2.Driver
quarkus.datasource.jdbc.url=jdbc:h2:tcp://localhost:40003/mem:default;DB_CLOSE_DELAY=-1
But, when I start Quarkus I receive this error:
2021-04-09 17:14:57,549 WARN [io.agr.pool] (agroal-11) Datasource '<default>': Connection is broken: "java.net.ConnectException: Connection refused (Connection refused): localhost:40003" [90067-197]
2021-04-09 17:14:57,551 WARN [org.hib.eng.jdb.env.int.JdbcEnvironmentInitiator] (Quarkus Main Thread) HHH000342: Could not obtain connection to query metadata: org.h2.jdbc.JdbcSQLException: Connection is broken: "java.net.ConnectException: Connection refused (Connection refused): localhost:40003" [90067-197]
at org.h2.message.DbException.getJdbcSQLException(DbException.java:357)
at org.h2.message.DbException.get(DbException.java:168)
at org.h2.engine.SessionRemote.connectServer(SessionRemote.java:451)
at org.h2.engine.SessionRemote.connectEmbeddedOrServer(SessionRemote.java:332)
at org.h2.jdbc.JdbcConnection.<init>(JdbcConnection.java:124)
at org.h2.jdbc.JdbcConnection.<init>(JdbcConnection.java:103)
at org.h2.Driver.connect(Driver.java:69)
at io.agroal.pool.ConnectionFactory.createConnection(ConnectionFactory.java:200)
at io.agroal.pool.ConnectionPool$CreateConnectionTask.call(ConnectionPool.java:452)
at io.agroal.pool.ConnectionPool$CreateConnectionTask.call(ConnectionPool.java:434)
at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
at io.agroal.pool.util.PriorityScheduledExecutor.beforeExecute(PriorityScheduledExecutor.java:65)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1126)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
at java.base/java.lang.Thread.run(Thread.java:834)
Caused by: java.net.ConnectException: Connection refused (Connection refused)
at java.base/java.net.PlainSocketImpl.socketConnect(Native Method)
at java.base/java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:399)
at java.base/java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:242)
at java.base/java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:224)
at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.base/java.net.Socket.connect(Socket.java:609)
at org.h2.util.NetUtils.createSocket(NetUtils.java:103)
at org.h2.util.NetUtils.createSocket(NetUtils.java:83)
at org.h2.engine.SessionRemote.initTransfer(SessionRemote.java:114)
at org.h2.engine.SessionRemote.connectServer(SessionRemote.java:447)
... 12 more
I believe that I have to pass another parameter to make Quarkus to start H2 in Server Mode or Quarkus is doing something more when I don't pass the quarkus.datasource.jdbc.url
anywhere, perhaps it is doing something like this issue - Quarkus JPA with H2 database
I could run my development mode without passing the quarkus.datasource.jdbc.url
parameter, the only inconvenient is that each time Quarkus choose a different port
to the database.
Summarizing, I'm able to run Quarkus in development mode with H2 in Mixed mode and accessing the database from a client. The downside is that H2 at mixed mode creates some files and running it directly with Server Mode would be useful!