Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you setup your connection pool?

What is the best way to setup your pool with respect to:-

  1. When do you create connections?
  2. When do you close connections, and would you close all of them?
  3. Do you test connections are still good. When and how?
  4. How do you figure out a good number for the maximum number of connections?
  5. What sort of monitoring do you have in place to ensure users of the pool are well behaved? Can you stop one bad piece of code from taking out everything?
  6. Have you written your own pool, or used a third-party library?

I believe this is an agnostic question, but comments about "features" of particular databases/languages are welcome. For example, it might be slower or more expensive to connect on some databases than others.

To clarify, I do not intend to write a pool from scratch, this question is more about how to configure an existing library that does pooling.

like image 597
WW. Avatar asked Nov 07 '08 03:11

WW.


People also ask

What is a connection pool in database?

What is database connection pooling? Database connection pooling is a way to reduce the cost of opening and closing connections by maintaining a “pool” of open connections that can be passed from database operation to database operation as needed.

What happens if connection pool is full?

If the maximum pool size has been reached and no usable connection is available, the request is queued. The pooler then tries to reclaim any connections until the time-out is reached (the default is 15 seconds). If the pooler cannot satisfy the request before the connection times out, an exception is thrown.

How do I create a connection pool in spring boot?

The default connection pool in Spring Boot 2 is HikariCP. It provides enterprise-ready features and better performance. HikariCP is a JDBC DataSource implementation that provides a connection pooling mechanism. If the HikariCP is present on the classpath, the Spring Boot automatically configures it.


1 Answers

I wrote a connection pool for the database in Java when it was just a design pattern and not a common library. Now I use the one built into Tomcat.

I used a thread to monitor several aspects of the pool and several parameters to control its behavior...

  1. minimumInPool="3"... These first three are created upon launch. The pool is never allowed to drop below three.
  2. maximumIdleTimeBeforeRemoval="60"... If a connect is idle for an hour, then drop it and create a new one. Idle time probably means there is only the minimum of three in the pool.
  3. maximumInUseTimeBeforeRemoval="30"... If a given connection has been checked out for over 30 minutes, then something is probably wrong. Recall it, and kill the connection.
  4. maximumTimeBeforeRemoval="60"... Remove it if it is over 60 minutes old.
  5. maximumUsageBeforeRemoval="1000"... Remove it if it has been checked out over 1000 times.
  6. monitorInterval="15"... Check the above parameters every 15 minutes.

This served me very well for a couple of years. The highest I ever saw the pool was 151 connections during a wild peek. Usually the pool was at about a dozen during heavy usage and idled down to the minimum three in the early morning hours.

I used Oracle's JDBC thin drivers and connected to an Oracle database.

like image 113
dacracot Avatar answered Sep 20 '22 13:09

dacracot