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!
Related
So I have an Azure App Service and I want it to be able to connect to mongo db atlas. For our Atlas setup there is a peering connection between our managed environment on azure (and the vnet it all sits in) and our mongo db cluster.
The mongo cluster is setup with "Connect via peering only" which we can't change. This means we can't connect to our db with any old public ip address -It has to come from the vnet it's peered with. The vms (from within the peered vnet) are able to access the db no problem.
I tried with the app service to connect to the db with the same connection string that worked for the vm. It returned a the error:
java.net.ConnectException: Connection refused (Connection refused)}}, {address=MYMONGOADDRESS.mongodb.net:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketOpenException: Exception opening socket}, caused by {java.net.ConnectException: Connection refused (Connection refused)}}]
This is expected due to the reasons above. However I setup the vnet integration for the app service
. I created a dedicated subnet as described for the app service and whitelisted the ip range in mongo. However I still couldn't connect to the db after using this vnet integration. I also got a very similar error but slightly different.
{java.net.SocketTimeoutException: connect timed out}}, {address=MYMONGOADDRESS.mongodb.net:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketOpenException: Exception opening socket}, caused by {java.net.SocketTimeoutException: connect timed out}}]
Where instead of a connection refused I'm not getting a connection time out. I've repeated this a few times and get the same result.
Anyone know why these errors are different and any help in being able to connect to the db from my azure app service?
Is there any firewall or network security group? What the network peering status is? If it shows « initiated » then this means it has not been completed. Network peering has to be done on each ends. If it shows « connected », then I would look for firewall or network security groups.
See this
https://docs.atlas.mongodb.com/security-vpc-peering/#vpc-peering
No idea if this can apply to your case but this question was the first and only that came up when I was searching for something similar.
In my case I have a MongoDB Atlas deployed on Azure and an app running in AKS. I set up peering as documented but still could not connect from the app container in AKS to the MongoDB. Turns out the problem was an incorrect connection string.
Atlas has a different URL when connecting directly and when connecting through a peer network:
# Direct connection
mongo "mongodb+srv://<cluster>.<something>.mongodb.net/<dbname>"
# Peered connection (note the -pri)
mongo "mongodb+srv://<cluster>-pri.<something>.mongodb.net/<dbname>"
As an additional note, I configured the Atlas Network Acces IP address list with the CIDR of the Azure VN Subnet used in AKS. (Under Virtual Network > Subnets > IPv4)
When I run maven test locally is passed. But got this error when I run it on CI server.
Error Message
Could not open JPA EntityManager for transaction; nested exception is org.hibernate.exception.JDBCConnectionException: Unable to acquire JDBC Connection
Stacktrace
org.springframework.transaction.CannotCreateTransactionException: Could not open JPA EntityManager for transaction; nested exception is org.hibernate.exception.JDBCConnectionException: Unable to acquire JDBC Connection
Caused by: org.hibernate.exception.JDBCConnectionException: Unable to acquire JDBC Connection
Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:
Communications link failure
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
Caused by: java.net.UnknownHostException: mysql
When running local test, they all passed, maven test default setting provided by IntelliJ IDEA is used.
Since the error complains about database connection, so I checked by Jenkins Audit to Database Plugin. Connection Successful!
The connection parameter in my application.properties also corresponds to this
spring.datasource.url=jdbc:mysql://mysql:3306/database?useLegacyDatetimeCode=false&serverTimezone=Asia/Shanghai
spring.datasource.username=root
spring.datasource.password=password
spring.datasource.maxActive=5
The MySQL in the URL is the MySQL docker container name. If change it with localhost or private IP in docker container inspect mysql the error message is the same, while the Stacktrace is a little different on last two lines.
for localhost
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
Caused by: java.net.ConnectException: Connection refused (Connection refused)
for private IP
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
Caused by: java.net.SocketTimeoutException: connect timed out
The different I think is the host in URL, localhost is used for the local test.
While the Jenkins server used Docker bridge network.
The container status is:
docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS
NAMES
51ea7c7864a4 mysql:5.7 "docker-entrypoint.s…" 19 hours ago Up 19 hours 0.0.0.0:3306->3306/tcp mysql
de364f7b5eaf maven:3-jdk-8 "/usr/local/bin/mvn-…" 21 hours ago Up 21 hours
optimistic_stallman
a6545591e358 jenkinsci/blueocean "/sbin/tini -- /usr/…" 43 hours ago Up 43 hours 0.0.0.0:50000->50000/tcp, 0.0.0.0:2048->8080/tcp frosty_cray
When I run the JUnit test in IntelliJ, it fails sometimes on the local environment. The error log is like:
Caused by: org.h2.jdbc.JdbcSQLException: Schema "DATABASE" not found; SQL statement:
TRUNCATE TABLE database.data_log
I have searched the issue, it's said h2 database use upper case by default.
After run maven test, this issue will go if run JUnit test in IDE again. But this should be not related to the root cause.
Search on the error message, find some similar question but with different nested exception:
Could not open JPA EntityManager for transaction; nested exception is javax.persistence.PersistenceException
SpingREST: Could not open JPA EntityManager for transaction; nested exception is org.hiberna
Could not open JPA EntityManager for transaction; org.hibernate.exception.GenericJDBCException: Could not open connection
Could not open JPA EntityManager for transaction in spring
All of them is about nested exception is javax.persistence.PersistenceException
But nested exception is org.hibernate.exception.JDBCConnectionException: is my situation.
Read Connect Java to a MySQL database
however since that plugin connects OK, means the connection from Jenkins container to MySQL container is fine.
Summarise:
1. local test with maven passed
2. Jenkins plugin connect to MySQL success
3. Integration test fails when run from Jenkins
4. local test environment is WIN10 64bit; Jenkins run in docker container on Ubuntu 16.04 64bit server, with MySQL 5.7 container connects to the same bridge network.
Thanks to #rohit-thomas. We narrow down the question to something related to the host of the URL.
The simple answer is changing the host of the JDBC URL in the spring boot application.properties to the docker host IP address. From
spring.datasource.url=jdbc:mysql://mysql:3306/database?
to
spring.datasource.url=jdbc:mysql://172.17.0.1:3306/database?
From inside of a Docker container, how do I connect to the localhost of the machine?
This post also helps as the final solution.
ip addr show docker0
4: docker0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default
...
inet 172.17.0.1/16 brd 172.17.255.255 scope global docker0
...
My conclusion is:
The Jenkins container builds from an image is able to communicate with the MySQL container with its container name or private address on docker bridge network. However, as the application built by the Jenkins is not able to do that.
Since the MySQL container port has bound to the host machine, the application could communicate with MySQL container through the host port.
If the conclusion is wrong, comments are welcome.
Some things you can check that might help you resolve this issue.
In application.properties try to use docker host IP address.
from
spring.datasource.url = jdbc:mysql://mysql:3306/DATABASE_URI_PATH
to
spring.datasource.url = jdbc:mysql://192.168.99.100:33060/DATABASE_URI_PATH
Note:
You will need to map your IP and port when you do docker run or ports in docker file.And use the same docker network among your containers.
Verify if you server app can reach your mysql or vice versa. Go inside the docker container and try to ping.
I am new to Mongo DB, and I didn't find any good resource/example for that. What I'd like to achieve is have a MongoDB running on my server, with authentication turned on. And a database for my application, with a user who can access the data.
I have mongo up and running and now try to connect to the db:
spring:
data:
mongodb:
port: 27017
host: <host name>
database: test
But I get connection refused all the time.
com.mongodb.MongoSocketOpenException: Exception opening socket
at com.mongodb.connection.SocketStream.open(SocketStream.java:62) ~[mongodb-driver-core-3.6.3.jar:na]
at com.mongodb.connection.InternalStreamConnection.open(InternalStreamConnection.java:126) ~[mongodb-driver-core-3.6.3.jar:na]
at com.mongodb.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:114) ~[mongodb-driver-core-3.6.3.jar:na]
at java.base/java.lang.Thread.run(Thread.java:844) [na:na]
Caused by: java.net.ConnectException: Connection refused (Connection refused)
at java.base/java.net.PlainSocketImpl.socketConnect(Native Method) ~[na:na]
at java.base/java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:400) ~[na:na]
at java.base/java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:243) ~[na:na]
at java.base/java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:225) ~[na:na]
at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:402) ~[na:na]
at java.base/java.net.Socket.connect(Socket.java:591) ~[na:na]
at com.mongodb.connection.SocketStreamHelper.initialize(SocketStreamHelper.java:59) ~[mongodb-driver-core-3.6.3.jar:na]
at com.mongodb.connection.SocketStream.open(SocketStream.java:57) ~[mongodb-driver-core-3.6.3.jar:na]
There are plenty of resources including a getting started guide on the Spring web site. The MongoDB documentation has detailed instructions on how to add users and enable authentication. The Spring Boot reference documentation describes how to configure the mongodb connection, including credentials.
But your output above says "Connection refused".
So either your mongodb isn't actually up and running, or your configuration points to the wrong server. Double check the host name (which you decided to hide in your question), and log on to the mongodb server and check that the mongod process is actually running. Then you can continue to work on the authentication.
I am using spring boot for my rest api but I have a problem about mongo database. Application sometimes throws mongo socket exception and does not execute following codes, when endpoint which needs mongo operation is triggred. I assign true to value of socketkeepalive but it did bot solve my problem. How can I get rid of this problem and can you offer me spring boot mongo db configuration values that are suitable?
By the way, program is working properly. But sometimes it throws this exception.
Thanks
INFO org.mongodb.driver.cluster - Exception in monitor thread while connecting to server **.***.***.***:42015
com.mongodb.MongoSocketOpenException: Exception opening socket
at com.mongodb.connection.SocketStream.open(SocketStream.java:63)
at com.mongodb.connection.InternalStreamConnection.open(InternalStreamConnection.java:115)
at com.mongodb.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:127)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.net.ConnectException: Connection refused (Connection refused)
at java.net.PlainSocketImpl.socketConnect(Native Method)
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)
at java.net.Socket.connect(Socket.java:589)
at com.mongodb.connection.SocketStreamHelper.initialize(SocketStreamHelper.java:57)
at com.mongodb.connection.SocketStream.open(SocketStream.java:58)
... 3 common frames omitted
Spring Boot has a feature called "auto configuration". In this case, as soon as the Mongo driver is detected on the classpath, the MongoAutoConfiguration is activated with default values, which point to localhost:27017. If you don't want that behaviour, you can now either configure the properties for MongoDB (see http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-mongodb for valid property keys) or disable the MongoAutoConfiguration:
#SpringBootApplication(exclude = {MongoAutoConfiguration.class, MongoDataAutoConfiguration.class})
Spring boot throws this exception when Mongo DB is not running. Please make sure that Mongodb is running. It got resolved for me after starting Mongo DB.
you can check if mongoDB is running on 27017 is running or not. use this code in your terminal
netstat -plntu
And Please show me your configurations file or properties file.
Got the same issue while running mongo inside a docker stack and i just missed the port mapping from 27017 (external) to 27017 (internal), so applying
ports:
- 27017:27017
to my docker-compose.yml file worked for me.
Here is the scenario
I am trying to connect to mysql database server from the core java app using spring. My application and server runs on different box i.e. Have limited ports open
Here is what I have done soo far
I am able to telnet to port 5506 (on which my database server run ) from the client machine
I am able to connect to database server from command prompt(mysql client) from client machine
3.I tried all driver version from 5.0.8 to latest
and off course same code works on everywhere except production :)
Somehow I started thinking that Java process required more ports to be open
Here is the exception
org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is com.mysql.jdbc.CommunicationsException: Communications link failure due to underlying exception:
** BEGIN NESTED EXCEPTION **
java.net.ConnectException
MESSAGE: Connection refused
STACKTRACE:
java.net.ConnectException: Connection refused
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:327)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:193)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:180)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:384)
at java.net.Socket.connect(Socket.java:546)
at java.net.Socket.connect(Socket.java:495)
at java.net.Socket.<init>(Socket.java:392)
at java.net.Socket.<init>(Socket.java:235)
at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:256)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:271)
at com.mysql.jdbc.Connection.createNewIO(Connection.java:2771)
at com.mysql.jdbc.Connection.<init>(Connection.java:1555)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:285)
at java.sql.DriverManager.getConnection(DriverManager.java:620)
at java.sql.DriverManager.getConnection(DriverManager.java:169)
at org.springframework.jdbc.datasource.DriverManagerDataSource.getConnectionFromDriverManager(DriverManagerDataSource.java:173)
at org.springframework.jdbc.datasource.DriverManagerDataSource.getConnectionFromDriver(DriverManagerDataSource.java:164)
at org.springframework.jdbc.datasource.AbstractDriverBasedDataSource.getConnectionFromDriver(AbstractDriverBasedDataSource.java:149)
at org.springframework.jdbc.datasource.AbstractDriverBasedDataSource.getConnection(AbstractDriverBasedDataSource.java:119)
at org.springframework.jdbc.datasource.DataSourceUtils.doGetConnection(DataSourceUtils.java:113)
at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:79)
at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:381)
at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:455)
at org.springframework.jdbc.core.JdbcTemplate.queryForRowSet(JdbcTemplate.java:498)
at
Have you specified the 5506 port in the connection URL? This port is not the default one for MySQL (3306).