Over the past few days, I have realized that my app crashes, and I get the exception TOO MANY CONNECTIONS to the database, and at the end, the DB CONNECTION returns an empty connection string. I usually experience this whenever more than 100 users try registering (student registration) in less than a minute. Due to this, I used a CompletableFuture and an ExecutorService to move the registration process from the MAIN THREAD and update the UI after the registration is completed, but the application will crash, and other users who try to register are unable to register until I restart the application from the server. What could be the possible cause of this exception, and how do I enhance user experience and prevent the app from crashing? I need your help. I am using #MySql as my DB.
Hard to say without seeing your code. My first guess would be: you are using entities in your UI and do some bad blocking there.
I’m using the default hikari pool of 10 connections - serving 1000k+ concurrent user/requests per minute without problems.
Actually I’m doing none of that. I realized from my research that it could probably be as a result of me creating a connection everytime I try saving a user like this:
public Connection getCon() {
return DriverManager.getConnection(…);
}
and then calling getCon().preparedStatement(query) on every request.
What show command from mysql console:
mysql> SHOW VARIABLES LIKE 'max_connections';
?
You are handling the connection yourself? Oh damn… good luck with that ![]()
Well, it turns out its not a good idea for large users at a time hence the issue I’m facing. And so I am trying to find a solution (We learn everyday based on the issues we encounter) ![]()
![]()
I can only guess that you don’t close connection after execute prepared statement per request
You should always use a connection pool. No matter how many users you have.
If you use Spring Boot or Quarkus this comes out of the box.
Otherwise configure it yourself
https://www.baeldung.com/hikaricp
Well noted, Sir. From my research, I realized that has been my mistake, and I’m working on that using Hikari CP.