Re: Stale data in DB_File?
From: Bob Walton (invalid-email_at_rochester.rr.com)
Date: 10/22/03
- Next message: Tad McClellan: "Re: First Perl/CGI need critique on current code before further developement"
- Previous message: Eric J. Roode: "Re: First Perl/CGI need critique on current code before further developement"
- In reply to: Mark: "Stale data in DB_File?"
- Next in thread: Mark: "Re: Stale data in DB_File?"
- Reply: Mark: "Re: Stale data in DB_File?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Wed, 22 Oct 2003 03:14:19 GMT
Mark wrote:
> Hello,
>
> I was wondering whether someone could explain to me a weird problem I am
> having with DB_File (Perl 5.8.0).
>
> Okay. A daemon uses this:
>
> use DB_File;
> use Fcntl;
>
> tie %drac, DB_File, '/usr/local/etc/dracd.db', O_RDONLY, 0440, $DB_BTREE;
>
> Within the daemon I then check like this:
>
> if ($drac{'192.168.0.1'}) {
> ...
>
> Works fine. Except, that when I delete an entry (with an external program,
> also using DB_File), like so,
>
> delete $drac{'192.168.0.1'};
>
> Then, within the daemon the entry still exists! And vice versa: if I add an
> entry to the dabase, then, within the daemon, this addition is not seen.
> Within the daemon I do this, for test purposes:
>
> foreach (sort keys %drac) {
> write_syslog ("$_, $drac{$_}");
> }
>
> The only way I was able to avoid stale data, is to evoke the tie call, each
> time, within the subroutine that tests $drac{'entry'}. Surely I am not
> supposed to do that, right? I thought you had to do the tie only once, after
> which %drac stays tied throughout the process.
>
> The drac database, of course, is updated from the outside (like my test
> programs did).
>
> Please, tell me what I'm missing and/or doing wrong.
>
> Thanks!
>
> - Mark
>
>
>
When you are reading and writing DBM-type files (or any other type of
file, for that matter) from more than one process or thread
simultaneously, you need to treat all the operations between the tie and
the untie as atomic from a file locking perspective. See:
perldoc -f flock
perldoc -q lock
perldoc DB_File
So the basic operations are: In a given process, establish a lock, then
tie the DBM-type file, do whatever to it (read, write), untie the
DBM-type file, and remove the lock. For best results, use a separate
empty file (or a file whose contents don't matter) for locking purposes
and only touch the locked DBM-type file when your process has a lock on
the lock file. Be sure to very carefully read all your system's
documentation about file locking -- that topic probably varies more than
any other amongst the various operating systems. Note that some OS's
require the lock file be opened for write to establish an exclusive lock
and that it be opened for read to establish a shared lock. And some
infamous wanna-be OS's don't support file locking at all, in which case
you cannot safely use file I/O in multiple processes simultaneously (in
that case, get an OS). Be sure and test your locking scheme to be
certain it is functional.
Note also that you risk a lot more than just loss of data synchronicity
("stale data") if you don't lock -- you can easily corrupt the file.
-- Bob Walton Email: http://bwalton.com/cgi-bin/emailbob.pl
- Next message: Tad McClellan: "Re: First Perl/CGI need critique on current code before further developement"
- Previous message: Eric J. Roode: "Re: First Perl/CGI need critique on current code before further developement"
- In reply to: Mark: "Stale data in DB_File?"
- Next in thread: Mark: "Re: Stale data in DB_File?"
- Reply: Mark: "Re: Stale data in DB_File?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|