quarkusio / quarkus

Quarkus: Supersonic Subatomic Java.
https://quarkus.io
Apache License 2.0
13.68k stars 2.65k forks source link

H2 database connection not working #3779

Closed chaminnk closed 4 years ago

chaminnk commented 5 years ago

Describe the bug When trying to connect to H2 database after containerizing the native build, connection is broken error is prompted. Expected behavior Connects to H2 database and creates a table.

Actual behavior 2019-08-30 03:25:35,923 INFO [io.quarkus] (main) Quarkus 0.21.1 started in 0.003s. Listening on: http://0.0.0.0:8080 2019-08-30 03:25:35,923 INFO [io.quarkus] (main) Installed features: [agroal, cdi, jdbc-h2, narayana-jta, resteasy] Connecting to database... org.h2.jdbc.JdbcSQLException: Connection is broken: "java.net.ConnectException: Connection refused: localhost" [90067-197] at org.h2.engine.SessionRemote.connectServer(SessionRemote.java:451) at org.h2.engine.SessionRemote.connectEmbeddedOrServer(SessionRemote.java:332) at org.h2.jdbc.JdbcConnection.(JdbcConnection.java:124) at org.h2.jdbc.JdbcConnection.(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:357) at io.agroal.pool.ConnectionPool$CreateConnectionTask.call(ConnectionPool.java:346) at java.util.concurrent.FutureTask.run(FutureTask.java:266) at io.agroal.pool.util.PriorityScheduledExecutor.beforeExecute(PriorityScheduledExecutor.java:64) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1146) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) at java.lang.Thread.run(Thread.java:748) at com.oracle.svm.core.thread.JavaThreads.threadStartRoutine(JavaThreads.java:460) at com.oracle.svm.core.posix.thread.PosixJavaThreads.pthreadStartRoutine(PosixJavaThreads.java:193) Caused by: java.net.ConnectException: Connection refused at java.net.PlainSocketImpl.socketConnect(PlainSocketImpl.java:5143) at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350) at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206) at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188) at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) Goodbye! at java.net.Socket.connect(Socket.java:589) 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) ... 14 more

To Reproduce Steps to reproduce the behavior:

  1. Create a Quarkus project with a resource
  2. Copy the contents of the file in the url, https://drive.google.com/file/d/1b02dvUeW8JsUdH-iGzSKjO4ITotxTyLK/view?usp=sharing , to the resource.java file
  3. Configure the contents in the application.properties file
  4. Build and run the native image using Dockerfile.multistage

Configuration

# Configuration file
# key = value
quarkus.datasource.h2source.url=jdbc:h2:tcp://localhost/~/test
quarkus.datasource.h2source.driver=org.h2.Driver
quarkus.datasource.h2source.username=admin
quarkus.datasource.h2source.password=admin
quarkus.datasource.h2source.min-size=3
quarkus.datasource.h2source.max-size=13

Environment (please complete the following information):

geoand commented 5 years ago

Hi,

Where does Dockerfile.multistage come from?

chaminnk commented 5 years ago

Hi, This is my Dockerfile.multistage


 ## Stage 1 : build with maven builder image with native capabilities
FROM quay.io/quarkus/centos-quarkus-maven:19.2.0 AS build
COPY src /usr/src/app/src
COPY pom.xml /usr/src/app
USER root
RUN chown -R quarkus /usr/src/app
USER quarkus

RUN mvn -f /usr/src/app/pom.xml -Pnative clean package
WORKDIR /usr/src/app/target/

## Stage 2 : create the docker final image
FROM registry.access.redhat.com/ubi8/ubi-minimal
WORKDIR /work/
COPY --from=build /usr/src/app/target/*-runner /work/application
RUN chmod 775 /work
EXPOSE 8080
CMD ["./application", "-Dquarkus.http.host=0.0.0.0"]
machi1990 commented 5 years ago

It seems that the H2 database is not started in TCP mode.

Did you try using the in memory mode?

chaminnk commented 5 years ago

I tried the memory mode first, but it prompts an error org.h2.jdbc.JdbcSQLException: Database may be already in use: null. That's why I used TCP mode.

gastaldi commented 4 years ago

@chaminnk are you still hitting this error? I tried in a sample project and it works. Here is my application.properties:

# configure your datasource
quarkus.datasource.url=jdbc:h2:mem:test
quarkus.datasource.driver=org.h2.Driver

# drop and create the database at startup (use `update` to only update the schema)
quarkus.hibernate-orm.database.generation = drop-and-create
geoand commented 4 years ago

I used H2 in TCP mode extensively this week and never ran into any problems

gastaldi commented 4 years ago

Closing as the error is caused because the H2 Database is not started in TCP mode during the Docker build and couldn't reproduce the problem with the memory mode.

Please reopen if necessary.

asamal commented 3 years ago

@gastaldi if TCP connection doesn't work then why would not update the example quarkus.datasource.users.jdbc.url=jdbc:h2:tcp://localhost/mem:users here in the documentation to the correct value?

gastaldi commented 3 years ago

@asamal sure, I think that makes sense. Do you mind providing a pull request with this change? Here is the file: https://github.com/quarkusio/quarkus/blob/main/docs/src/main/asciidoc/datasource.adoc