Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Tomcat, CGI scripts and DBI connection persistence

by spx2 (Chaplain)
on Oct 29, 2011 at 18:41 UTC ( #934628=perlquestion: print w/ replies, xml ) Need Help??
spx2 has asked for the wisdom of the Perl Monks concerning the following question:

Dear monks,

I've a situation in a project I'm working on where I'm not allowed to use anything other than tomcat to run CGI scripts. So basically the whole tomcat+CGI thing is settled, that's a given and it's part of the hypothesis(I know it's awkward but I can't change it).

Now every time a user decides to access some page, some cgi-bin/<scriptname>.pl is executed, makes a DB connection, processes data, throws it back to the client and then exists, and as it exists it also closes the DB connection.

If I would've been using Apache with mod_perl I could've used Apache::DBI to persist the connection, but I can't since I'm tied down to tomcat.

Is there any way to persist DBI connections which are made from CGI Perl scripts running on top of tomcat ?

Comment on Tomcat, CGI scripts and DBI connection persistence
Re: Tomcat, CGI scripts and DBI connection persistence
by ikegami (Pope) on Oct 29, 2011 at 19:57 UTC

    DB connections cannot survive a process's lifespan. I really don't know anything about Tomcat, but I don't see anything about a persistent Perl interpreter (i.e. one that survives more than one request).

      After reading the docs of Apache::DBI I thought that this is .. in fact , possible. Are you sure they can't span a process's lifespan ?

        Apache::DBI is used in combination with mod_perl. It maintains a pool of DBI connections inside each Apache process, that can be used by Perl code executed by mod_perl, still inside the Apache process.

        Alexander

        --
        Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

        Perl code executed by mod_perl is executed in a Perl interpreter in the Apache process. The interpreter is created when apache starts up and it persists on until the apache child exits.

        While the db connection is still limited to a process -- each apache process has its own pool -- that process can handle more than one HTTP request.

Re: Tomcat, CGI scripts and DBI connection persistence
by keszler (Priest) on Oct 29, 2011 at 19:58 UTC

    One option: write a standalone Perl program that runs as a daemon/service; that program maintains the database connection and answers requests from the cgi-bin/<scriptname>.pl programs. (Communication to the standalone program via socket, pipe, etc.)

      That's a good idea, but wouldn't that induce additional overhead ? opening the socket, passing data through, receiving it .. ?

        There would be overhead, but how do the cgi-bin/<scriptname>.pl programs communicate with the DB? That has all the same overhead, plus the database's authentication.

        If the individual programs typically run a small number of queries per invocation then the overhead of DB authentication is relatively more significant.

        Also, if the website is heavily used the DB might be "happier" with a small number of connections making many queries vs many connections making few queries each.

Re: Tomcat, CGI scripts and DBI connection persistence
by Perlbotics (Abbot) on Oct 29, 2011 at 20:06 UTC

    FCGI could solve your problem with the persistent DB connection, however AFAIK Tomcat does not support FCGI out of the box. Though there seems to be a working JFastCGI servlet... (never tried it myself).

    Another idea would be to emulate FCGI by creating a daemon that implements your service and interfaces with a stub CGI script, e.g. by means of (domain-)sockets? Thus, the daemon maintains a permanent connection to the DB and handles request forwarded from the CGI stub.

    client <-(HTTP)-> Tomcat <-(CGI)-> CGI-stub <-(IPC)-> Daemon <-DBI +-> DB
    Very awkward and probably not much faster than simply re-connecting to the DB... Maybe it's easier to stay with Tomcat and write a servlet (Java) or stay with the Perl (F)CGI script and use a Perl based HTTP server (e.g. Starman, Twiggy, HTTP::Server::*)? Perhaps you can have two servers running (Tomcat and Perl-HTTP-server) and edit the CGI URIs, so CGI calls get routed to the Perl HTTP server?

    Very awkward again... I would negotiate with customer or boss again to get rid of this artificial limitation. Why spend time and energy circumventing a problem that could be solved better the traditional way?

    HTH
Re: Tomcat, CGI scripts and DBI connection persistence
by bart (Canon) on Oct 29, 2011 at 21:53 UTC
    Are you sure the DBI connection is your bottleneck? You shoul time it, with Time::HiRes you fetch the times just before and after the connection, to calculate how many milliseconds it actually takes. Depending on the database this might actually be not more than a few milliseconds, in which case I'd say: don't bother.
      Thanks, I will use Time::HiRes to see if it is indeed a bottleneck

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://934628]
Approved by keszler
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (15)
As of 2014-09-23 18:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (239 votes), past polls