javaapache-commons-dbcp

What is the best way to keep Apache DBCP connection pool for a Java scheduler task


I have to develop Java scheduler task (Job) which has to run each and every 5 minutes periodically. I have two ideas two keep the connection pool and i don't know what should be the best way.

Method 1 : Crates connection pool each and every 5 minutes Query Closes the connection

Method 2 : Crates connection pool at initiation of the Java Job Query each and every 5 minutes Keeps the connection alive without closing

I just wanted to know what should be the ideal way to handle this scenario


Solution

  • The whole idea of a connection pool is to provide you with connections on demand. So don't recreate the pool every time. Create the pool once, keep it alive, and get a connection for each job. Close it when the job is done so it is made available to the pool again. Rinse and repeat.