Spring Data JPA repository keep connection open in a request - spring

my problem is a bit complicated to explain, but basically seems that there some relation between my number of active connections and the delay to get some data from a remote server (web service). I mean I have a query to get some data in the database and after that a I do a call to this webserver, but everytime the latency of this webservice increase my connection in the database start to increase as well. I'm using Spring data jpa and Spring Boot.

Related

Spring Boot multiple datasources on same database

I was reviewing a code which seems to lose connection to DB approximately once a month intermittently. It leaves no logs on DB side but server prints a log of Connection closed by peer.
Its a Spring Boot App (2.1.9) which uses Spring Integration to read from queues (JMS).
Under pom.xml spring-boot-started-jdbc , Hikari is excluded and tomcat is used.
The app is designed in such a way that it has an in-queue to get traffic, then series of internal queues to split the workload and finally an out queue to the destination.
This app has 4 datasources created all pointing to the same oracle database.
First datasource is used for all CRUD operations in the flow between in-queue and first internal queue. Second datasource is used for all CRUD operations in the flow between first and second internal queue. Third datasource is used between second internal queue and destination queue. Fourth datasource is not used anywhere which suggests some flow was commented but datasource was never removed.
Each datasource has different min and max connections but rest of the properties are same.
Wanted to understand is that a good practice to create
datasources for each spring integration flow pointing to same database?
Can it cause any problems to leave a datasource initialized without using it anywhere?
Is there any obvious reason why HikariCP shouldn't be chosen for production when lot of matrix online show Hikari is superior to Tomcat and Spring boot 2 has chosen it as default for a reason?

If DB connection got lost, will JPA save the saved data later?

I would like to know if Spring JPA will save the data later if the DB connection got lost and then the connection with DB works again.
Assume that we have a myService with the method save(MyEtinty myEntity).
And then I call this method every second.
myService.save(myEntity);
And suddenly the DB connection lost due to the WiFi is ubstable. But efter 10 minutes it works again.
Will JPA buffer the data and then load it into the database after it has restablish the connection?
No, there's no such buffer implemented. You'll have to do it manually, if needed.
If you're using Spring Data JPA repositories, the CrudRepository#save method only checks if the object already exists on the database and then call EntityManager#save or EntityManager#merge.

Is it possible to disable a Spring Boot datasource configuration based on unavailability of a connection to a DB

We have an application that uses several data sources. A DB underlying one of those data sources is down at the moment: IOError. Network adapter couldn't establish the connection & Socket read timed out.
Is there an annotation (or other means) of configuring Spring Boot such that it bypasses the culprit data source and still starts up: the DB is not essential in current development work. spring.datasource.continue-on-error=true doesn't seem to work. This is Spring 2.2.2.RELEASE.
using multiple datasource, so when your apps fail at start up your apps still work, i mean using memory db / sqlite to handle fail at connection error...

How to use Apache Ignite as a layer between Spring Boot app and MongoDB?

I have a Spring Boot application that uses MongoDB. My plan is to store data in a distributed caching system before it gets inserted into Mongo. If the database fails, the caching will have a queue and send to the DB once it is up. So, the plan is to make the caching layer in between the application and Mongo.
Can you suggest some ideas on how to implement this using Apache Ignite?
Take a look at write-behind cache store mode. It retries writing to the underlying database if insertion to the underlying DB fails. Let me know how it works for you.
You can also implement a custom CacheStore for an Ignite cache that will do the caching and enable write through for it. If the connection is lost, then you'll be able to collect entries in a buffer, while retrying to establish the connection back.
See more: https://apacheignite.readme.io/docs/3rd-party-store

Spring Data when does it connect to the database

I have been researching Spring Data Rest especially for cassandra and one of the questions my coworkers and I had was when does Spring Data connect to the database. We don't always want a rest controller to connect to the database so when does spring establish a connection if say we had a class extend the CRUDRepository? Does it connect to the database during the start of application itself? Is that something we can control?
For example, I implemented this example on Spring's website:
https://spring.io/guides/gs/accessing-data-rest/
At what point in the code does spring connect to the database?
Spring will connect to the DB as soon as the Datasource get initialized. Basically, Spring contexts will become alive somehow (Web listeners, manually calling them) and start creating beans. As soon as it reaches the Datasource, connection will be made and the connection pool will be populated.
Of course the above is based on a normal out of the box configuration and everything can be setup up to your taste.
So unless, you decide to control the connections yourself, DB connections will be sitting there waiting to be used.
Disagree with the above answer.
As part of research i initiated the datasource using a bean configuration and then changed my database password(not in my spring application but the real db username password)
The connection stays for a while and then in some point of time (maybe idle time) it stops working and throws credential exception.
This is enough to say the JPA does not keep the connection sitting and waiting to be used but uses some mechanism to occupy/release the db connection as per the need.

Resources