Re: cgi and perl database interaction

[email protected] (Paweł Prędki) Wed, 02 Dec 2009 11:01:29 +0100
Newsgroups perl.beginners.cgi
Message-ID <[email protected]>

Greg Jetter pisze:
> On Tuesday 01 December 2009 2:52:38 pm Paweł Prędki wrote:
>> Hello,
>> I have a website that uses a php engine for news generation and,
>> basically, most of the other pages. It uses a MySQL database to store
>> the majority of the page contents (i.e. news).
>>
>> However, I've written before that I've started using simple CGI scripts
>> in Perl to make some activities automatic (i.e. statistics updates,
>> standings updates - it's a sports-related website :) ).
>>
>> At first, I used the Storable module and kept all the data in flat files
>> but it generally is not the best solution so I moved to DBI.
>>
>> Now, the thing is that the PHP scripts also connect to the database and,
>> presumably, uphold the connection over the duration of the session so as
>> not to disconnect and reconnect continually when the user browses the
>> website.
>>
>> My question is - is it possible to do the same thing with those CGI
>> scripts? At the moment, each script 'requires' a module where a function
>> is defined which returns a database handle upon connecting to the
>> database. This is not an efficient solution and I would like to change
>> that. There is no mod_perl running on the server but maybe there is a
>> way to keep the connection via some Apache mechanisms. I'm not
>> experienced with the server operation that much so forgive me if what I
>> wrote is hogwash ;)
>> Cheers,
>> Pawl
> 
> There  is absolutely no  reason to keep a  connection to the database  active 
> once the query has finished and the results are fetched and processed , doing 
> so  only ties up system resources and memory. The default  for  Mysql in a  
> un- altered server  install is 50 concurrent connections.  A web site can very 
> easily surpass this  if the  connections are  kept alive. The  behavior of the  
> DBI module returning a handle that you interact  with  is the most efficient use 
> of  resources .   And I'm pretty sure if you research it you will find that PHP 
> does the same thing as PHP is modled after Perl on many levels. Once the 
> object is created it persist for the duration of the  script  or until it is   
> destroyed with a call to disconnect. Or you risk  getting "unable to connect , 
> too many  connections" from your MySql server. and if you  have not  
> programmed  to handle the error , you  will get  silent failure with  the  
> page  just hanging up  never completing a read or write.  
> 
> good luck
> 
> Greg
> 
> 
> 
If that's the case then there is no problem. I thought that the database 
connection persists until the user navigates away from the site (there 
is some kind of an inactivity timeout). Taking into consideration that 
there are many sql queries from one single page consisting of many 
separate php 'modules' I would assume that each of the separate files 
doesn't connect to the db on its own but rather uses one connection.

I guess that it is possible for the first include in each php file to 
create a database connection which is then visible to all the submodules 
included in the same file. I need to look into the php mechanism more 
closely.

Thanks for your input.
Pawel