You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by Konstantyn Smirnov <in...@yahoo.com> on 2010/09/13 11:01:55 UTC

[commons.pool] unexpected object invalidation

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?

TIA,
Konstantyn
-- 
View this message in context: http://apache-commons.680414.n4.nabble.com/commons-pool-unexpected-object-invalidation-tp2537096p2537096.html
Sent from the Commons - User mailing list archive at Nabble.com.

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


Re: [commons.pool] unexpected object invalidation

Posted by Konstantyn Smirnov <in...@yahoo.com>.
Bingo!

Thanks Phil.
-- 
View this message in context: http://apache-commons.680414.n4.nabble.com/commons-pool-unexpected-object-invalidation-tp2537096p2537208.html
Sent from the Commons - User mailing list archive at Nabble.com.

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


Re: [commons.pool] unexpected object invalidation

Posted by Phil Steitz <ph...@gmail.com>.
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