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

The question is, the data source of my web application is not a database but a remote server which could be communicated through a private protocol based on tcp connection. As creating a new connection to the data server for every http request is not very effective, I do want to have only one connection created when the site start and kept for later use. I know that I can create a daemon process to communicate with the remote server, but it will too complicated. Is there any simple resolution for my problem? Or I have to move to java to see if jsp could do it. Please forgive me for my poor english. I tried my best to make me clear.
  • Comment on How to cache a socket in cgi programming?

Replies are listed 'Best First'.
Re: How to cache a socket in cgi programming?
by Fletch (Bishop) on May 22, 2006 at 17:46 UTC

    Short answer is "you can't really", at least for vanilla CGI. Every request that's dispatched by your CGI program is a separate process which services the request and then exits. Having said that, you do have options but they require more than just setting up a CGI does.

    One option is to move to something like FastCGI or mod_perl, which keep a persistent perl interpreter around which can keep database connections around (see also Apache::DBI for the later).

    Another is to use some form of connection caching in your database if it supports it (e.g. pgpool for Postgres).

    The problem with these two approaches is that they may not be available if you're using some sort of vanilla lowest-common-denominator web hosting (although FastCGI does seem to be getting more support since Ruby on Rails can use it, so there may be hope).

      I already know that each mod_perl process would keep a connection to the database. But the problem is, my program is not connectting to a database. So, Apache::DBI can not help me. Keeping a local copy of data is unaccpetalbe because the data might change quickly. Maybe I should give more details of my task. My job is writing the configuration module for a embeded system that allows administrator to manage it remotely by a client program. The client and server talk to each other by a private protocol. Now my boss realized that CS is not as popular as BS. So he asked me to give him a BS resolution but I should not change codes on the server side. So my plan is like this: | Browser |<---http---->| Web Server |<--private protocol-->| Data |

        Sorry, I should have been more clear and said "connections like database connections (see Apache::DBI for an example how)". Even if you're not specifically caching a DBI handle the principle is the same and you could gain insight into implementation by looking at that module.

Re: How to cache a socket in cgi programming?
by Joost (Canon) on May 22, 2006 at 18:09 UTC
    As noted above, mod_perl and fastcgi seem to be the "logical" choice for this kind of thing. Both work by keeping the perl process alive for multiple requests. Note that you typically still need a connection for each apache child or fastcgi process, so if you really want only one connection you still need some kind of deamon to mediate, unless you're willing to throttle access to your program.

Re: How to cache a socket in cgi programming?
by TedPride (Priest) on May 22, 2006 at 17:57 UTC
    How often does the remote server update its records, and do you need real-time data? One option would be to just request the newest records every so often and store a copy of the database on your system, where it can be accessed locally.