Search code examples
javadatabasejdbcdatabase-connectionconnection-pooling

Database Connection Pooling (w/ Java)


I would like to know your thoughts on whether this type of methodology could be practically implemented. I am very knew to database pooling, so forgive me for any misunderstandings. I have multiple classes calling a dbData class (which connects to a database and has helper functions, like frequently uses gets and updates and etc). I need to keep all the get and update functions in dbData class, but keep a pool of active connections I can re-use. Which means I will instantiate the dbData class multiple times, each time checking whether an appropriate connection exists, if not, create a new one and put it into the pool.

My question is this how and where you would store this pool. I could perhaps accomplish this if dbData would not be instantiated more than once and keeps one persistent pool object. But in my case, I have multiple dbData instances which should all connect to a single pool. I thought about serializing the pool object, but this seems ridiculous. Is this possible (what is shown in the pic)? It seems I am having trouble with the object-oriented part of this design.

The applications uses multithreading with Class1 and Class2.

I would not like to use any external libraries if possible.

db pool img


Solution

  • If it is a standalone app, then I would create a seperate service with a static collection that keeps the connection and does all the handling of those. Then the dbData class can call the static method to get a connection. The service then itself takes care of creating new connections if required. If your dbData instances are running in parallel you have to think about synchronized access (if required).