A Day in the Life of #Apache
Subject:   RewriteMap
Date:   2007-08-27 16:31:34
From:   Jezzdk
I recently came across your article on RewriteMap, and I have something to add in the case of the prg map example.
Since apache opens a kind of persistent connection to the script in question, any changes made to resources used by that script can break the entire thing. Especially when using databases. In the given example:

use DBI;
my $dbh = DBI->connect('DBI:mysql:wordpress:dbserver',
'username', 'password');
my $sth = $dbh->prepare("SELECT ID FROM wp_posts
WHERE post_name = ?");
my $ID;

# Rewrite permalink style links to article IDs
while (my $post_name = <STDIN>) {
chomp $post_name;

print "/wordpress/index.php?p=$ID\n";

Imagine what would happen if the connection to the database is lost (i.e. due to a restart of the mysql service). By putting the connection string inside the while loop, we make sure that a connection is established for each call.

I'm no expert in either perl or apache, but for me it seems to work better this way. The question is, will it have an impact on performance (is connections closed properly etc.)?