J
jtbutler78
I have a module that I instantiate a DBI connection from. My question
is - I am doing the right way? It works but I dont know know I am
making a connection to the DB each time I call getResults or am I only
using the first connection I make. I call getResults inside loops or
make several calls to it within my application and its a waste to make
connections with each call.
sub new {
my ($pkg, $db_host, $db, $uid, $pwd) = @_;
my $app = {};
$app->{DB} = DBI->connect("DBI:mysql:$db:$db_host", "$uid",
"$pwd");
bless $app, $pkg;
return $app;
} #end new
sub getDB { return $_[0]->{DB} };
sub getResults {
my ($pkg, $query) = @_;
my $sth = $pkg->getDB()->prepare($query);
$sth->execute or die "Can't execute statement: $DBI::errstr";
return $sth->fetchall_arrayref();
} #getResults
is - I am doing the right way? It works but I dont know know I am
making a connection to the DB each time I call getResults or am I only
using the first connection I make. I call getResults inside loops or
make several calls to it within my application and its a waste to make
connections with each call.
sub new {
my ($pkg, $db_host, $db, $uid, $pwd) = @_;
my $app = {};
$app->{DB} = DBI->connect("DBI:mysql:$db:$db_host", "$uid",
"$pwd");
bless $app, $pkg;
return $app;
} #end new
sub getDB { return $_[0]->{DB} };
sub getResults {
my ($pkg, $query) = @_;
my $sth = $pkg->getDB()->prepare($query);
$sth->execute or die "Can't execute statement: $DBI::errstr";
return $sth->fetchall_arrayref();
} #getResults