I was wondering if I need to implement my own connection pool, what will be high level algorithm?
I had a glance over couple of solutions(below links) on google but all of them looks not scalable to me. When i say scalable
I mainly focus on getConnection()/borrowConnection()
method where i need to ensure if multiple threads calls this method at
same time, they don't get same connection and also wait is minimal. All of the below solution uses the synchronized method/block approach
which is not scalable at all as in application like ecommerce threads has to wait.
Mine Solution :- Basically my approach focus on how i can reduce the concurrency at granular level instead at data structure holding the pool of connection. So i will keep two list(arralylist)
ConnectionsNotInUse will hold all connections(wrapped in custom connection class) in pool at startup. Now if a thread asks for connection, once it gets it successfully , it will remove it from ConnectionsNotInUse and put it in ConnectionsInUse.
Inside each custom connection class, there will be method getConnection() method which will use Semaphore.tryAcquire()
which Acquires a lock, if one is available and returns immediately, with the value true. It will be semaphore with one permit. So if thread does not get connection , it will loop over for another connection in list.
If at last if thread does not get any connection, it will create another connection if max permissible limit allows otherwise it will wait to be connection to be released. Once connection is released , it notify the threads waiting for connection
Any comments/suggestions on approach suggested ?
As far as I understand your description:
the original implementation of the connection pool is like a room with only one entrance and only one person (thread) is allow to get in. What you are worried is person will line up in the entrance and influence the scalability of your app. So you decide to have multiple entrances (free list). But you seems not specify which entrance they should try, I assume you to let them try the first. If first is not available, they will try next entrance.
So if my understand is right, the policy which entrance they choose is the core of performance. If they all try first, then second, it will make little difference with original implementation.
What I can think about the fast and no sync way is to hash the identity of the person. If the resulted entrance is not free any more, there would be two ways:
So move back from metaphor. What I described is like the implementation of a hashmap and you may try it in two ways:
Some Notice:
Some suggestions:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With