There's more...

Deciding how many connections your pool will hold, as well as all the other parameters, is an architecture decision made based on a number of factors such as the type of data, database design, application and user behavior, and so on. We could write a whole book about it.

But if you are starting from scratch and/or still don't need much information, consider a number between 10% to 20% of your concurrent users. In other words, if your application has, for instance, 100 concurrent users, you should provide 10 to 20 connections to your pool.

You will know that your connections aren't enough if some methods are taking too much time to get a connection from the pool (it should take no time at all). It means that the server has no available connection at that moment.

So, you need to check if there are some methods taking too long to complete, or even some part in your code that is not closing the connection (consider what gives the connection back to the server). Depending on the issue, it might not be a pooling problem but a design one.

Another important thing for dealing with connection pools is to use the "try-with-resources" statement as we did here:

        try (Connection conn = ConnectionPool.getConnection();
PreparedStatement ps = conn.prepareStatement(sql);
ResultSet rs = ps.executeQuery()) {

This will guarantee that these resources will be properly closed once the method is done and also deal with their respective exceptions, also helping you to write less code.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.119.158.134