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

I would like to have each pre-forked Net::Server process start it's connection to the database using DBI and maintain the database connection.
The issue I'm running into is that the process_request() function doesn't get called until after the socket connection has been established, so the database connection doesn't get started until the socket connection is started, then when the socket goes away, so does the database connection.

How can I get each process to start the DBI connection before the socket is established and maintain the connection after the socket is done?

Thanks,
Aaron

Replies are listed 'Best First'.
Re: Net::Server and DBI
by zwon (Abbot) on May 19, 2009 at 20:20 UTC

    I have no idea how to establish DBI connection just after fork, it seems there's no appropriate hook in Net::Server, but you can establish DBI connection in post_accept hook. This is after the client connected, but you should do it only for first client, just check if the connection already established. In order to maintain DBI connection between requests you should store DBI handler in some global variable, if you storing it in the some variable local for process_request, it's destroyed after you return from the function.

      zwon,

      I guess what I'm not sure about is, if I put the DBI->connect() call before process_request() then will it still be part of the child, or will it belong to the parent?

      use base qw(Net::Server::PreFork); MyPackage->new({conf_file => '/home/aaron/serv.conf',})->run; //does $dbh belong to the parent? $dbh = DBI->connect("DBI:mysql:$DSN", $user, $password); sub process_request { //do child stuff here }

      As for why I chose to use Net::Server, it seemed easy and reliable. And it is so far. This is my first issue with it directly and I'm nearly done with my app. I'm just trying to make things more efficient, like not having to establish a database connection for each socket connection. I want the sockets to come and go, while I maintain a single DB connection per process.

        Yes, post_accept and process_request are executed by the same process. Here's the sample that demonstrates this:

        use strict; use warnings; MyServer->run(port => 7777); package MyServer; use base qw(Net::Server::PreFork); sub post_accept { warn "post accept in $$\n"; } sub process_request { warn "process request in $$\n"; } __END__ ... post accept in 9238 process request in 9238 post accept in 9241 process request in 9241 post accept in 9242 process request in 9242
Re: Net::Server and DBI
by targetsmart (Curate) on May 20, 2009 at 05:42 UTC
    Can you tell me why you have chosen Net::Server for your requirement
    Just wanted to know.

    Vivek
    -- In accordance with the prarabdha of each, the One whose function it is to ordain makes each to act. What will not happen will never happen, whatever effort one may put forth. And what will happen will not fail to happen, however much one may seek to prevent it. This is certain. The part of wisdom therefore is to stay quiet.