commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Phil Steitz <phil.ste...@gmail.com>
Subject Re: [commons.pool] unexpected object invalidation
Date Mon, 13 Sep 2010 10:15:35 GMT
On 9/13/10 5:01 AM, Konstantyn Smirnov wrote:
>
> Hi all
>
> in my Grails app I'm using a GenericObjectPool to hold sockets that connect
> to some legacy system.
> Each idle socket in the pool must be "pinged" every 25 sec, so that the
> server leaves the connection open.
>
> For that I'm using the pool:
>
> poolSize = 4
>
> socketPool = new GenericObjectPool(
>    maxActive:poolSize,
>    minIdle:poolSize,
>    numTestsPerEvictionRun:poolSize,
>    testOnBorrow:true,
>    testOnReturn:true,
>    testWhileIdle:true,
>    whenExhaustedAction:GenericObjectPool.WHEN_EXHAUSTED_FAIL,
>    maxWait:1000,
>    timeBetweenEvictionRunsMillis:25000,
>    minEvictableIdleTimeMillis:-1,
>    factory:socketFactory
> )
>
> and a SocketFactory (abbreviated for simplicity sake):
>
> class SocketPoolFactory extends BasePoolableObjectFactory {
>
>    def stayingAlive( r, w ){
>      w<<  '?'
>      w.flush()
>      r.read()
>    }
>
>    Object makeObject(){
>      def s = new Socket( host, port )
>      stayingAlive r, w
>      log.debug "created socket ..."
>      [ socket:s ]
>    }
>
>    boolean validateObject( Object socket ){
>      try{
>        stayingAlive socket.reader, socket.writer
>      }catch( e ){
>        return false
>      }
>      true
>    }
>
>    void destroyObject( Object socket ){
>      if( socket?.socket ){
>        log.info "socket ... died"
>        if( !socket.socket.closed ) socket.socket.close()
>      }
>    }
>
> }
>
> the pool is used like:
>
>    protected withSocket( ... ){
>      def socket
>      try{
>        socket = socketPool.borrowObject()
>
>        doStuffWithSocket( socket, .... )
>
>      }catch( Exception e ){
>          socketPool.invalidateObject socket
>          socket = null
>        }
>      }finally{
>        if( socket ) socketPool.returnObject socket
>      }
>    }
>
>
> The whole thing works perfectly, until I set the poolSize to something
> greater than 8.
>
> In that case the first 8 sockets are kept alive in a pool as long as I need,
> but the rest gets invalidated and created each 25 sec (I see lots of "socket
> ... died" and "created socket ..." messages in the log).
>
> What am I missing?

Set maxIdle to whatever you have maxActive set for.

Phil
>
> TIA,
> Konstantyn


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org


Mime
View raw message