• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Thread : Resource allocation

 
Arnab Karmakar
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If there are 10 threads who are trying to access connection object from database connection pool (5 instances in pool), how can it be implemented to ensure that none of the threads wait for any other thread to get a connection pool object.

I am not sure if it is at possible to ensure that all threads get access to connection object at the same time because there are only 5 instances in the pool (and 10 thread trying to access it).

Any insight will be appreciated.
 
Ireneusz Kordal
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, it is not possible.
If you have 5 cars and 10 drivers, then only 5 drivers can drive a car, the other drivers must wait.
The same is true for 5 connections and 10 threads.
 
Arnab Karmakar
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ireneusz Kordal wrote:No, it is not possible.
If you have 5 cars and 10 drivers, then only 5 drivers can drive a car, the other drivers must wait.
The same is true for 5 connections and 10 threads.


What if there are 5 thread and 5 connections in the pool. How can it be ensured that all gets access to an instance from the pool. If I make the getConnection() method synchronized, while thread 1 is trying to get a connection from pool (using getConnection() method), other threads will be waiting till Thread 1 finishes getting a connection. How can it be ensure that none waits for others?
 
Henry Wong
author
Marshal
Pie
Posts: 21489
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Arnab Karmakar wrote:
What if there are 5 thread and 5 connections in the pool. How can it be ensured that all gets access to an instance from the pool. If I make the getConnection() method synchronized, while thread 1 is trying to get a connection from pool (using getConnection() method), other threads will be waiting till Thread 1 finishes getting a connection. How can it be ensure that none waits for others?


The time to get a lock is quick, measured in nano seconds. This is probably true to get the connection from the pool (assuming that it has been preallocated). So it is not an issue. It is highly unlikely that anyone will notice that a thread had to wait its turn to get a connection -- especially since after the connection is gotten, the thread will be doing a network / I/O bound operation to a database.

Henry
 
Arnab Karmakar
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
Arnab Karmakar wrote:
What if there are 5 thread and 5 connections in the pool. How can it be ensured that all gets access to an instance from the pool. If I make the getConnection() method synchronized, while thread 1 is trying to get a connection from pool (using getConnection() method), other threads will be waiting till Thread 1 finishes getting a connection. How can it be ensure that none waits for others?


The time to get a lock is quick, measured in nano seconds. This is probably true to get the connection from the pool (assuming that it has been preallocated). So it is not an issue. It is highly unlikely that anyone will notice that a thread had to wait its turn to get a connection -- especially since after the connection is gotten, the thread will be doing a network / I/O bound operation to a database.

Henry

You are considering the term "wait" as tolerance for a particular application. In some application 1 second might not be called as acceptable wait and in others 1 nano second might be called as too late. My question is, is it at all possible to provide Connection object from pool to the 5 threads at the same time without each thread waiting for others to get the object. That is to say, is it possible to do a lock free atomic operation.
 
Henry Wong
author
Marshal
Pie
Posts: 21489
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Arnab Karmakar wrote:
That is to say, is it possible to do a lock free atomic operation.


Sure. That is what the atomic library is for... Now, of course, you'll have to check to see if your database library uses it.

Henry
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic