CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Spawning multiple DB connections

 



alex29555
New User

Aug 15, 2012, 5:20 PM

Post #1 of 5 (1447 views)
Spawning multiple DB connections Can't Post

Hi experts - I'm a newbie when it comes to Perl. I'm attempting to spawn thousands of DB sessions by looping through and establishing DB connections. I never see more than a single session being spawned. One single session is created and it doesn't exit until the Perl script completes. I would assume that every connect would be a separate session until it disconnects or the Perl script ends, but I only notice one being spawned during the run of the loop. I pasted the current code I'm using below.

Any help or insight would be appreciated.
Thank you

for (my $i = 0; $i < 5000; $i++)
{
$dbh = DBI->connect("dbi:Oracle:host=esg-db2;sid=cga2-1",
'build','build123', {RaiseError => 1, AutoCommit => 1})
or die "Can't connect to database $DBI::errstr\n";
my $sql = 'select * from tab';

# Instead you could do $dbh->do($sql) or execute
$sth = $dbh->prepare($sql);
$sth->execute;
}
sleep 10000


FishMonger
Veteran / Moderator

Aug 15, 2012, 8:24 PM

Post #2 of 5 (1440 views)
Re: [alex29555] Spawning multiple DB connections [In reply to] Can't Post

You're creating a new connection at each iteration, clobbering the one before it.

Why would you want to do something so inefficient?

What is your real intention?


(This post was edited by FishMonger on Aug 15, 2012, 8:25 PM)


alex29555
New User

Aug 16, 2012, 10:12 AM

Post #3 of 5 (1428 views)
Re: [FishMonger] Spawning multiple DB connections [In reply to] Can't Post

 I need to simulate X number of connections to insure we don't run into resource limitations such as user ulimits.


FishMonger
Veteran / Moderator

Aug 16, 2012, 10:28 AM

Post #4 of 5 (1424 views)
Re: [alex29555] Spawning multiple DB connections [In reply to] Can't Post


Code
my @dbh_connection; 
my $sql = 'select * from tab';

for my $i (0..5000) {
$dbh_connection[$i] = DBI->connect("dbi:Oracle:host=esg-db2;sid=cga2-1",
'build', 'build123',
{RaiseError => 1, AutoCommit => 1})
or die "Can't connect to database $DBI::errstr\n";

my $sth = $dbh_connection[$i]->prepare($sql);
$sth->execute;
}

sleep 10000;



alex29555
New User

Aug 16, 2012, 12:05 PM

Post #5 of 5 (1417 views)
Re: [FishMonger] Spawning multiple DB connections [In reply to] Can't Post

Thank you for your response. It's exactly what I was looking for.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives