Tuesday, December 15, 2009



If we wants to make programmatically Connection management, connection may be Database or TCP Socket or JMS or other. Instead of establishing connection every time for every SQL command, we can cache the connection for future use. Once the usage of connection usage is completed then we can take connection back to pool. For instance, We have established 20 connections, but 50 SQL commands has to be run in the given point of time. These 20 connections has to be alotted to first 20 SQL commands to run and whenever SQL command is completed then connection will be used for rest of SQL commands.

We have to maintain a mark for each connection that whether that connection is get used or not. Two methods has to be used to acquire and release the resource. Everything perfect, who will take care of filtering and ordering the thread entry, how thread state managed. For these questions Semaphore is the right answer, here is the code snippet

class ConnectionPool {
private static final int MAX_AVAILABLE = 100;
private final Semaphore available = new Semaphore(MAX_AVAILABLE, true);

public Object getConnection() throws InterruptedException {
return getNextAvailableItem();

public void releaseConnection(Object x) {
if (markAsUnused(x))

protected Object[] connections = ... whatever kinds of connections being managed
protected boolean[] used = new boolean[MAX_AVAILABLE];

protected synchronized Object getNextAvailableItem() {
for (int i = 0; i < MAX_AVAILABLE; ++i) {
if (!used[i]) {
used[i] = true;
return connections[i];
return null; // not reached

protected synchronized boolean markAsUnused(Object item) {
for (int i = 0; i < MAX_AVAILABLE; ++i) {
if (item == connections[i]) {
if (used[i]) {
used[i] = false;
return true;
} else
return false;
return false;



InfinityMatrixWizard101 said...


KraliyetRüyaKızı48 said...

sakarya parça eşya taşıma
aksaray parça eşya taşıma
urfa parça eşya taşıma
kocaeli parça eşya taşıma

Cenk said...

Tokat Lojistik
Konya Lojistik
Mersin Lojistik
Karabük Lojistik
Samsun Lojistik

ElectricWandererA205 said...

adana evden eve nakliyat
bolu evden eve nakliyat
diyarbakır evden eve nakliyat
sinop evden eve nakliyat
kilis evden eve nakliyat

52991JadeCD958 said...

Sakarya Evden Eve Nakliyat
Osmaniye Parça Eşya Taşıma
Mersin Evden Eve Nakliyat
Denizli Parça Eşya Taşıma
Diyarbakır Lojistik

6FC36Skylar07335 said...

Bursa Şehir İçi Nakliyat
Altındağ Fayans Ustası
Çerkezköy Çelik Kapı
Erzincan Evden Eve Nakliyat
Konya Şehir İçi Nakliyat
Antep Parça Eşya Taşıma
Batman Lojistik
Adıyaman Şehir İçi Nakliyat
Kilis Şehir İçi Nakliyat

1F8F8Victor7761E said...

Ardahan Şehir İçi Nakliyat
Denizli Lojistik
Batman Lojistik
Bartın Lojistik
Gümüşhane Lojistik
Ünye Asma Tavan
Bilecik Şehirler Arası Nakliyat
Keçiören Boya Ustası
Mardin Evden Eve Nakliyat

62E19RachelF5500 said...

Ordu Lojistik
Kalıcı Makyaj
Bingöl Lojistik
Konya Lojistik
Bayburt Şehirler Arası Nakliyat
Çerkezköy Mutfak Dolabı
Ünye Kurtarıcı
Manisa Şehirler Arası Nakliyat
Siirt Parça Eşya Taşıma

Post a Comment

Recent Posts

Unix Commands | List all My Posts


This blog intended to share the knowledge and contribute to JAVA Community such a way that by providing samples and pointing right documents/webpages. We try to give our knowledege level best and no guarantee can be claimed on truth. Copyright and Terms of Policy refer blogspot.com