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

I'm just wondering if theres any way to lock a DB whilst updating it? My script accesses it makes the changes but more than one person could update at anyone time, which would cause problems if ones happens to read before the other has wriiten. Cheers Chris

Replies are listed 'Best First'.
Re: Locking a DB
by mirod (Canon) on Jun 05, 2001 at 13:15 UTC

    There is plenty of information about locking on this site, try using the search box at the top of the page. My favourite is KM's RE: File Locking, which works for plain files and for DB's which don't have locking capabilities, which I believe include GDB and the likes (and yes, I know if it does not support locking it should not claim to be a DB ;--)

Re: Locking a DB
by davorg (Chancellor) on Jun 05, 2001 at 12:56 UTC

    What DB are you using? Most serious databases will automatically put locks on data pages so that only one connection can update a page.

    --
    <http://www.dave.org.uk>

    "Perl makes the fun jobs fun
    and the boring jobs bearable" - me

      If your DB doesn't have locking capability, use a locking module. When you need to update a db; create a new one. Lock the new db. When the new db is finished writing to the disk, unlock it and replace the old db with the new db. That way, the old db is safe while the update is being made to the new db.
      I hope that helps,
       ERIC 
Re: Locking a DB
by fs (Monk) on Jun 05, 2001 at 17:09 UTC
    That depends on exactly what DB you're using. If it's a file based DB (such as the *DBM_File modules), there are methods you can use to lock the entire database - they're all documented in the man page.

    If you're using a RDBMS (such as MySQL, Oracle, or PostgreSQL) then you can get away with more fine grained locking, depending on the database. MySQL, for example, only supports table locking, while PostgreSQL supports individual row locking. Check out the docs on whichever server you're using if this is the case.