0

I have a JDBC connection pool setup in tomcat server.

enter image description here Code 1 takes more time than Code 2. My question is that if connection pool is working. Then it should parallelly open other connections and it should not be taking much time (as I have maxActive= 100 connections). Then what is the point in using connection pool ?

Vinayak
  • 43
  • 5
  • The *only* think you can say *secure* is that the code 1 makes no much sense - it allocates connection *without closing them* - a bad practice... With `initialSize` and `minIdle` you have at least 10 connection in the pool, so there will be no much difference, except that code 1 lets 10 connection in the pool to be timeouted. – Marmite Bomber Dec 23 '21 at 21:15

1 Answers1

1

This makes perfect sense. When you get a Connection from the pool it looks to see if it has a Connection available. If the pool doesn't have an available connection it has to go through the process of obtaining the connection. This is true in both of your examples.

In your first loop, every time you get a connection the pool has to allocate it. Pools get to decide when the "initialSize" is actually allocated - it may not be instantly.

In your second loop, however, you get a Connection and then release it by calling close(). This releases it back to the pool. In this example it's likely you're getting the same connection over and over again.

stdunbar
  • 16,263
  • 11
  • 31
  • 53