Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: ModPerl: ModPerl

preloading modules and apache::dbi

 

 

ModPerl modperl RSS feed   Index | Next | Previous | View Threaded


mcardeiro at yahoo

Jan 18, 2012, 2:08 PM

Post #1 of 7 (1094 views)
Permalink
preloading modules and apache::dbi

Hi,

I am totally new to mod perl (after 13+ years of building web applications on shared servers I now am developing on a dedicated box woo-hoo)

I have installed apache::dbi and set it to preload in the apache config file.  Here is what I think my problem might be.

I have a library I want to preload.  This library makes a database connection and assigns it to a variable that is exported so all programs can use this handle.

will this global handle db handle be a single handle amongst all apache processes, or will each process load this library on its own thus having a unique db handle for each httpd process?

sorry if this is incoherent.

Mike Cardeiro


brettlee at yahoo

Jan 18, 2012, 2:17 PM

Post #2 of 7 (1043 views)
Permalink
Re: preloading modules and apache::dbi [In reply to]

Hi Mike,

I believe the answer is that each http process will have its own handle.  At least that's what it appears to be when I view the handles via http://hostname/perl-status.

Hope you enjoy mod_perl.

 
Best regards,
- -
Brett Lee
Encrypt your data with PDS - http://crypto.brettlee.com/


>________________________________
> From: mike cardeiro <mcardeiro [at] yahoo>
>To: modper <modperl [at] perl>
>Sent: Wednesday, January 18, 2012 10:08 PM
>Subject: preloading modules and apache::dbi
>
>
>Hi,
>
>
>I am totally new to mod perl (after 13+ years of building web applications on shared servers I now am developing on a dedicated box woo-hoo)
>
>
>I have installed apache::dbi and set it to preload in the apache config file.  Here is what I think my problem might be.
>
>
>I have a library I want to preload.  This library makes a database connection and assigns it to a variable that is exported so all programs can use this handle.
>
>
>will this global handle db handle be a single handle amongst all apache processes, or will each process load this library on its own thus having a unique db handle for each httpd process?
>
>
>sorry if this is incoherent.
>
>
>Mike Cardeiro
>
>


perrin at elem

Jan 19, 2012, 3:57 AM

Post #3 of 7 (1046 views)
Permalink
Re: preloading modules and apache::dbi [In reply to]

On Wed, Jan 18, 2012 at 5:08 PM, mike cardeiro <mcardeiro [at] yahoo> wrote:
> I have a library I want to preload.  This library makes a database
> connection and assigns it to a variable that is exported so all programs can
> use this handle.
>
> will this global handle db handle be a single handle amongst all apache
> processes, or will each process load this library on its own thus having a
> unique db handle for each httpd process?

Apache::DBI does not make handles persistent when you open them during
startup. However, if you are trying to make the handle persistent
yourself, by putting it in a global variable or something similar,
that will be a problem. You have to close all connections you open
during startup, and open new ones in the child processes.

- Perrin


jnarins at seniorbridge

Jan 19, 2012, 5:48 AM

Post #4 of 7 (1051 views)
Permalink
RE: preloading modules and apache::dbi [In reply to]

The idea of Apache::DBI is that you get to pool connections.

If you call Apache::DBI->new and there is a spare connection, you get it, if not, one is created for you.

You almost certainly don't want one $db object being shared as a member of a class, unless your entire program also happens to represent one DB transaction.





Josh Narins
Director of Application Development
SeniorBridge

845 Third Ave
7th Floor
New York, NY 10022
Tel: (212) 994-6194
Fax: (212) 994-4260
Mobile: (917) 488-6248
jnarins [at] seniorbridge
seniorbridge.com<http://www.seniorbridge.com/>

[http://www.seniorbridge.com/images/seniorbridgedisclaimerTAG.gif]


________________________________
SeniorBridge Statement of Confidentiality: The contents of this email message are intended for the exclusive use of the addressee(s) and may contain confidential or privileged information. Any dissemination, distribution or copying of this email by an unintended or mistaken recipient is strictly prohibited. In said event, kindly reply to the sender and destroy all entries of this message and any attachments from your system. Thank you.
From: mike cardeiro [mailto:mcardeiro [at] yahoo]
Sent: Wednesday, January 18, 2012 5:08 PM
To: modper
Subject: preloading modules and apache::dbi

Hi,

I am totally new to mod perl (after 13+ years of building web applications on shared servers I now am developing on a dedicated box woo-hoo)

I have installed apache::dbi and set it to preload in the apache config file. Here is what I think my problem might be.

I have a library I want to preload. This library makes a database connection and assigns it to a variable that is exported so all programs can use this handle.

will this global handle db handle be a single handle amongst all apache processes, or will each process load this library on its own thus having a unique db handle for each httpd process?

sorry if this is incoherent.

Mike Cardeiro


mpeters at plusthree

Jan 19, 2012, 6:29 AM

Post #5 of 7 (1039 views)
Permalink
Re: preloading modules and apache::dbi [In reply to]

On 01/19/2012 08:48 AM, Josh Narins wrote:
> The idea of Apache::DBI is that you get to pool connections.
>
> If you call Apache::DBI->new and there is a spare connection, you get
> it, if not, one is created for you.

This isn't quite accurate. Apache::DBI doesn't do connection pooling, it
manages persistent connections. It's still one connection per-process
(per connection attributes) and connections aren't shared between child
processes (that's where things get messy).

Apache::DBI makes sure you don't create more connections than needed and
makes sure that the persistent connections are still connected before
handing them back.

> You almost certainly don't want one $db object being shared as a member
> of a class, unless your entire program also happens to represent one DB
> transaction.

This is actually a pretty common and ok pattern. Remember that execution
through a request is still linear (not threaded or handled by multiple
processes) so if you want your whole request to be a single transaction
you can, or if you want it to auto-commit you can, or if you want it to
be a mix of transactions and auto-committed statements, you can do that too.

When you use Apache::DBI and then call DBI->connect or
DBI->connect_cached you'll get the same database connection every time
(per process of course).

--
Michael Peters
Plus Three, LP


mcardeiro at yahoo

Jan 19, 2012, 6:41 AM

Post #6 of 7 (1043 views)
Permalink
Re: preloading modules and apache::dbi [In reply to]

> From: Perrin Harkins <perrin [at] elem>



> However, if you are trying to make the handle persistent
>yourself, by putting it in a global variable or something similar,
>that will be a problem.  You have to close all connections you open
>during startup, and open new ones in the child processes.
>


heres kinda sorta whats going on.   The library Main.pm is preloaded at apache startup.  it exports a variable $dbh which is empty.

When a cgi script runs and uses Main.pm it runs an initialize subroutine from Main.pm.  In that subroutine the  $dbh variable gets the return from DBI::connect. 


Since Main.pm is preloaded in apache startup, is there 1 $dbh variable shared amongst all httpd processes; or does each process have it's own $dbh variable?


perrin at elem

Jan 19, 2012, 9:59 AM

Post #7 of 7 (1040 views)
Permalink
Re: preloading modules and apache::dbi [In reply to]

On Thu, Jan 19, 2012 at 8:48 AM, Josh Narins <jnarins [at] seniorbridge> wrote:
> The idea of Apache::DBI is that you get to pool connections.
>
> If you call Apache::DBI->new and there is a spare connection, you get it, if not, one is created for you.

That's a little misleading. There's no actual pooling. Connections
can't be shared between processes or threads.

What Apache::DBI does is keep connections that you open around in a
global hash, and if you ask for another connection with the same
parameters it gives you back the one you already opened. There will
never be two connections with the same parameters. I like to call it
persistence, not pooling.

- Perrin

ModPerl modperl RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.