(Database-)Object initialization during server-startup

2006-06-23 Thread Tobias Kremer
Hey guys, I'm wondering what's the best way to design a database-backed module which works completely on its own as well as within mod_perl (mp1). The module should make a database connection during object initialization (new). This connection should then be used by all methods which want to

Re: (Database-)Object initialization during server-startup

2006-06-23 Thread Perrin Harkins
Tobias Kremer wrote: That's easy under standalone conditions (connect within new, store the dbh in $self-{_dbh} and use that in other methods). I don't recommend doing that. Better to use DBI-connect_cached in most cases. It will actually check to see if your connection is still good

Re: (Database-)Object initialization during server-startup

2006-06-23 Thread Tobias Kremer
Zitat von Perrin Harkins [EMAIL PROTECTED]: You need to separate managing your database connections from caching this data. They are not related, and there's no reason to do both in one class. Either just call connect_cached all the time (it uses Apache::DBI when it finds it loaded), or

Re: (Database-)Object initialization during server-startup

2006-06-23 Thread Perrin Harkins
Tobias Kremer wrote: Ok, got that! But how does the class get its database handle to operate on? One of these ways: my $dbh = DBI-connect_cached(...); my $dbh = My::Database-get_dbh(); You do that every time you need one. sub new { ... $self-{_dbh} = My::Database-new()-dbh(); ... } No,

Re: (Database-)Object initialization during server-startup

2006-06-23 Thread Perrin Harkins
John ORourke wrote: It does make a separate DB connection in each apache process but I think trying to share 1 DB connection between httpd's is probably going to cost more than it gains. It's not actually possible to share a connection between processes unless your database library

Re: (Database-)Object initialization during server-startup

2006-06-23 Thread Tobias Kremer
Zitat von Perrin Harkins [EMAIL PROTECTED]: Create a new instance of the mod_perl handler module during startup and refer to that for the handlers (code below). This module would then cache DB connections and provide a method for 'create or fetch current DB connection'.

Re: (Database-)Object initialization during server-startup

2006-06-23 Thread John ORourke
Perrin Harkins wrote: Create a new instance of the mod_perl handler module during startup and refer to that for the handlers (code below). This module would then cache DB connections and provide a method for 'create or fetch current DB connection'. DBI-connect_cached will do all of this