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

I have come across something that does not appear to be an issue right now, but I would like to address said issue before it does become a problem. My CGI environment consists of scripts run under mod_perl 2 and Apache 2.0.48, where Apache is compiled with the WinNT MPM (mpm_winnt.c). This means that I have one physical process handling requests, which uses threads to 'fork' off each request.

The issue I am bringing up is that of using DBI within this mod_perl environment. Not only am I keeping the database connection (to mysql) persistent; since the whole environment is threaded off via Apache, only a single database connection is established. This means that each and every request made to the mod_perl scripts work off of one database connection. I am worrying as to what will happen if and when these scripts start to see a heavy load. Is it perfectly safe for multiple instances of the same script to simultaneously access the same connection to the database? Or would I be suddenly hit with interferance between instances? Or is some method of sharing/locking mechanism automatically put in place that queues queries from each instance and serves each in turn?

  • Comment on DBI, Apache::DBI and mod_perl - Is one connection sufficient?

Replies are listed 'Best First'.
Re: DBI, Apache::DBI and mod_perl - Is one connection sufficient?
by dws (Chancellor) on Feb 25, 2004 at 19:33 UTC
    Is it perfectly safe for multiple instances of the same script to simultaneously access the same connection to the database?

    Not without someone acting as a traffic cop. A connection to a database server uses a socket. If you try to hold n>1 conversations on a single socket, you're asking for confusion. But it you set up a traffic cop, you lose a lot of the benefit of having simultaneous instances. Conversations with a database connection take a relatively long time.

    I'd look into connection pooling if I were you.

Re: DBI, Apache::DBI and mod_perl - Is one connection sufficient?
by iburrell (Chaplain) on Feb 25, 2004 at 19:36 UTC
    My impression is that DBI connections are not shared between threads in Apache 2.0. With plain DBI, connections are local to each client thread. With Apache::DBI, each thread gets its own persistent connection but the connections are not pooled together.
      That's correct, nothing is shared between threads unless you explicitly make it shared and Apache::DBI does not. Each thread will have its own connection. As long as your DBD is thread-safe, it will work fine.

        I am wondering what is wrong with my setup then (if anything). If I use the mysql client to connect to the server, it shows me "Your MySQL connection id is 0 to server version: 4.0.17-nt". Then I start up Apache, do many page requests (even tried simultaneously loading 40 pages within Opera). I then go back to the mysql client and reconnect and get connection #3. This tells me that all the page requests were handled within connection #2. So...?