How to reopen connection in JPA - java

I have app in SpringBoot with JPA.
When I lost connection my app send me error:
WARN 6812 --- [io-8080-exec-42] o.h.engine.jdbc.spi.SqlExceptionHelper : SQL Error: 17002, SQLState: 08006
ERROR 6812 --- [io-8080-exec-42] o.h.engine.jdbc.spi.SqlExceptionHelper : IO Error: Socket read timed
out
After estabilishin connection I can't use my EntityManager, because I get:
WARN 6812 --- [io-8080-exec-50] o.h.engine.jdbc.spi.SqlExceptionHelper : SQL Error: 17008, SQLState: 08003
ERROR 6812 --- [io-8080-exec-50] o.h.engine.jdbc.spi.SqlExceptionHelper : Closed Connection
My connection properties:
spring.datasource.driverClassName=oracle.jdbc.driver.OracleDriver
spring.datasource.url=jdbc:oracle:thin:#...:..:..
spring.datasource.username=...
spring.datasource.password=...
spring.datasource.test-on-borrow=true
spring.datasource.test-while-idle=true
spring.datasource.validation-query=SELECT 1;
What should I do to reconnect that DB connection?

Try adding an external connection pool, such as HikariCP, since Spring Boot will automatically detect and use it:
If HikariCP is available we will use it.
If your using a JDBC4 Driver, the connection pool will validate the connection before handing it to you. If you are using an older driver you need to set this property:
connectionTestQuery
If your driver supports JDBC4 we strongly recommend not setting this
property. This is for "legacy" databases that do not support the JDBC4
Connection.isValid() API. This is the query that will be executed just
before a connection is given to you from the pool to validate that the
connection to the database is still alive. Again, try running the pool
without this property, HikariCP will log an error if your driver is
not JDBC4 compliant to let you know. Default: none

Related

SQL Error: 17002, SQLState: 08006 IO Error: A connection attempt failed because the connected party did not properly respond after a period of time

I have an multithreaded spring application which interacts with database with a set of queries and gives the output. I recently encountered this error and not sure exactly what is its cause. As far as managing the connections on the server, I don't see any issues over there and till now everything was getting executed as expected and I haven't made any recent changes.
I see After some time things started working as expected, but now I'm curios about this error and the cause of it just to prevent this in future.
Posting the log trace as below, any help in decoding this will be highly appreciated.
SQL Error: 17002, SQLState: 08006
2021-03-11 13:46:28,508 ERROR [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] (default task-59) IO Error: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond
2021-03-11 13:46:28,548 WARN [org.jboss.jca.adapters.jdbc.WrappedConnection] (default task-59) IJ030041: Error during closing a statement for: java:jboss/datasources/<pool-name>: java.sql.SQLRecoverableException: Closed Connection
at oracle.jdbc.driver.PhysicalConnection.needLine(PhysicalConnection.java:3247)
at oracle.jdbc.driver.OracleStatement.closeOrCache(OracleStatement.java:1392)
at oracle.jdbc.driver.OracleStatement.close(OracleStatement.java:1375)
at oracle.jdbc.driver.OracleStatementWrapper.close(OracleStatementWrapper.java:119)
at oracle.jdbc.driver.OraclePreparedStatementWrapper.close(OraclePreparedStatementWrapper.java:96)
at org.jboss.jca.adapters.jdbc.WrappedStatement.internalClose(WrappedStatement.java:1491)
at org.jboss.jca.adapters.jdbc.WrappedConnection.returnConnection(WrappedConnection.java:286)
at org.jboss.jca.adapters.jdbc.WrappedConnection.close(WrappedConnection.java:256)
at org.hibernate.engine.jdbc.connections.internal.DatasourceConnectionProviderImpl.closeConnection(DatasourceConnectionProviderImpl.java:144)
at org.hibernate.internal.AbstractSessionImpl$NonContextualJdbcConnectionAccess.releaseConnection(AbstractSessionImpl.java:391)
at org.hibernate.engine.jdbc.internal.LogicalConnectionImpl.releaseConnection(LogicalConnectionImpl.java:255)
at org.hibernate.engine.jdbc.internal.LogicalConnectionImpl.close(LogicalConnectionImpl.java:182)
at org.hibernate.engine.jdbc.internal.JdbcCoordinatorImpl.close(JdbcCoordinatorImpl.java:205)
at org.hibernate.engine.transaction.internal.TransactionCoordinatorImpl.close(TransactionCoordinatorImpl.java:297)
at org.hibernate.internal.SessionImpl.close(SessionImpl.java:369)
Hibernate tries to close a statement but the connection (session) has been closed/terminated for some reason:
Error during closing a statement for:
java:jboss/datasources/: java.sql.SQLRecoverableException:
Closed Connection
SQL Error: 17002, SQLState: 08006 IO Error: A connection attempt
failed because the connected party did not properly respond after a
period of time, or established connection failed because connected
host has failed to respond
There could be multiple reason why this happen. Maybe it was a network problem that caused a timeout.
You should check with your DBA if he can see any errors on the database server.
Do you have configured database connection validation for your JDBC datasource?
http://www.mastertheboss.com/jboss-server/jboss-datasource/how-to-automatically-reconnect-to-the-database-in-wildfly
There was a network connection blip between your app server & database server. But what caused it, we can't explain without any more details. If it started working automatically, then it looks like a temporary network issue between the two servers. Or it would have caused if you have used invalid ip/dns name in db server url. But that would require a change from your side to fix the issue.

Not able to connect spring boot application with SQL SERVER Database

I am trying to connect my spring boot application with sql server database but it throwing an Exception and saying :
020-08-18 16:58:11.580 ERROR 14800 --- [ main] com.zaxxer.hikari.pool.HikariPool : HikariPool-1 - Exception during pool initialization.
com.microsoft.sqlserver.jdbc.SQLServerException: The driver could not establish a secure connection to SQL Server by using Secure Sockets Layer (SSL) encryption. Error: "SQL Server did not return a response. The connection has been closed. .
However I am able to connect with oracle database the issue arises when I try to connect with sql server.
here is the application.properties file
spring.datasource.url=jdbc:sqlserver://192.168.*.*\\DB2008;databaseName=mydbname
spring.datasource.username=myuser
spring.datasource.password=mypassword
spring.datasource.driver-class-name=com.microsoft.sqlserver.jdbc.SQLServerDriver
spring.jpa.hibernate.dialect=org.hibernate.dialect.SQLServer2012Dialect
here is the detailed error:
2020-08-18 16:58:11.580 ERROR 14800 --- [ main] com.zaxxer.hikari.pool.HikariPool : HikariPool-1 - Exception during pool initialization.
com.microsoft.sqlserver.jdbc.SQLServerException: The driver could not establish a secure connection to SQL Server by using Secure Sockets Layer (SSL) encryption. Error: "SQL Server did not return a response. The connection has been closed. ClientConnectionId:37aa16bd-92f0-4af0-a090-06f2349cb51a".
at com.microsoft.sqlserver.jdbc.SQLServerConnection.terminate(SQLServerConnection.java:2924) ~[mssql-jdbc-7.4.0.jre8.jar:na]
at com.microsoft.sqlserver.jdbc.TDSChannel.enableSSL(IOBuffer.java:1881) ~[mssql-jdbc-7.4.0.jre8.jar:na]
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLServerConnection.java:2484) ~[mssql-jdbc-7.4.0.jre8.jar:na]
In this case can you can try the following steps.
Make sure that your SQL server is running locally(Are you trying to connect to a remove SQL server?)
Alternatively, In your URL after your Data base name use this statement jdbc:mysql://IP:3306/someDatabase?autoReconnect=true&useSSL=false
I have resolved this issue by following the below steps:
Edit C:\Program Files\Java\jdk1.8.0_251\jre\lib\security\java.security
find the below variable it will have multiple comma separated values remove one value which I have mentioned below:
Find: jdk.tls.disabledAlgorithms=
Remove: 3DES_EDE_CBC
This should fix it!

How can I log actual queries to MongoDB with mongo java driver

I want to see what queries mongo java driver produce, but I'm not able to do that.
Using information from the official documentation I'm able just see in the log that update operation executes, but I don't see the query of this operation.
You can set the logger level for org.mongodb to DEBUG and your Java driver will emit detailed logging like this:
2018-01-18 16:51:07|[main]|[NA]|INFO |org.mongodb.driver.connection|Opened connection [connectionId{localValue:2, serverValue:39}] to localhost:27017
2018-01-18 16:51:07|[main]|[NA]|DEBUG|org.mongodb.driver.protocol.insert|Inserting 1 documents into namespace stackoverflow.sample on connection [connectionId{localValue:2, serverValue:39}] to server localhost:27017
2018-01-18 16:51:07|[main]|[NA]|DEBUG|org.mongodb.driver.protocol.insert|Insert completed
2018-01-18 16:51:07|[main]|[NA]|DEBUG|org.mongodb.driver.protocol.command|Sending command {find : BsonString{value='sample'}} to database stackoverflow on connection [connectionId{localValue:2, serverValue:39}] to server localhost:27017
2018-01-18 16:51:07|[main]|[NA]|DEBUG|org.mongodb.driver.protocol.command|Command execution completed
2018-01-18 16:51:07|[main]|[NA]|DEBUG|org.mongodb.driver.protocol.command|Sending command {findandmodify : BsonString{value='sample'}} to database stackoverflow on connection [connectionId{localValue:2, serverValue:39}] to server localhost:27017
2018-01-18 16:51:07|[main]|[NA]|DEBUG|org.mongodb.driver.protocol.command|Command execution completed
In the above log output you can see the details of a query submitted by the client:
org.mongodb.driver.protocol.command|Sending command {find : BsonString{value='sample'}}
Alternatively, you can enable profiling on the server side ...
db.setProfilingLevel(2)
... causes the MongoDB profiler to collect data for all operations against that database.
The profiler output (which includes the query submitted by the client) is written to the system.profile collection in whichever database profiling has been enabled.
More details in the docs but the short summary is:
// turn up the logging
db.setProfilingLevel(2)
// ... run some commands
// find all profiler documents, most recent first
db.system.profile.find().sort( { ts : -1 } )
// turn down the logging
db.setProfilingLevel(0)
If you're using Spring Boot 1.5.x (I'm on 1.5.19) you'll need to override the version of org.mongodb:mongodb-driver to at least version 3.7.0 to get the additional info in the logs.
See this ticket for more details: https://jira.mongodb.org/browse/JAVA-2698

Connection issue while connecting to HANA DB via JDBC

I am connecting HANA DB via ngdbc.jar. Connection is made properly but after running query 3-4 times connection to HANA DB is lost. When I restart my JAVA server again it works for 3-4 times. Can anyone help?
Error Msg-
WARN [org.hibernate.util.JDBCExceptionReporter] (http--0.0.0.0-8080-6) SQL Error: -708, SQLState: 08006
ERROR [org.hibernate.util.JDBCExceptionReporter] (http--0.0.0.0-8080-6) Data receive failed [Connection reset].
INFO [com.ultimatix.controller.MetricsController] (http--0.0.0.0-8080-6) context setMonthFreezeDateorg.hibernate.exception.JDBCConnectionException: could not execute query
ERROR [org.hibernate.transaction.JDBCTransaction] (http--0.0.0.0-8080-6) JDBC rollback failed: com.sap.db.jdbc.exceptions.jdbc40.SQLNonTransientConnectionException: Connection to database server lost; check server and network status [System error: Socket closed]
I can see you are using Hibernate based on your log.
Can you elaborate a little bit on your stack?
As #RC said, you should consider connection pooling instead of opening direct connections if this is what you are doing in your "JAVA" server.
May be your are keeping the connection open for too long and it times out.
These are all guesses, until you can share any logs or sample code.
One more thing, related to the ngjdbc driver only, there is a "reconnect" connection property which by default is set to false.
Regards

Connection pool getting exhausted

I have a spring boot, hibernate using java app. I deploy it on a jetty webserver with multiple instances. if I have too (greater than 10) many instances I get
com.zaxxer.hikari.pool.HikariPool$PoolInitializationException: Failed to initialize pool: FATAL: remaining connection slots are reserved for non-replication superuser connections
Many of the connections (10x instance) show up idle
ps:
postgres 9104 0.0 0.0 248968 4392 ? Ss 08:07 0:00 postgres: user my_db 127.0.0.1(60095) idle
Hikari trace log for an instance:
2017-02-21 10:59:47.578 DEBUG 7401 --- [l-1 housekeeper] com.zaxxer.hikari.pool.HikariPool : HikariPool-1 - Pool stats (total=10, active=0, idle=10, waiting=0)
setting
hikari.leakDetectionThreshold: 30000
Doesnt log anything interesing. I think this looks interesting HikariCP - connection is not available
Any ideas how i could debug this? Also I am on java 7, so hikari 2.4.7
I think you must increase the max_connections parameter in your PostgreSQL configuration.
See https://wiki.postgresql.org/wiki/Tuning_Your_PostgreSQL_Server
You cannot allow the pool to keep more connections than your PostgreSQL installation allows.

Categories