Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Pool Connection - Close Issue

Status
Not open for further replies.

mdazam

Programmer
Apr 1, 2005
28
0
0
US
Hello,

I copied this class from this site somewhere and its working great. But I'm having issue of 'connection reset' and 'Broken Pipe' error messages. Since I'm not the owner of this class I dont know where to make modification in order to close the connection or delete or make it expired, that was told by someone to me in order to get rid of that error msg.

1 Can anyone tell me basic idea what this below class do, just for my total understanding?
2. Can we close or delete or expire connection in this class ? If not, why not ? Whatz other alternative ?
Code:
public class Pool
{
     private static Pool p=null;
     private Vector con_pool=null;
     private Vector used_connections=null;
     private Connection c=null;
     private int cache_size=10;
     public static Pool getInstance()
     {
          if (p==null)
               p=new Pool();
          return p;
     }
     public Pool()
     {
          con_pool=new Vector();
          used_connections=new Vector();
          for(int i=0;i<cache_size;i++)
          {
               c=create();
               con_pool.addElement(c);
          }

     }
     public Connection get_Connection()
     {

          Connection temp=null;
          if (con_pool.size()>0)
          {
               temp=(Connection) con_pool.firstElement();
               used_connections.addElement(temp);
               con_pool.removeElement(con_pool.firstElement());
          }
          else
          {
               temp=create();
               used_connections.addElement(temp);
          }
          return temp;
     }
     public void return_Connection(Connection con)
     {

          Enumeration enum=used_connections.elements();
          Connection temp=null;
          while(enum.hasMoreElements())
          {
               temp=(Connection) enum.nextElement();
               if (temp==con)
               {
                    used_connections.removeElement(temp);
                    con_pool.addElement(temp);
                    break;
               }
          }


     }
     public Connection create()
     {          
          try
          {
               Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
               c= DriverManager.getConnection("....");
          }
          catch(Exception e)
          {
              System.out.println("Error in create..");
          }
          return c;
     }
}
Thanks for you time.
 
Without knowing how you are using this "Pool", I'm not sure what the problem is. I'm guessing that you are closing the connection and then returning it to the "Pool" - and then when you try to get another one, it is broken.

Are you trying to learn connection pooling or do you just want to use a connection pool ?
If the latter, it might be more worth of using decent pooling solution.

--------------------------------------------------
Free Database Connection Pooling Software
 
I know about Pool Connection. But I mean to say since I'm not the owner of that above Pool class I'm having diff. to address my issue. In my JSP page I'm importing that class and using it as below. Actualy I'm not closing connection in JSP as u can see I'm returning the connection back to Pool Class.

Why I need to close or ... ? Because after implementing this class I'm very often getting error I mention above. But once I do refresh those error goes. But I was told may be becoz I'm not closing or make it expire connection it is setting forever and one of [software application or security app.] disconnects any connection after 5 or 10 min. I dont know if that make sense to U. Basic idea I want to apply and see is can we close connection or make connection expire or delete, if so whats the command and where it has to be written. please help

Code:
Pool con_pool=Pool.getInstance();
Connection connection =con_pool.get_Connection();

.....
.....

con_pool.return_Connection(connection);
 
I still don't really follow. The point of pooling is to not close connections.

That class IMO is really bad code - so if its not yours, then I wouldn't bother using it - its just poor.

You say you "know" about connection pooling, so I am a bit lost to understand why you are using this code.

If you want to learn about how pooling works, I would suggest looking into a tutorial or open source pooling code - there are loads out there if you google for them.

Why you are getting connection reset issues I don't know - a stack trace would be more helpful - but as I said before its just really lame code and I wouldn't bother looking at it.

--------------------------------------------------
Free Database Connection Pooling Software
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top