Connection pool and threads 
Author Message
 Connection pool and threads

Hi, I have a question about threads and a connectionpool.

I have a ConnectionPool object that holds java.sql.Connection objects.

The main method is /*pseudo code*/
public Connection waitForConnection() {
  // this method returns the first available connection to the client
  while (noConnectionIsAvailable) {}
  return AvailableConnection;

Quote:
}

In order to make this work properly the connection pool object extends the
Thread class.

Now my question is if this is the right approach for a connection pool and
what for me to do in the run() method since this is not the main method of
this object?

Any comments will help.

Thanks in advance
Filip

             ||||
----oo--00--oo--------------
           ~~
Filip Hanik
Software Engineer / JAVA-CORBA
Verge Software Co.
539 Bryant Street, #304
San Francisco, CA 94109



Mon, 04 Sep 2000 03:00:00 GMT
 Connection pool and threads


somewhere:

Quote:
>Any comments will help.

Well, while I'm lurking about this group hoping that some kind soul
will address my posted questions about threads and connections I'll
take a stab at helping you on this one.

My guess is there are any number of ways folks do this.

I use a simple stack for many of my pools (threads, jobs, connections,
etc.).  Here's a modified version of what I use (modified because mine
actually extends another class called Fifo as the java.util.stack is a
LIFO.  For your connection pool a LIFO should be just fine.

import java.util.EmptyStackException;
/** This class provides a synchronized, blocking queue.
*/

public class LifoPool extends java.util.stack
{
        /** pop is a blocking call that will not return until there is
    something to pop.
    */
    public synchronized Object pop()
    {
        Object o;
        while (true)// loop forever
        {
            try
            {
                o = super.pop();  //call the statck pop
                return o;// return the popped object
            }
            catch (EmptyStackException e)
            {
                try {this.wait();}// wait for a signal from a push()
                catch (InterruptedException ex)
                {Thread.yield();}; // yield and loop again YOU may
want to do something else other than yield in this case.
            }
        }
    }

        /** peek is a blocking call that will not return until there is
    something to peek.
    */
    public synchronized Object peek()
    {
        Object o;
        while (true)// loop forever
        {
            try
            {
                o = super.peek();  //call the stack peek
                return o;// return the popped object
            }
            catch (EmptyStackException e)
            {
                try {this.wait();}// wait for a signal from a push()
                catch (InterruptedException ex)
                {Thread.yield();}; // yield and loop again
            }
        }
    }

    /** This overrides the stack push() by notifying ourself when
    something is pushed on the stack.
    */
    public synchronized Object push(Object o)
    {
        super.push(o);
        this.notify(); // notify ourself.
        return o;
    }

Quote:
}

So, whenever you want to wait on the next available connection (or
whatever) in the pool, just do a pop on it.  Of course, this is a
potentially eternal wait.   You could add a pop(int waitSeconds) which
would return null if nothing was available in the prescribed time.

Hope this helps.

confused



Tue, 05 Sep 2000 03:00:00 GMT
 
 [ 2 post ] 

 Relevant Pages 

1. connection pooling - to pool or not to pool

2. pool connections and threads

3. Thread Safe Pooled Database connections

4. Java connection pooling vs oracle connection pooling

5. ODBC Connection Pooling Vs OLE DB Resource Pooling

6. ODBC Connection Pooling Vs OLE DB Resource Pooling

7. ODBC Connection Pooling Vs OLE DB Resource Pooling

8. ODBC Connection Pooling Vs OLE DB Resource Pooling

9. OLEDB Resource Pooling (database connection pooling) without MTS

10. connection pooling, "use msdb" ruins connection

11. TCP/IP Connection to SQL fails/Connection Pooling


 
Powered by phpBB® Forum Software