Prevent Spring Boot startup failure on couchbase connection error - java

Using a combination of Spring Boot 1.4.1.Release and Spring Data Couchbase 2.1.3.RELEASE, is there a way to prevent application startup failure if it cannot connect to couchbase?
Current behavior: Application cannot connect to couchbase on startup and exits.
The goal: Application would continue to start even though it cannot connect to Couchbase, use the timeout configurations in CouchbaseEnvironment, and continually try to connect in the background.
In Spring Boot, we can configure Spring JDBC to ignore failure on startup with spring.datasource.continue-on-error=true. Any chance something like that coupled with a retry on failure exists in Spring Data Couchbase?

You can configure CouchbaseEnvironment by overriding getCouchbaseEnvironment using JavaConfig and try increasing the connect timeout. Is there a specific connection failure you are running into?
There isn't continue-on-error property for spring-data-couchbase. This property is available on Spring Boot for relational JDBC and it is useful to ignore initialize failures such as failing to create tables and loading into data sources using scripts.

Related

Spring Batch initialize-schema always will throw error relation "batch_job_instance" already exists

I have a Spring Batch application which will be triggered and started by a Kubernetes CronJob once a day and do some operations.
So it is not needed, that the application will run the whole time and idle until the scheduler starts the job.
To initialize the spring batch schema
spring:
batch:
jdbc:
initialize-schema: always
is used.
By doing so, every time the application is triggered to run, the schema will be initialzed again. This is why I get an error in the database logs which says
[380] ERROR: relation "batch_job_instance" already exists
because the schema will be initialzed always.
I searched for another option than just always or never, but seems nothing else.
I'm using Spring Batch Version 4.3.6 and PostgreSQL.
The spring.batch.jdbc.initialize-schema=always property tells Spring Batch Auto configuration to create the schema everytime the application restarts. This property also silently sets continueOnError= true in the auto-configuration, so even if the error comes it will not fail the application.
You will not observe this behavior locally when with embedded database, if you have any other local DB running on either docker or on system it will throw the error.
It is advisable that in containerize deployment you should set the property to never and do schema creation for batch metadata tables manually.
You can get these DDL script from Spring Batch org/springframework/batch/core/migration package. You can add this to your migration script if you are using Flyway or Liuquibase, or in docker initialization script for your DB service in docker-compose.
Note: Reference Spring Doc here

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?

AWS RDS Proxy Connection from Java Spring Boot

We are using Java Spring Boot 2.3 alongside Postgresql as our database.
For better availability, we want to implement Multi A-Z and RDS Proxy for Postgresql.
In application.properties file, we are using the following properties for normal connection to the database and all works fine
spring.datasource.url=jdbc:postgresql://zzzz.us-east-1.rds.amazonaws.com:5432/postgres
spring.datasource.username=xxx
spring.datasource.password=yyy
When using RDS Proxy, i am trying to use same properties, but instead of the DB URL, i am using the endpoint of the RDS Proxy
spring.datasource.url=jdbc:postgresql://rds-proxy-endpoint.us-east-1.rds.amazonaws.com:5432/postgres
spring.datasource.username=xxx
spring.datasource.password=yyy
For testing purposes, RDS Proxy Security group is open and my machine has access to it, but still it wont let me connect, here is the error
24832 [task-1] ERROR com.zaxxer.hikari.pool.HikariPool --- HikariPool-1 - Exception during pool initialization.
org.postgresql.util.PSQLException: The connection attempt failed.
Am i missing something?
Thanks a lot in advance

influxdb failed cause spring application run failed

I use spring auto-configure to connect influxdb in a spring boot command line application.
the application will run failed, when influxdb is suddenly down.
I Autowired the Influxdb instance in Application, and when inflxudb is down, my application runs failed.
I didn't find any information about reconnecting to influxdb in influx-java or spring auto-configure.
How should I config to reconnect to the influxdb?
You didn't paster your code, but I think if your db connection faild cause your spring application failed, it usually means, you did NOT catch all of your exception and throw to upper level, and at last spring framework can't handle the exception and run failed.
So check your code around the database action, and catch all the exception, I think , at least, your spring application will not failed.
If you have any question, please show us your code.

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).

Categories