bloritsch    01/04/06 11:12:11

  Modified:    src/java/org/apache/avalon/component
                        DefaultComponentManager.java
                        DefaultComponentPool.java
                        DefaultComponentSelector.java
               src/java/org/apache/avalon/util/datasource
                        JdbcConnectionPool.java
  Log:
  FIxed pools so that they only have one Mutex.  They should work under any load now.
  
  Revision  Changes    Path
  1.3       +3 -3      
jakarta-avalon/src/java/org/apache/avalon/component/DefaultComponentManager.java
  
  Index: DefaultComponentManager.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-avalon/src/java/org/apache/avalon/component/DefaultComponentManager.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- DefaultComponentManager.java      2001/04/06 13:55:47     1.2
  +++ DefaultComponentManager.java      2001/04/06 18:12:10     1.3
  @@ -34,7 +34,7 @@
    *
    * @author <a href="mailto:[EMAIL PROTECTED]">Berin Loritsch</a>
    * @author <a href="mailto:[EMAIL PROTECTED]">Paul Russell</a>
  - * @version CVS $Revision: 1.2 $ $Date: 2001/04/06 13:55:47 $
  + * @version CVS $Revision: 1.3 $ $Date: 2001/04/06 18:12:10 $
    */
   public class DefaultComponentManager extends AbstractLoggable
           implements ComponentManager, Configurable, Contextualizable, Disposable {
  @@ -147,10 +147,10 @@
               try {
                   component = handler.get();
               } catch (Exception ee) {
  -                throw new ComponentManagerException("Could not access the Component 
for you", ee);
  +                throw new ComponentManagerException("Could not access the Component 
for role: " + role, ee);
               }
           } catch (Exception e) {
  -            throw new ComponentManagerException("Could not access the Component for 
you", e);
  +            throw new ComponentManagerException("Could not access the Component for 
role: " + role, e);
           }
   
           this.componentMapping.put(component, handler);
  
  
  
  1.2       +73 -64    
jakarta-avalon/src/java/org/apache/avalon/component/DefaultComponentPool.java
  
  Index: DefaultComponentPool.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-avalon/src/java/org/apache/avalon/component/DefaultComponentPool.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- DefaultComponentPool.java 2001/04/05 19:40:45     1.1
  +++ DefaultComponentPool.java 2001/04/06 18:12:10     1.2
  @@ -17,7 +17,6 @@
   import org.apache.avalon.util.pool.Pool;
   import org.apache.avalon.util.pool.ObjectFactory;
   import org.apache.avalon.util.Lock;
  -import org.apache.avalon.util.LockException;
   import org.apache.avalon.Recyclable;
   import org.apache.avalon.AbstractLoggable;
   
  @@ -41,7 +40,7 @@
       private boolean initialized = false;
       private boolean disposed = false;
   
  -    private Lock lock = new Lock();
  +    private Lock mutex = new Lock();
       private Thread initializationThread;
   
       protected ObjectFactory factory = null;
  @@ -79,32 +78,40 @@
       }
   
       public void run() {
  -        this.lock.lock(this.availableResources);
  +        try {
  +            this.mutex.lock();
   
  -        for( int i = 0; i < this.initial; i++ ) {
  -            try {
  -                this.availableResources.add(this.factory.newInstance());
  -            } catch (Exception e) {
  -                getLogger().warn("Could not create poolable resource", e);
  +            for( int i = 0; i < this.initial; i++ ) {
  +                try {
  +                    this.availableResources.add(this.factory.newInstance());
  +                } catch (Exception e) {
  +                    getLogger().warn("Could not create poolable resource", e);
  +                }
               }
  -        }
   
  -        if (this.availableResources.size() > 0) {
  -            this.initialized = true;
  +            if (this.availableResources.size() > 0) {
  +                this.initialized = true;
  +            }
  +        } catch (Exception e) {
  +            getLogger().debug("ComponentPool.run()", e);
  +        } finally {
  +            this.mutex.unlock();
           }
  -
  -        this.lock.unlock(this.availableResources);
       }
   
       public void dispose() {
  -        this.lock.lock(this.availableResources);
  -        this.disposed = true;
  +        try {
  +            this.mutex.lock();
  +            this.disposed = true;
   
  -        while ( ! this.availableResources.isEmpty() ) {
  -            this.availableResources.remove(0);
  +            while ( ! this.availableResources.isEmpty() ) {
  +                this.availableResources.remove(0);
  +            }
  +        } catch (Exception e) {
  +            getLogger().debug("ComponentPool.dispose()", e);
  +        } finally {
  +            this.mutex.unlock();
           }
  -
  -        this.lock.unlock(this.availableResources);
       }
   
       /**
  @@ -134,6 +141,7 @@
                   throw new IllegalStateException("You cannot get a resource before 
the pool is initialized");
               } else {
                   this.initializationThread.join();
  +                this.initializationThread = null;
               }
           }
   
  @@ -141,28 +149,29 @@
               throw new IllegalStateException("You cannot get a resource after the 
pool is disposed");
           }
   
  -        this.lock.lock(this.availableResources);
  -        // See if there is a resource in the pool already
           Poolable resource = null;
   
  -        if (this.availableResources.size() > 0) {
  -            resource = (Poolable)this.availableResources.remove(0);
  +        try {
  +            this.mutex.lock();
  +            // See if there is a resource in the pool already
   
  -            this.lock.lock(this.usedResources);
  -            this.usedResources.add(resource);
  -            this.lock.unlock(this.usedResources);
  -        } else {
  -            resource = this.getOverflowResource();
  +            if (this.availableResources.size() > 0) {
  +                resource = (Poolable)this.availableResources.remove(0);
   
  -            if (resource != null) {
  -                this.lock.lock(this.usedResources);
                   this.usedResources.add(resource);
  -                this.lock.unlock(this.usedResources);
  +            } else {
  +                resource = this.getOverflowResource();
  +
  +                if (resource != null) {
  +                    this.usedResources.add(resource);
  +                }
               }
  +        } catch (Exception e) {
  +            getLogger().debug("ComponentPool.get()", e);
  +        } finally {
  +            this.mutex.unlock();
           }
   
  -        this.lock.unlock(this.availableResources);
  -
           if (resource == null) {
               throw new RuntimeException("Could not get the component from the pool");
           }
  @@ -176,43 +185,43 @@
       public void put(Poolable resource)
       {
           int pos = -1;
  -
  -        this.lock.lock(this.usedResources);
   
  -        // Make sure the resource is in the used list
  -        pos = usedResources.indexOf(resource);
  +        try {
  +            this.mutex.lock();
   
  -        if (resource instanceof Recyclable) {
  -            ((Recyclable)resource).recycle();
  -        }
  +            // Make sure the resource is in the used list
  +            pos = usedResources.indexOf(resource);
   
  -        // If the resource was in the used list, remove it from the used list and
  -        // add it back to the free list
  -        if (pos >= 0) {
  -            this.usedResources.remove(pos);
  -
  -            this.lock.lock(this.availableResources);
  +            if (resource instanceof Recyclable) {
  +                ((Recyclable)resource).recycle();
  +            }
   
  -            if (this.availableResources.size() < this.maximum) {
  -                // If the available resources are below the maximum add this back.
  -                this.availableResources.add(resource);
  -            } else {
  -                // If the available are above the maximum destroy this resource.
  -                try {
  -                    this.factory.decommission(resource);
  -                    getLogger().debug("Component Pool - decommissioning Overflow 
Resource:"
  -                                    + " Resource=" + resource
  -                                    + " Available=" + availableResources.size()
  -                                    + " Used=" + usedResources.size() );
  -                    resource = null;
  -                } catch (Exception e) {
  -                    throw new RuntimeException("caught exception decommissioning 
resource: " + resource);
  +            // If the resource was in the used list, remove it from the used list 
and
  +            // add it back to the free list
  +            if (pos >= 0) {
  +                this.usedResources.remove(pos);
  +
  +                if (this.availableResources.size() < this.maximum) {
  +                    // If the available resources are below the maximum add this 
back.
  +                    this.availableResources.add(resource);
  +                } else {
  +                    // If the available are above the maximum destroy this resource.
  +                    try {
  +                        this.factory.decommission(resource);
  +                        getLogger().debug("Component Pool - decommissioning 
Overflow Resource:"
  +                                        + " Resource=" + resource
  +                                        + " Available=" + availableResources.size()
  +                                        + " Used=" + usedResources.size() );
  +                        resource = null;
  +                    } catch (Exception e) {
  +                        throw new RuntimeException("caught exception 
decommissioning resource: " + resource);
  +                    }
                   }
               }
  -
  -            this.lock.unlock(this.availableResources);
  +        } catch (Exception e) {
  +            getLogger().debug("ComponentPool.put()", e);
  +        } finally {
  +            this.mutex.unlock();
           }
  -
  -        this.lock.unlock(this.usedResources);
       }
   }
  
  
  
  1.2       +3 -3      
jakarta-avalon/src/java/org/apache/avalon/component/DefaultComponentSelector.java
  
  Index: DefaultComponentSelector.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-avalon/src/java/org/apache/avalon/component/DefaultComponentSelector.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- DefaultComponentSelector.java     2001/04/05 19:40:45     1.1
  +++ DefaultComponentSelector.java     2001/04/06 18:12:10     1.2
  @@ -35,7 +35,7 @@
    *
    * @author <a href="mailto:[EMAIL PROTECTED]">Berin Loritsch</a>
    * @author <a href="mailto:[EMAIL PROTECTED]">Paul Russell</a>
  - * @version CVS $Revision: 1.1 $ $Date: 2001/04/05 19:40:45 $
  + * @version CVS $Revision: 1.2 $ $Date: 2001/04/06 18:12:10 $
    */
   public class DefaultComponentSelector extends AbstractLoggable implements 
Contextualizable, ComponentSelector, Composer, Configurable, ThreadSafe, Disposable {
   
  @@ -144,7 +144,7 @@
           try {
               component = handler.get();
           } catch (Exception e) {
  -            throw new ComponentManagerException(this.getName() + ": 
ComponentSelector could not access the Component for you", e);
  +            throw new ComponentManagerException(this.getName() + ": 
ComponentSelector could not access the Component for hint: " + hint, e);
           }
   
           if (component == null) {
  @@ -269,7 +269,7 @@
        * <code>addComponentInstance</code> with no associated configuration
        */
      private String getName() {
  -     if (this.conf != null) {
  +     if (this.conf != null && ("".equals(this.conf.getName()) == false)) {
          return this.conf.getName();
        }
   
  
  
  
  1.14      +10 -17    
jakarta-avalon/src/java/org/apache/avalon/util/datasource/JdbcConnectionPool.java
  
  Index: JdbcConnectionPool.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-avalon/src/java/org/apache/avalon/util/datasource/JdbcConnectionPool.java,v
  retrieving revision 1.13
  retrieving revision 1.14
  diff -u -r1.13 -r1.14
  --- JdbcConnectionPool.java   2001/04/06 17:25:07     1.13
  +++ JdbcConnectionPool.java   2001/04/06 18:12:11     1.14
  @@ -26,7 +26,7 @@
    * thread to manage the number of SQL Connections.
    *
    * @author <a href="mailto:[EMAIL PROTECTED]">Berin Loritsch</a>
  - * @version CVS $Revision: 1.13 $ $Date: 2001/04/06 17:25:07 $
  + * @version CVS $Revision: 1.14 $ $Date: 2001/04/06 18:12:11 $
    */
   public class JdbcConnectionPool
       extends AbstractLoggable
  @@ -41,8 +41,7 @@
       private List                   m_ready         = new ArrayList();
       private boolean                m_initialized   = false;
       private boolean                m_disposed      = false;
  -    private Lock                   m_activeMutex   = new Lock();
  -    private Lock                   m_readyMutex    = new Lock();
  +    private Lock                   m_mutex         = new Lock();
       private Thread                 m_initThread;
   
       public JdbcConnectionPool( final String url,
  @@ -129,7 +128,7 @@
           Poolable obj = null;
   
           try {
  -            this.m_readyMutex.lock();
  +            this.m_mutex.lock();
               final int size;
   
               if( 0 == m_ready.size() )
  @@ -138,9 +137,7 @@
                   {
                       obj = this.createJdbcConnection();
   
  -                    this.m_activeMutex.lock();
                       m_active.add(obj);
  -                    this.m_activeMutex.unlock();
                   }
                   else
                   {
  @@ -151,14 +148,12 @@
               {
                   obj = (Poolable)m_ready.remove( 0 );
   
  -                this.m_activeMutex.lock();
                   m_active.add( obj );
  -                this.m_activeMutex.unlock();
               }
           } catch (Exception e) {
               getLogger().debug("JdbcConnectionPool.get()", e);
           } finally {
  -            this.m_readyMutex.unlock();
  +            this.m_mutex.unlock();
           }
   
           if (((Connection)obj).getAutoCommit() != m_autoCommit) {
  @@ -179,7 +174,7 @@
           }
   
           try {
  -            this.m_activeMutex.lock();
  +            this.m_mutex.lock();
               m_active.remove( obj );
   
               if(! m_disposed)
  @@ -192,16 +187,14 @@
                       connection = this.createJdbcConnection();
                   }
   
  -                this.m_readyMutex.lock();
                   m_ready.add( connection );
  -                this.m_readyMutex.unlock();
               } else {
                   recycle((Recyclable) obj);
               }
           } catch (Exception e) {
               getLogger().warn("Error returning connection to pool", e);
           } finally {
  -            this.m_activeMutex.unlock();
  +            this.m_mutex.unlock();
           }
   
           getLogger().debug( "JdbcConnection '" + m_dburl + "' has been returned to 
the pool." );
  @@ -210,7 +203,7 @@
       public void run()
       {
           try {
  -            this.m_readyMutex.lock();
  +            this.m_mutex.lock();
               for (int i = 0; i < m_max; i++)
               {
                   try {
  @@ -226,14 +219,14 @@
           } catch (Exception e) {
               getLogger().debug("JdbcConnectionPool.run()", e);
           } finally {
  -            this.m_readyMutex.unlock();
  +            this.m_mutex.unlock();
           }
       }
   
       public void dispose()
       {
           try {
  -            this.m_readyMutex.lock();
  +            this.m_mutex.lock();
               m_disposed = true;
   
               while( ! m_ready.isEmpty() )
  @@ -243,7 +236,7 @@
           } catch (Exception e) {
               getLogger().debug("JdbcConnectionPool.dispose()", e);
           } finally {
  -            this.m_readyMutex.unlock();
  +            this.m_mutex.unlock();
           }
       }
   }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to