Resetting DB connection in Spring boot at runtime after getting connection closed - java

I am getting error connection reset in my running spring boot app for db2 database. On restarting the application the connection starts working.
If I receive connection closed or connection reset error, I need to reconnect to the database at runtime.
What will be the best way to do this?
I am using Hikari connection pool , Is there some property which allows the database connection to be checked ..if broken ..then reconnect.
Or some spring boot configuration like #RefreshScope recreates the bean after configuration changes. Any spring boot bean which might help in this case?
Thanks for the help.

I have same issue, I try config param for hiraki but still getting connection is closed.
You can try the way by this link https://www.linkedin.com/pulse/spring-boot-retry-database-connection-startup-abdelghani-roussi?articleId=6723332249346981888

Related

how to configure a connection-ready callback function for Hikari cp in spring boot

I want to get notified after successfully creating the connections. As an example, due to some connection issue, the connections go down and the connection will be successful after a while. then I want to be updated. can I configure a method to be executed in spring boot hikariCP?

Connection closed exception when using Oracle Weblogic's JNDI datasource

I'm making use of Oracle Weblogic's (12.1.3) JNDI datasource for connection pooling. The max idle time for a connection is set to 30 seconds in the admin console. But, there is this method that takes more than a minute to complete, which results in the connection getting automatically closed.
My problem is that I have set autoCommit as false, and I'm unable to rollback the transaction in case of an Exception. I'm making use of pure JDBC. I tried using Spring's JdbcTemplate, surprisingly there is no Connection related issue when I'm making use of the #Transactional annotation.
Is there a technique using which I can keep the connections that I get from the Weblogic's JNDI datasource alive for more than 30 seconds, without modifying the configurations in the admin console?

How to automatically reconnect a DataSource connection in Spring?

Problem: when my spring application is running, and meanwhile the database server is stopped/restarted, then then db connection is lost and never restored.
I tested as follows:
execute query: OK
service mysql stop
execute query: exception:
Could not open JPA EntityManager for transaction; nested exception is javax.persistence.PersistenceException:
com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: No operations allowed after connection closed.
service mysql start
execute query: still exception!
Question: how can I tell spring DataSource to automatically reconnect after connection has been lost?
This is my configuration:
spring.datasource.url=jdbc:mysql://localhost/tablename?useSSL=false
spring.datasource.username=root
spring.datasource.password=rootpw
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.tomcat.validation-query=SELECT 1
spring.datasource.tomcat.validation-query-timeout=5000
spring.datasource.tomcat.test-on-borrow=true
spring.datasource.tomcat.test-on-connect=true
I'm using a connection pool like HikariCP to do a reconnect automatically.
see the example at Stackoverflow: How do I configure HikariCP in my Spring Boot app in my application.properties files?
My bad, I had a misconfiguration. The config above is all that's needed for auto reconnect.

How do I get Spring Boot to automatically reconnect to PostgreSQL?

I am running Spring Boot connecting to a PostgreSQL database. I have verified that data is written to the database if Spring Boot is started after the database.
spring.datasource.url = jdbc:postgresql://localhost/dbname
spring.datasource.username = user
spring.datasource.password = secret
spring.datasource.driver-class-name = org.postgresql.Driver
spring.datasource.testOnBorrow=true
spring.datasource.validationQuery=SELECT 1
When there are changes in the database in development, I run into exceptions:
PreparedStatementCallback; SQL []; This connection has been closed.; nested exception is org.postgresql.util.PSQLException: This connection has been closed.
Note that the database is accessible again when the exceptions occur; I think the problem is that connection is stale because the database endpoint it originally connected to is no longer available because of the restart. Restarting Spring Boot resolves the issue.
How do I configure Spring Boot to reconnect to PostgreSQL so that I do not need to restart it?
I have attempted to follow the answers in Spring Boot JPA - configuring auto reconnect and How to reconnect database if the connection closed in spring jpa?. I am not sure whether the PostgreSQL behavior is different from MySQL. My reading of the Spring Boot documentation has not helped; I do not know the components described well enough to understand what documentation I should be looking at.
Spring boot should be configured to reconnect automatically, problem is that it is not aware of the broken connection.
Since you are already using test on borrow and validation query, just try reducing validation interval so it is executed every time.
spring.datasource.tomcat.test-on-borrow=true
spring.datasource.tomcat.validation-query=SELECT 1
spring.datasource.tomcat.validation-interval=0
Tomcat jdbc connection pool on testOnBorrow:
(boolean) The indication of whether objects will be validated before being borrowed from the pool. If the object fails to validate, it will be dropped from the pool, and we will attempt to borrow another. NOTE - for a true value to have any effect, the validationQuery or validatorClassName parameter must be set to a non-null string. In order to have a more efficient validation, see validationInterval. Default value is false
But be aware of validationInterval:
(long) avoid excess validation, only run validation at most at this frequency - time in milliseconds. If a connection is due for validation, but has been validated previously within this interval, it will not be validated again. The default value is 30000 (30 seconds).

jdbc connection error: not associated with a managed connection

My application is trowing the following exception:
java.sql.SQLException: Connection is not associated with a managed connection.org.jboss.resource.adapter.jdbc.jdk6.WrappedConnectionJDK6#4fe91321
This is happening in production I'm not able to get this problem in development and for that I'm not able to solve it.
The root of the exception is code line dbConn.prepareStatement(sql);
From what I was able to find in the web, the cause for this can be:
Over jdbc connection, a jboss connection is wrapped but the wrapper is empty. The original jdbc connection inside is no longer there.
JDBC Connection is already closed and trying to work with close connection is the reason why I'm getting this exception.
The transaction manger detects transaction that is taking to long then the transaction timeout.
So if someone can point me what is the problem because I'm not able to get this problem in mine development environment.
Also what logs can I add that will help me detect the problem in production - I'm using Java, JBoss, Postgre.
I'have enable connection close monitoringa, and also to add is that the issue is not consistent
I just resolved the issue.
It's my jndi driver not compatible with the latest java frameworks.
I'm using Oracle db and ojdbc6, and i download a latest ojdbc6 from maven repository and replace the old same name jar file. It works like bird now.
If you are using an EJB, it's possible that your Stateless Session Bean Pool is not big enough. The connection can't find an associated thread.
Maybe you have an extended Transaction for a not Stateful Session Bean, wich is maybe not a good idea.

Categories