You are viewing a plain text version of this content. The canonical link for it is here.
Posted to modperl@perl.apache.org by Kelvin Wu <ke...@gmail.com> on 2007/03/29 15:27:38 UTC

Apache::DBI questions

I have CGI script which uses normal DBI mysql calls(DBD, DBI::Mysql), I am
tryint to port it to mod_perl2 and Apache::DBI.

1. Do I still need to use 'use Apache::DBI;' in my script (or in startup.pl) if
I already set 'PerlModule Apache::DBI' in httpd.conf?

2. Will Apache::DBI work (caches and manages connections etc) if I dont
change my CGI script DB calls but simply adding one line "use Apache::DBI"?

3. How to limit DB connections from script? Or number of connections simply
equals to number of HTTPd?

4. By setting $Apache::DBI::DEBUG = 2; Where output goes to?

5. How many DB connections will be established by using
Apache::DBI->connect_on_init()?

6. If I use Apache::DBI->connect_on_init() in startup.pl, will it take care
of DBI->connect() in my script? eg, DBI->connect() wont actually create a
new connection.

7. If I do NOT use Apache::DBI->connect_on_init() in startup.pl, will
Apache::DBI still manage DBI->connect() in my script? eg, wont actually
create a new connection if there is a free connection created by
ex-DBI->connect() call.

8. Will $dbh->disconnect() simply be ignored by using Apache::DBI or, it
will kill DB connection?

Thanks for your help.

-- 
Sent from my BlackBerry. Ignore the typos unless they're funny.

Re: Apache::DBI questions

Posted by "Philip M. Gollucci" <pg...@p6m7g8.com>.
Kelvin Wu wrote:
> 1. Do I still need to use 'use Apache::DBI;' in my script (or in 
> startup.pl) if I already set 'PerlModule Apache::DBI' in httpd.conf?
you should _never_ use Apache::DBI in your script itself.
1)
httpd.conf
<Perl>
   use Aapche::DBI ();
</Perl>

2) httpd.conf
PerlModule Apache::DBI

3) startup.pl
   use Apache::DBI ();

Are all equivalent

> 2. Will Apache::DBI work (caches and manages connections etc) if I dont 
> change my CGI script DB calls but simply adding one line "use Apache::DBI"?
Yes.

> 3. How to limit DB connections from script? Or number of 
> connections simply equals to number of HTTPd?
in prefork mpm, you'll need at least 1 $dbh perl httpd child process
Theres nothing stopping your from opening more though (aka 2 different 
dbs 1 each)

> 4. By setting $Apache::DBI::DEBUG = 2; Where output goes to?
STDERR aka error_log

> 5. How many DB connections will be established by using 
> Apache::DBI->connect_on_init()?
This doesn't change the number of connects, this just sets it so that
when httpd children are spawned we add a PerlInitChildHandler() that
create/connects the $dbh for this child.  Without this, the _first_
request per child will have to actually connect to the db and _then_
it will be managed by Apache::DBI.

> 6. If I use Apache::DBI->connect_on_init() in startup.pl, will it take 
> care of DBI->connect() in my script? eg, DBI->connect() wont actually 
> create a new connection.
connect_on_init() has no effect on the script.  DBI knows about 
Apache::DBI and if Apache::DBI is loaded (correctly) will forward all
calls to DBI::connect() -> Apache::DBI::connect()

> 7. If I do NOT use Apache::DBI->connect_on_init() in startup.pl, will 
> Apache::DBI still manage DBI->connect() in my script? eg, wont actually 
> create a new connection if there is a free connection created by 
> ex-DBI->connect() call.
Ditto #6.

> 8. Will $dbh->disconnect() simply be ignored by using Apache::DBI or, it 
> will kill DB connection?
Apache::DBI overrides DBI::disconnect.  You should not change your 
current $dbh->disconnect() calls.

# overload disconnect
{
   package Apache::DBI::db;
   no strict;
   @ISA=qw(DBI::db);
   use strict;
   sub disconnect {
       my $prefix = "$$ Apache::DBI            ";
       Apache::DBI::debug(2, "$prefix disconnect (overloaded)");
       1;
   }
   ;
}

Notice all it does it write to the log.

-- 
------------------------------------------------------------------------
Philip M. Gollucci (pgollucci@p6m7g8.com) 323.219.4708
Consultant / http://p6m7g8.net/Resume/resume.shtml
Senior Software Engineer - TicketMaster - http://ticketmaster.com
1024D/EC88A0BF 0DE5 C55C 6BF3 B235 2DAB  B89E 1324 9B4F EC88 A0BF

Work like you don't need the money,
love like you'll never get hurt,
and dance like nobody's watching.

Re: Apache::DBI questions

Posted by Rafael Caceres <rc...@aasa.com.pe>.
On Thu, 2007-03-29 at 10:57 -0400, Jonathan Vanasco wrote:
> On Mar 29, 2007, at 9:27 AM, Kelvin Wu wrote:
> 
> > 1. Do I still need to use 'use Apache::DBI;' in my script (or in  
> > startup.pl) if I already set 'PerlModule Apache::DBI' in httpd.conf?
Putting it in startup.pl makes sure that memory sharing is maximized. 

> > 2. Will Apache::DBI work (caches and manages connections etc) if I  
> > dont change my CGI script DB calls but simply adding one line "use  
> > Apache::DBI"?
In fact, just keep the 'use DBI;' in your CGI scripts, Apache::DBI works
under the hood to make sure that IDENTICAL connections made from scripts
under the same httpd child will use the same handles.

Rafael



Analizado por ThMailServer para Linux.

Re: Apache::DBI questions

Posted by Perrin Harkins <ph...@gmail.com>.
On 3/29/07, Kelvin Wu <ke...@gmail.com> wrote:
> The Apache::DBI perldoc is too simple to understand its usage

There is more documentation available here:
http://www.modperlbook.org/html/ch20_01.html

- Perrin

Re: Apache::DBI questions

Posted by Kelvin Wu <ke...@gmail.com>.
The Apache::DBI perldoc is too simple to understand its usage, but I would
like to thank your guys for your great explaination on my questions... :-)



-- 
Sent from my BlackBerry. Ignore the typos unless they're funny.

Re: Apache::DBI questions

Posted by Jonathan Vanasco <mo...@2xlp.com>.
On Mar 29, 2007, at 9:27 AM, Kelvin Wu wrote:

> 1. Do I still need to use 'use Apache::DBI;' in my script (or in  
> startup.pl) if I already set 'PerlModule Apache::DBI' in httpd.conf?
> 2. Will Apache::DBI work (caches and manages connections etc) if I  
> dont change my CGI script DB calls but simply adding one line "use  
> Apache::DBI"?
someone who uses registry will have to comment.

> 3. How to limit DB connections from script? Or number of  
> connections simply equals to number of HTTPd?
number of connections = httpd children * number of unique connect  
strings
if 2 connect strings are different -- same db/user/pass, but  
different transaction setting , there will be 2 connections.

> 4. By setting $Apache::DBI::DEBUG = 2; Where output goes to?
it should go to STDERR, which is either the apache error log or the  
vhost error log.

> 5. How many DB connections will be established by using Apache::DBI- 
> >connect_on_init()?
1 connection per unique connect string, per child

will make 1 connection per child
	Apache::DBI->connect_on_init( 'abc' )
will make 2 connections per child
	Apache::DBI->connect_on_init( 'abc' )
	Apache::DBI->connect_on_init( 'abcd' )

multiply that by # of apache children

> 6. If I use Apache::DBI->connect_on_init() in startup.pl, will it  
> take care of DBI->connect() in my script? eg, DBI->connect() wont  
> actually create a new connection.
yes. it hijacks.

> 7. If I do NOT use Apache::DBI->connect_on_init() in startup.pl,  
> will Apache::DBI still manage DBI->connect() in my script? eg, wont  
> actually create a new connection if there is a free connection  
> created by ex-DBI->connect() call.
yes.

> 8. Will $dbh->disconnect() simply be ignored by using Apache::DBI  
> or, it will kill DB connection?
its ignored.
you need to do some fancy stuff to force a disconnect , if thats what  
you want to do.