Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implement custom connection pool?

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.

  1. https://codereview.stackexchange.com/questions/40005/connection-pool-implementation
  2. http://www.javaworld.com/article/2076690/java-concurrency/build-your-own-objectpool-in-java-to-boost-app-speed.html
  3. https://sridharrao85.wordpress.com/2011/07/20/sample-connection-pool-implementation/
  4. http://www.javamadesoeasy.com/2015/12/connection-pooling-in-java-with-example.html

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)

  1. ConnectionsNotInUse
  2. ConnectionInUse

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 ?

like image 223
emilly Avatar asked Nov 09 '22 02:11

emilly


1 Answers

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:

  • find next
    • hash again
    • next position, etc
  • create new

So move back from metaphor. What I described is like the implementation of a hashmap and you may try it in two ways:

  • replace your list with hashmap
  • use a single map mixed with used and free connection, in which case, you may avoid giant lock but you need use ConcurrentHashMap or implement your version.

Some Notice:

  • If you use two lists which are shared by different threads to maintain, you still need giant lock on them, otherwise will crash it.
  • You will increment count of connection in order to compare with max permissible connections, which also need synchronization.

Some suggestions:

  • Use atomic integer as counter
  • Use ConcurrentHashMap or implement your version (actually it's like a array add linkedlist and the array element is the lock of list which avoid the giant lock)
like image 108
Tony Avatar answered Nov 27 '22 10:11

Tony