cyril has asked for the wisdom of the Perl Monks concerning the following question:
Hi,
I have a question regarding how DB connections work within a single process perl script. Hope someone doesn't mind giving me a few pointers.
I have a question regarding how DB connections work within a single process perl script. Hope someone doesn't mind giving me a few pointers.
If I was to have a sub routine that setups a connection and returns a connection handler variable (or perhaps a reference), such as the code shown below. If I then call this sub everytime I need to make a query (my $dbh = &dbConnect), am I right in thinking that a new connection would NOT be openend every time but that DBI would use an existing connection if one existed and was not already in use; assuming that I hadn't yet called disconnect()
My second question is: Is the connection opened on the call to connect or is it delayed until the first query is prepared?
Regards to all,
Cyril
sub one { my $dbh = &dbConnect; # query continues here } sub two { my $dbh = &dbConnect; # query continues here } sub dbConnect { my $dsn = "DBI:mysql:database:localhost"; my $db_user_name = 'username'; my $db_password = 'password'; my $dbh; $dbh = DBI->connect ($dsn, $db_user_name, $db_password, { RaiseError => 1 } ) or die("cannot connect to DB: ".DBI::errstr. +"\n",$dbh); return $dbh; }
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: DBI / Mysql connections
by Mutant (Priest) on Dec 03, 2004 at 11:12 UTC | |
by radiantmatrix (Parson) on Dec 03, 2004 at 15:48 UTC | |
Re: DBI / Mysql connections
by perrin (Chancellor) on Dec 03, 2004 at 11:54 UTC | |
Re: DBI / Mysql connections
by monsieur_champs (Curate) on Dec 03, 2004 at 11:24 UTC | |
Re: DBI / Mysql connections
by pingo (Hermit) on Dec 03, 2004 at 11:05 UTC | |
Re: DBI / Mysql connections
by cyril (Acolyte) on Dec 03, 2004 at 14:07 UTC | |
by Anonymous Monk on Dec 03, 2004 at 15:32 UTC | |
by Errto (Vicar) on Dec 04, 2004 at 05:27 UTC |
Back to
Seekers of Perl Wisdom