influxdb failed cause spring application run failed - java

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.

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?

Spring Boot: how to run a server at Spring Context startup and stop it during context shutdown during execution of tests?

My Spring Boot application uses a database server. During the tests, I would like to run an embedded version of the database. The server starts with a random port each time (it's from testcontainers.org).
First thing I tried was to use JUnit4's #ClassRule to start/stop the server, but Spring Boot is smart and re-uses contexts across test classes. So for a single test class everything works fine, but when I run tests in a package (or all tests), they fail due to this lifecycle difference.
Is it possible to somehow hook into tests execution and get a callback when Spring Boot Test infrastructure starts and stops a new context?
The most probable answer I will get is 'just add a server bean to the context when your tests run'. Ok, but here I face another problem:
How do I make sure that the server bean is initialized before other beans that talk to the server? #DependsOn does not seem to fit here as I do not want to have in production a bean annotated with #DependsOn("testServer")
Spring Boot is 2.1.6.

How to know Spring Boot starting errors in details in console

I know that this is a very trivial question but please suggest me to find the way. Sometimes when I start Spring Boot server, it stops but it does not display the complete error/exception stack trace. Think of situation that spring boot server has been started in 8080 port and if somebody starts in 8080 port, it should clearly display that java.net.BindException. But in my case server simply aborts.
I normally find the issue from the IDE like eclipse/Idea when I start the server in debug mode. But how to find the error when somebody starts spring boot server using command prompt ? There may be many errors for which spring boot is unable to start. My question is what configurations should be added in application.properties to know more details about the error/s for which spring boot is not getting started. Currently I am using Spring Boot 2.1.1.RELEASE version. Please help me fix this.
To see more details of the actions being carried out for a spring boot application, change the log level to debug. You can do it just by simply adding below lines in application.property/yaml file.
logging.level.=DEBUG
Or for web applications you can try
logging.level.org.springframework.web: DEBUG
Add the below property in application.xml - worked in Spring boot 2.2.4
logging.level.root:ERROR

Prevent Spring Boot startup failure on couchbase connection error

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.

Categories