perl DBI OO
am 13.08.2007 16:35:01 von 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
Re: perl DBI OO
am 13.08.2007 17:27:08 von JT
jtbutler78@comcast.net wrote:
> 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);
I guess it's about this line. No, you don't create a new connection
every time since the getDB method returns a value you stored after
you called connect(). You call connect() only each time you create
a new object.
> $sth->execute or die "Can't execute statement: $DBI::errstr";
> return $sth->fetchall_arrayref();
> } #getResults
What I would think about is why you writw your own class when
there are existing ones like e.g. DBIx::Class etc. - you don't
even have to write SQL anymore which makes writing programs a
lot simpler and less tedious.
Regards, Jens
--
\ Jens Thoms Toerring ___ jt@toerring.de
\__________________________ http://toerring.de
Re: perl DBI OO
am 13.08.2007 17:34:00 von jtbutler78
On Aug 13, 11:27 am, j...@toerring.de (Jens Thoms Toerring) wrote:
> jtbutle...@comcast.net wrote:
> > 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);
>
> I guess it's about this line. No, you don't create a new connection
> every time since the getDB method returns a value you stored after
> you called connect(). You call connect() only each time you create
> a new object.
>
> > $sth->execute or die "Can't execute statement: $DBI::errstr";
> > return $sth->fetchall_arrayref();
> > } #getResults
>
> What I would think about is why you writw your own class when
> there are existing ones like e.g. DBIx::Class etc. - you don't
> even have to write SQL anymore which makes writing programs a
> lot simpler and less tedious.
> Regards, Jens
> --
> \ Jens Thoms Toerring ___ j...@toerring.de
> \__________________________ http://toerring.de
I will look into it - I didnt know about that module. I basically
want to have one place where I make/modify my connections.
Re: perl DBI OO
am 13.08.2007 19:04:27 von brian d foy
In article <1187015701.377762.127150@22g2000hsm.googlegroups.com>,
<"jtbutler78@comcast.net"> wrote:
> 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.
Although it looks like you're fine in this situation, if you're ever
curious what DBI is doing, you can use tracing feature:
DBI->trace($level);
See the DBI docs for details
--
Posted via a free Usenet account from http://www.teranews.com
Re: perl DBI OO
am 13.08.2007 19:06:44 von xhoster
"jtbutler78@comcast.net" wrote:
> 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.
You are making a new connection each time you call "new", and just re-use
that connection each time you call getResults on a given object obtained
by new. So if your class is only instantiated once per program, then
you only make one connection. But from the bigger picture, I'd have to
wonder what the point is. It looks like you just made a simple wrapper
around DBI which adds no new functionality but takes away a lot of existing
functionality.
Xho
--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB
Re: perl DBI OO
am 13.08.2007 21:03:05 von jtbutler78
On Aug 13, 1:06 pm, xhos...@gmail.com wrote:
> "jtbutle...@comcast.net" wrote:
> > 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.
>
> You are making a new connection each time you call "new", and just re-use
> that connection each time you call getResults on a given object obtained
> by new. So if your class is only instantiated once per program, then
> you only make one connection. But from the bigger picture, I'd have to
> wonder what the point is. It looks like you just made a simple wrapper
> around DBI which adds no new functionality but takes away a lot of existing
> functionality.
>
> Xho
>
> --
> --------------------http://NewsReader.Com/------------------ --
> Usenet Newsgroup Service $9.95/Month 30GB
I made this because I am making the calls all the time and its much
cleaner code-wise.
Re: perl DBI OO
am 13.08.2007 21:30:14 von xhoster
"jtbutler78@comcast.net" wrote:
> On Aug 13, 1:06 pm, xhos...@gmail.com wrote:
> > "jtbutle...@comcast.net" wrote:
> > > 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.
> >
> > You are making a new connection each time you call "new", and just
> > re-use that connection each time you call getResults on a given object
> > obtained by new. So if your class is only instantiated once per
> > program, then you only make one connection. But from the bigger
> > picture, I'd have to wonder what the point is. It looks like you just
> > made a simple wrapper around DBI which adds no new functionality but
> > takes away a lot of existing functionality.
> >
>
> I made this because I am making the calls all the time and its much
> cleaner code-wise.
It is cleaner than just turning on RaiseError and then
calling $dbi->selectall_arrayref as needed?
At least that way you will get the right error messages. With your code,
you're likely to get can't call method on undef value errors instead of the
correct error message, as you only do error checking at one out of 3
potential DBI-error points.
Xho
--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB
Re: perl DBI OO
am 14.08.2007 00:00:10 von jtbutler78
On Aug 13, 3:30 pm, xhos...@gmail.com wrote:
> "jtbutle...@comcast.net" wrote:
> > On Aug 13, 1:06 pm, xhos...@gmail.com wrote:
> > > "jtbutle...@comcast.net" wrote:
> > > > 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.
>
> > > You are making a new connection each time you call "new", and just
> > > re-use that connection each time you call getResults on a given object
> > > obtained by new. So if your class is only instantiated once per
> > > program, then you only make one connection. But from the bigger
> > > picture, I'd have to wonder what the point is. It looks like you just
> > > made a simple wrapper around DBI which adds no new functionality but
> > > takes away a lot of existing functionality.
>
> > I made this because I am making the calls all the time and its much
> > cleaner code-wise.
>
> It is cleaner than just turning on RaiseError and then
> calling $dbi->selectall_arrayref as needed?
>
> At least that way you will get the right error messages. With your code,
> you're likely to get can't call method on undef value errors instead of the
> correct error message, as you only do error checking at one out of 3
> potential DBI-error points.
>
> Xho
>
> --
> --------------------http://NewsReader.Com/------------------ --
> Usenet Newsgroup Service $9.95/Month 30GB
Good call I will make the change. I am still looking into DBIx::Class
as well.
Re: perl DBI OO
am 16.08.2007 19:02:18 von Ted Zlatanov
On Mon, 13 Aug 2007 08:34:00 -0700 "jtbutler78@comcast.net" wrote:
jn> I will look into [DBIX::Class] - I didnt know about that module. I
jn> basically want to have one place where I make/modify my connections.
I have been happiest with Rose::DB::Object (on CPAN). It uses the
Rose::DB class to hold database connections, so you can at least use
Rose::DB to cache and manage connections. I would consider RDBO as a
whole solution, though. It's very good.
Ted