http://www.perlmonks.org?node_id=412087


in reply to DBI / Mysql connections

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.