You are viewing a plain text version of this content. The canonical link for it is here.
Posted to modperl@perl.apache.org by Angie Ahl <al...@vertebrate.co.uk> on 2005/01/08 20:07:46 UTC

Apache::DBI

Hi

I posted this to the DBI list yesterday and no replies yet ;(. It's 
just as much a question about mod_perl and persistence as anything else 
so I thought I'd ask you lovely people.

can anyone tell me whether the following will work. I've got myself
all confused over persistency (of every kind).

I'm using Mod_perl and apache (1.3)
I have a httpd.conf containing PerlSetVar for the DB's datasource,
username and password.
I get these values in my perl module using $r->dir_config
I then pass the values to various objects (other perl modules).

eventually I hope to have just one instance of the modules on the
server with paths and db vars all being sent to the modules with
PerlSetVar.

There will be several sites all using the same modules but each will
have its own paths and config for the DB.

Will this work?
Will I have problems with variables becoming "shared" ie vars values
getting mixed up between connections?
Is PerlSetVar the best way to pass values from httpd.conf to perl 
securely?
Do I need to open/close the connection in each of the modules too or
can I pass the $dbh object to each of my external subroutines, or is
there an even better way.

I'm hoping to have a persistent database connection for each request,
but not get values mixed up between requests and sites.

I thought I'd ask the experts here before I go do something really 
bloated.

Make sense?

Thanks


Re: Apache::DBI

Posted by "Charles P. Frank" <cf...@plusthree.com>.
Hi Angie,

> I'm hoping to have a persistent database connection for each request,
> but not get values mixed up between requests and sites.

I think you should be able to connect to your datasource and store the
connection in Apache pnotes something like this in your main connection
routine:

  my $dbh;
  if ( $ENV{'MOD_PERL'} and !$Apache::ServerStarting ) {
      $dbh = Apache->request()->pnotes('dbh');
  }
  if ( !$dbh ) {
      $dbh = DBI->connect_cached(
          $config->get_dsn(),  $config->get_user(),
          $config->get_password(), $db_options
      );
      if ( $ENV{'MOD_PERL'} and !$Apache::ServerStarting ) {
          Apache->request()->pnotes( 'dbh', $dbh );
      }
  }
  return $dbh;


> I thought I'd ask the experts here before I go do something really
> bloated.

I am a bit new to the list and mod_perl, but I have asked the same
question behind the scenes and received help from the list, you will
probably here from others more qualified with a more elegant problem
solution ;)

I use the above code with Apache 1.33 and its corresponding mod_perl.

HTH,

CPF

Re: Apache::DBI

Posted by Martin Moss <ma...@btopenworld.com>.
> > Is PerlSetVar the best way to pass values from
> httpd.conf to perl
> > securely?
> 
> It's a good way, although there are plenty of
> others.  I use a separate
> config file usually.
> 

For what it's worth I use both. I keep 'site wide'
configuration vales in a Config file/module (usually
Backend DB access modules) and then 'location
specific' values in PerlSetVar's in each </Location>
I find this the most adaptable and maintainable.

regards,

Marty


	
	
		
___________________________________________________________ 
ALL-NEW Yahoo! Messenger - all new features - even more fun! http://uk.messenger.yahoo.com

Re: Apache::DBI

Posted by Perrin Harkins <pe...@elem.com>.
On Sat, 2005-01-08 at 19:07 +0000, Angie Ahl wrote:
> Will this work?

Yes.

> Will I have problems with variables becoming "shared" ie vars values
> getting mixed up between connections?

No, not if you call DBI->connect() every time you want a connection.
You only get in trouble if you keep your own copies of the database
handles around and then get them mixed up.

> Is PerlSetVar the best way to pass values from httpd.conf to perl
> securely?

It's a good way, although there are plenty of others.  I use a separate
config file usually.

> Do I need to open/close the connection in each of the modules too or
> can I pass the $dbh object to each of my external subroutines, or is
> there an even better way.

Either of those will work, or you could make your own "get_dbh()"
function that handles the dir_config stuff and calls DBI->connect() and
just use that from your subs.  

- Perrin