in reply to Net::Server and DBI

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.

Replies are listed 'Best First'.
Re^2: Net::Server and DBI
by alager (Acolyte) on May 20, 2009 at 18:15 UTC
    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