spring retryable database connection - java

I have a spring-boot application that I'm deploying to an Azure App Service. This application connects to a MySQL server. I would like that the application would retry the database connection. I looked around for a solution and I found this blog post.
My question: is there any spring setting that would do this? I honestly expected that the database connection would retry by default since it's kinda annoying to break the app if for a split second the connection drops.

Related

Why is the MySQL connection getting closed when running from Azure Web Service with Spring boot & JPA

I have a web service on Azure, with a spring boot application. This application uses JPA to communicate with a MySQL server.
Running this application on localhost has not given me any issues, but as soon as I published the code to the web service, problems started to arise.
My main problem right now is that whenever I am not using the application for around 5 minutes, the connection to MySQL is getting dropped and it throws an error whenever I try to access it again: com.mysql.cj.exceptions.ConnectionIsClosedException: No operations allowed after connection closed.
And it never opens another connection, which means I have to restart my app.
I have been looking online for a while now for a solution and have only found that I should try putting ?autoReconnect=true in my connection string, which unfortunately did not fix this issue.
Hope anyone can help.

Connecting to existing in-memory H2 from different application/process

I have a simple Quarkus application that uses an in-memory H2 database. The JDBC connection string has some custom settings: jdbc:h2:mem:quarkus_db;DATABASE_TO_LOWER=TRUE;DB_CLOSE_DELAY=-1;MODE=MariaDB.
I'm trying to connect to the exact same database from a database manager — say DataGrip. After following the documentation, several answers from StackOverflow, and some blog posts, I'm still unable to connect to the same instance and query for any existing data using SQL.
There is something I don't fully understand from the examples. I've seen that initially using a connection like this in the application: jdbc:h2:mem:quarkus_db, and while the same it's up and running, then on the other application/process (DataGrip in my case) using jdbc:h2:tcp://localhost/mem:quarkus_db should work, but I fail to see how the first scheme will create a TCP server.
My goal is to verify some data while the application is running. I understand that when using something like jdbc:h2:mem:*, everything will be lost after the application stops.

Multiple ORACLE DB connections created while running 1 spring boot app

I am trying to understand the correlation between Database connections and spring boot app. My spring boot app is connecting to one schema and I am running 4 such spring boot apps on my system. Each app is connecting to a different schema.
The problem is that these 4 apps are acquiring 50 oracle db connections, but when I close all apps and open DB through oracle sql developer only one connection is acquired.
I don't have enough rep to post this as a comment.
By default Spring Boot uses HikariCP as a connection pooling framework. Some good information can be found at Baeldung, which I recommend using as it covers lots Spring Boot functionality and is almost always up to date.
https://www.baeldung.com/hikaricp
Spring-boot specific information:
https://www.baeldung.com/spring-boot-hikari
While 10 connections are not at all required, you can 'play' around with how much is best (or most optimum) for your app and set it as best fits your design. Usually this will be done by optimizing this as the need arises in your application. 10 is what spring/hikari identifies as a good starting point for most projects.
https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#data.sql.datasource.connection-pool
Since I had to post this as an answer I'll go a little more in depth in your actual question:
If we think of a connection to a database without a pool we can think of:
The application requests the driver to open a connection to your database
A socket is opened between your application and the database
You are authenticated to the database
Your query runs and the connection is closed
This is fine in a small application without many requests going through, but hopefully you can see the issue here as we scale and get more users. Removing the first 3 steps can make a huge difference.
It should also be noted that while your database holds 3 connections, the connections will be idle and not generating much (if any) load on your database.

running springMVC project on tomcat but I have to restart the web server every month. what should I do?

Hi brilliant stackoverflow users.
I've got a problem with my apache tomcat web server I didn't have before with other projects
I'm running a website using SpringMVC and JPA and also I'm using MySql database. First days I had 503 error every week and I found out that my database connection is always up and after each transaction it will not close the database connection so the web server crashed after too many requests.
Now I managed that problem using spring transaction manager.
Now I have no error, nothing at all. My log file is clear as pure water but my web server crashes every month, so I have to restart my tomcat.
any thoughts?
I've also tried spring session manager but it didn't work.
my apache tomcat is 8.0v
I'm using java 8
and my spring version is 3.7.0

How to use JDBC Connection Pool in Restlet Java Webservice?

Hi All I am using REST framework to devlope webservice in Java through restlet API.
I am using Tomcat 6.0.35 as web container.
I want to use Database Connection Pool for JDBC connection in my project.
I came to know about BoneCP is good one, so I want to try it.
I have 20 webservices in one project i.e. one Resource class for one webservice. And there is single Application class that routes each web service by clients request.
Right now I am opening and closing the JDBC Connection in every webservice.
So my question is, How can I use one JDBC connection for all webservices?
In which class I have to open and close the JDBC connection?
Please help me.
Thanks in Advance.
I would suggest you use Hibernate with C3P0 connection pooling , its best , it directly gives you sessions and you dont have to worry about closing the connection etc.

Categories

Resources