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

A monk told me I should look into flocking my databases a few days past and since then I've tried to find documentation on it but didn't find any. I looked up flock on here, perldoc and cpan but it was only for basic filehandles, not databases. On the bottom of the pages they said something like "for more information, click 'this link'" and in each case the link wasn't retrievable. Not only this but I asked on www.tek-tips.com and a person there said they never heard of flocking a database and they don't think it's possible. Oh, and I looked in LP3 and the Cookbook and it didn't give me the information I was looking for either :(

Will any of you wise monks tell me if this is possible and/or show me documentation or an example on how to flock a database rather than just a file?

thanks

"Age is nothing more than an inaccurate number bestowed upon us at birth as just another means for others to judge and classify us"

sulfericacid

Replies are listed 'Best First'.
Re: flocking databases
by diotalevi (Canon) on Apr 14, 2003 at 03:19 UTC

    Prepended From conversation with sulfericacid and other posts you'll note that zie is already using a dbm style database - SDBM if I recall correctly. So I'm suggesting this as a work-alike (except better).

    When I first started out I was using DB_File::Lock (its documentation has pointers to Tie::DB_Lock and Tie::DB_LockFile). I've since gotten a clue and if I need to do this task then I use BerkeleyDB and use its concurrant locking system. This sort of task is something that is fully in the let-someone-else handle-it-if-you-can category.

    Added It occurred to me that an example would be a good idea.

    tie %hash, 'BerkeleyDB::Hash', -Filename => 'foo.db', -Flags => DB_CREATE | DB_INIT_CDB | DB_MPOOL;

    Easy eh? Note that BerkeleyDB's locking mechanism uses flock so NFS users (probably not sulfericacid) have to use one of the other modules mentioned above.

      Definately have a look at MLDBM::Sync if you are using SDBM. It will handle locking for you.

      From the docs:

      This module wraps around the MLDBM interface, by handling concurrent access to MLDBM databases with file locking, and flushes i/o explicity per lock/unlock.
(jeffa) Re: flocking databases
by jeffa (Bishop) on Apr 14, 2003 at 03:44 UTC
    You should read turnstep's File Locking tutorial. While databases are not mentioned, the concept is same - your database (which you didn't mention, but i happen to know is a DBM_File *) is a single file, which can be locked just like an 'ordinary' file (whatever that is ;)).

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    
      Thanks for your help everyone. I spent days looking for documentation on flocking databases but literally found nothing because I was told it was totally different than flocking just a basic filehandle. In any case the link jeffa tossed up was the solution to my problem.

      Who would have thought flocking a file could be so easy? All you do is add the line flock(FILEHANDLE, NUM) || die; :)

      "Age is nothing more than an inaccurate number bestowed upon us at birth as just another means for others to judge and classify us"

      sulfericacid

        And that is bad advice for this context. Working with dbm style files isn't like working with plain files. Originally the Perl Cookbook used such an approach and while I can't find an exact mention of the problem there's a quick overview at What about table and file locking?.

        So - if you follow only the advice in the link jeffa gave you then you'll be only part way there. In general, if you've got flock() in your code and you're working with a database then you did something wrong.

        jeffa didn't help you here at all. Bad jeffa.

Re: flocking databases
by Zaxo (Archbishop) on Apr 14, 2003 at 03:24 UTC

    What kind of database are you talking about, and what module(s) are you using?

    RDBMS servers do their own locking. You need flock for roll-your-own flat text databases. BerkeleyDB and DBM-type implementations vary, often having some internal call for locking.

    Check your db and module docs.

    After Compline,
    Zaxo