Click here to Skip to main content
15,889,403 members
Articles / Programming Languages / Java
Tip/Trick

Connection Pool Implementation

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
2 Mar 2016CPOL 11.1K   60   2   2
Connection pool java without ConnectionContext

Introduction

In this post, I try to make something like a connection pool mechanism in generic workflow project. In my company, the code is used to solve max cursor exceed in connection as work flow engine is multithreaded and manages many requests at the time may use database.

Background

I used my work experience to make my connection pool in Java programming.

Using the Code

This block of Java code gets connection free to use in other subsystems:

Java
Connection GetConnection(DBNames name) {
           Connection conn = null;
          int j = -1;
           for(Map.Entry<Connection,Integer>entity :pooledconn.entrySet()) {
              if(entity.getValue()<max) {
                  j =  entity.getValue();
                     synchronized(this)
                  {
                      pooledconn.put(entity.getKey(),++j);
                  }
                conn = entity.getKey();
                break;
              }
           }
           if(j==-1) {
               this.SelectDB(name);
               conn =  this.GetConnection(name);
           }
           if(conn==null) {

                System.out.println("Exceeded"+ this.max+"\n");
                this.max++;
               if(this.max<Max_conn){
                  conn =  this.GetConnection(name);
                  this.max++;
               }
               else {
                         System.out.print("Sleeping To gain connection");
                   long EndTime = System.nanoTime() + TimeUnit.SECONDS.toNanos(5);
                        while(this.max>Max_conn) {
                   try {

                       Thread.sleep(2000);
                       if(System.nanoTime()>EndTime) {
                           break;
                       }
                       System.out.println("Critical Load");
                   } catch (InterruptedException e) {
                   }
                   conn =  this.GetConnection(name);
                   this.max++;
               }
                     }
           }
       return conn;
   }

If anyone here can improve this code and share your experience, you are welcome.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Egypt Egypt
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionInterruptedException Pin
Ender Yemenicioglu3-Mar-16 20:28
professionalEnder Yemenicioglu3-Mar-16 20:28 
AnswerRe: InterruptedException Pin
Mahmoud_Gamal4-Mar-16 10:14
professionalMahmoud_Gamal4-Mar-16 10:14 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.