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.
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;
}
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|