mdog has asked for the wisdom of the Perl Monks concerning the following question:

Brethern --

So I have just now gotten Perl 5.8, Apache 2.x, and mod_perl 2 (1.9x) installed. Sweet!

I now have persistent dB connections TRANSPARENTLY and the perl interpreter doesn't need to fire up, load my modules, and then execute my script -- don't have to touch my code at all. Rad!

But what about database connection pooling? I just started playing around with mod_perl and Apache::DBI but after scouring the Internet it looks like mod_perl/Apache::DBI doesn't handle connection pooling. Does this mean that if a 1000 users hitting the same script at the same time, the first one lucks out and the rest are queued up waiting for the dB handle to free up so that they can perform their lookups/inserts? Or am I misinterpretting what a persistent dB handle is all about.

Could someone please clear this up for me?

Is there a way to get mod_perl/Apache::DBI to hold (multiple) connections around so that users can exec the same CGI concurrently without having to wait?

I've looked at SQLRelay and it looks like it can do all this but I liked the idea of having dB handles tied to Apache so that I know that if Apache is up and running, dB connectivity should be as well.

I'm a little worried that if I went the SQLRelay way that if SQLRelay crashed/core dumped, that connectivity would be toast and applications would be worthless. Does anyone know if SQLRelay has some sort of monitor that would restart itself if it were to die. I'm sure something could be written but it's nice when it's built in.

I think I am done rambling now...

Thanks in advance,
mdog

Replies are listed 'Best First'.
Re: Database Connection Pooling
by runrig (Abbot) on Mar 06, 2003 at 22:41 UTC
    Is there a way to get mod_perl/Apache::DBI to hold (multiple) connections around so that users can exec the same CGI concurrently without having to wait?
    Apache::DBI does hold the database connections open. One connection for each process. But only under mod_perl, not CGI (don't confuse the two, Apache::Registry lets your CGI scripts run under mod_perl, but then it's not CGI anymore). That's not connection pooling though. Each apache process will hold a database connection open, which means if you allow 1000 simultaneous processes for 1000 people hitting your site at once, there are 1000 connections. (update: connection "pooling" would be something more like having those 1000 processes share a pool of 100 connections, and having each processes grabbing an available connection as needed, then releasing it back to the pool when done).

    True connection pooling is possible with Sybase (there's a module on CPAN), but most databases don't support that. Besides, Sybase only allows one active statement handle per connection, so it tends to need more than one connection more than other databases (update: that's been changed, it now transparently opens more connections for more statement handles, but now I'm probably getting OT).

    Update: Just noticed that it says Apache 2.x, which I vaguely think I remember supports connection pooling, but I don't know how...

    and more thoughts on the subject (a post in the thread in the first link of this google currently proposes that connection pooling is actually rarely needed, and that people only think they need it because MS has it).

      I think that cleared things up for me...Thanks :)

      I am running my scripts under mod_perl using: PerlResponseHandler ModPerl::PerlRun (just to see how they'd work, will probably use Registry later).

      Just wanted to verify something you wrote, though:

      Each apache process will hold a database connection open...

      You are saying that for each Apache process that is spawned, it gets its own database handle that it holds?

        Each Perl interpreter keeps a connection open. With the pre-fork MPM (or mod_perl 1.x), that means one per process.

        There is a working prototype right now for true pooling of db connections between threads, so hopefully that will be available soon. However, this is really only helpful for applications that do a lot of non-database work and ones where each user has a different database login.

Re: Database Connection Pooling
by Coruscate (Sexton) on Mar 07, 2003 at 04:08 UTC

    Just wanted to say... lucky you. I have mod_perl 1.99_05-3 and apache 2.0.40 installed, and I can't seem to get Apache::DBI to work. It needs Apache2.pm, and some otehr things, and none of it is working :(


    If the above content is missing any vital points or you feel that any of the information is misleading, incorrect or irrelevant, please feel free to downvote the post. At the same time, please reply to this node or /msg me to inform me as to what is wrong with the post, so that I may update the node to the best of my ability.

Re: Database Connection Pooling
by RiotTown (Scribe) on Mar 10, 2003 at 17:49 UTC
    You may also want to check out SQLrelay. I've messed with it a bit in a testing environment, and it seems to do exactly what you are looking for. The documentation leaves a little to be desired, and some of the other engineers I work with seem a little worried about the stability of the group maintaining the code, but it may be worth a look:

    sqlrelay.sourceforge.net