Dear cyril:
Sorry, but I think that you're wrong in assuming that the same connection will be reused every time you call DBI::Connect() with the same parameters. My experience says that this varies from DBD driver to DBD driver. Guess you shall use a cacheable implementation. Maybe Ima::DBI would be more appropriated.
An alternative solution could be provide a closure that knows about the database connection and Just Do The Right Thing:
#!/usr/bin/perl
my $db_conn;
{ my $dbh = eval{ DBI::Connect( @params ) }
or &RaiseError();
$db_conn = sub { return $dbh; };
}
sub one{
my $db_conn = shift;
my $dbh = &$db_conn;
# use connection here...
return;
}
sub two{
my $db_conn = shift;
my $dbh = &$db_conn;
# use connection here...
return;
}
About your second question, I guess the first contact is at the time you call DBI::Connect(): your password needs verification so the DBI/DBD engine makes sure you have authorization to connect to the specified database or if there is an error condition to be reported. Of course, this behavior depends on what DBD driver you're using.
-
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.
|