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

So I'm writing this new site, using HTML templates, blah-dee-blah, and one thing I'm considering using is Apache::Sessions with a MySQL database. This would be done in lieu of passing hidden variables from page to page; they would be stored in the session and read at will, an obvious improvement to security. However, all of my Java coherts sic? cringe at the word session... seems in Java the sessions are stored in memory ALL OF THE TIME. Is this true with Apache::Sessions, or is my overhead only contained in the fact that I have an extra connection to the database per pageload?
  • Comment on Apache::Sessions -- Good, Bad or just Ugly?

Replies are listed 'Best First'.
RE: Apache::Sessions -- Good, Bad or just Ugly?
by lachoy (Parson) on Oct 24, 2000 at 00:57 UTC

    Apache::Session is somewhat misnamed -- you don't need to use Apache/mod_perl to use the module. It stores the information in whatever storage mechanism you think is appropriate (filesystem, database, DBM), and not in memory -- although you can use a memory storage (like IPC::Shareable or something) if you're feeling lucky and industrious.

    In your case, it would be MySQL. If you're using CGI, then you're going to need to open up a database handle for each request. Various other schemes exist based on whatever sort of server process you're using. The module has some decent documentation, check it out: Apache::Session.

Re: Apache::Sessions -- Good
by lhoward (Vicar) on Oct 24, 2000 at 01:33 UTC
    There are a whole slew of Apache::Session modules to let you do the storage of sessions any way you want: Just pick the one best suited to your needs. IPC or MemoryStore is probably fastest, but probably won't work if your session maintenance needs extend across servers. DBI is probably the most rock-solid (sessions would presist if your server crashed), but also slower because it has to go to the DB to get and store the session stuff. Daemon is probably fast, and works well if you need to span hosts, but (like IPC) you would loose your sessions if your server crashed.
Re: Apache::Sessions -- Good
by gregorovius (Friar) on Oct 24, 2000 at 01:05 UTC
    Actually, database connections would happen only on the first pageload per apache server spawned. Apache::Session sits on top of DBI, and database connections are cached (as any global variables are) if you use mod_perl. If you use Session on MySql your sessions are still going to be very fast, and any performance difference from memory-only sessions would in most cases be offset by the object persistance advantage you get with Apache::Session (otherwise you'd usually need to fill in the session data yourself, with the accompanying db quieries).

    I recommend you look at the mod_perl documentation for more on how DB connections are cached.

      Well, this opens up a whole other discussion about DB handle caching.. I've got memory (512MB) to burn, so can i use mod_perl to cache a whole load of handles (say 10 per httpd?) and use them interchangeably between whichever sessions? This would obviously improve a bit on performance.
        with Apache::DBI each httpd process automatically opens up just as many handles as it needs. If each of your .cgi scripts only does one DBI->Connect then only one handle is needed. Extra handles would only impose extra overhead. The magic that Apache:::DBI gives you is that once it has a handle open, it doesn't close it so your next call to a .cgi running on that httpd process will re-use that earlier handle.
Re: Apache::Sessions -- Good
by ismail (Acolyte) on Oct 24, 2000 at 01:02 UTC
    I wrote this post but somehow I got logged out. :/ Thanks for the info, and that confirms what I thought... I don't know what's up with those Java guys, and why they can't do it like this, but with Java guys who really knows anyways.