MiDhuN
MiDhuN

Reputation: 232

which design pattern helps me to implement connection pool

i am trying to create a connection pool for factory class ,which design pattern helps me to create connection pool .user want to specify no of connections and maximum connections externally.I have search for some time, some people proposed to use Singleton or put the initialization code inside some static block. But others said singleton is bad. So, what should be a right design pattern to use for connection pooling? Thanks.

Upvotes: 1

Views: 840

Answers (3)

INeedMySpace
INeedMySpace

Reputation: 315

As previous commenters suggest, use libs like Apache DBCP, C3P0, BoneCP or HikariCP.

But if you want to "invent your own connection pool" and understand how it could be done for educational purpose, start with singletone and extend it to hold preconfigured number of your instances.

Upvotes: 1

ashokramcse
ashokramcse

Reputation: 2861

Database Connection Pooling

Initially in my application i use apache-commons-dpcp and i faced lot of connection issues and latter after long analysis i use bonecp. I would suggest bonecp connection polling. And Strictly don't use Singleton patten. It will bring you big pain point once your application is go live.

TCP Connection Pooling

For TCP Pooling I suggest to use Netty and Take a look on Apache MINA also.

Upvotes: 1

robert
robert

Reputation: 2027

Singleton is bad for a few reasons e.g. usually you cannot inject a Mock object. If you provide a mechanism (or do not need) to mock test it, then I do not see a problem.

Please note that there are a lot of excellent connection pools available, I would first suggest to look into existing ones.

Upvotes: 4

Related Questions