Search code examples
javajakarta-eeconnection-pooling

Singleton connection and database pooling


I developed some web application. In application I have now singleton pattern for recieve DB connection. I realized that it is bad approach because after some hours I always get "MySQLNonTransientConnectionException". So now I come with new solution of database pooling (http://jolbox.com/). But my problem is how to design it.

Should I use singleton for connection pool and create one instance of pool in whole application?

Or I should always create new pool and then ask for connection from pool for every query?


Solution

  • The whole point of a connection pool is that you have one connection pool and then everything in your application requests connections from the pool when they need them.

    So yes having one connection pool is the way forwards, whether you achieve that using a Singleton or the better option of dependency injection is up to you.