in reply to Connection Pooling

Please show samples of your code and/or anything showing research you've done on the topic.

Replies are listed 'Best First'.
Re^2: Connection Pooling
by muk (Initiate) on Oct 28, 2011 at 04:23 UTC

    Hi

    Thanks for replying

    I wrote a package,the code is as below,it is working fine but the only issue is that every time there is a request a new factory is created.

    I want to know how to create the factory only once and reuse the connections in the pool.

    use ResourcePool; use ResourcePool::Factory; use ResourcePool::Factory::DBI; $db_timeOut = 120; # wait 60 seconds for connection #User credentials $data_source = 'dbi:DB2:databasename'; $username = 'username'; $auth = 'password'; eval{ # set alarm to timeout current operation local $SIG{ALRM} = \&input_timed_out; alarm $db_timeOut; $factory = ResourcePool::Factory::DBI->new($data_source,$user +name,$auth); $pool = ResourcePool->new($factory, Max => 2, MaxTry => 3, Sle +epOnFail => [30, 60] ,MaxExecTry => 3); }; alarm 0; # reset the alarm sub resource_pool { $resource = $pool->get(); return $resource ; } sub release_pool { $pool->free($resource); } sub input_timed_out { print "connection timed out hence exiting\n"; exit; }

      If it's opening new connections every time I'd look into why it's closing the connections in the first place.

      With 'Max' set to 2, $pool->get() should return undef when you try to exceed 2 connections.

      Set up a test where you attempt to get 3 resources simultaneously and watch the database logs to see what's going on with the connections.