File: README

package info (click to toggle)
libdb-file-lock-perl 0.05-2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 68 kB
  • ctags: 10
  • sloc: perl: 187; makefile: 37
file content (122 lines) | stat: -rw-r--r-- 4,315 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122

README

DB_File::Lock version 0.05
by David Harris <dharris@drh.net>


    -- WHAT DOES THIS MODULE DO?

This module provides a wrapper for the DB_File module, adding locking.

When you need locking, simply use this module in place of DB_File and
add an extra argument onto the tie command specifying if the file should
be locked for reading or writing.

The alternative is to write code like:

  open(LOCK, "<$db_filename.lock") or die;
  flock(LOCK, LOCK_SH) or die;
  tie(%db_hash, 'DB_File', $db_filename,  O_RDONLY, 0600, $DB_HASH) or die;
  ... then read the database ...
  untie(%db_hash);
  close(LOCK);

This module lets you write

  tie(%db_hash, 'DB_File', $db_filename,  O_RDONLY, 0600, $DB_HASH, 'read') or die;
  ... then read the database ...
  untie(%db_hash);

This is better for two reasons:

(1) Less cumbersome to write.

(2) A fatal exception in the code working on the database which does
not lead to process termination will probably not close the lockfile
and therefore cause a dropped lock.


   -- WHY MIGHT ONE NEED A LOCKING WRAPPER MODULE?

There can be many reasons:

(1) You have an application which is going to modify a DB_File database
and it's possible that multiple instances of the application will be run
at the same time. If you don't do locking then you can easily corrupt
the database file.

  (a) You get tired of writing your locking code manually and want it
  handled for you by a nice module. You know, it's kind of cumbersome
  writing all that stuff out. :-)

  (b) You are running in an environment (such as mod_perl or code which
  may be inside an eval { .. } error trapper) where your code may be
  interrupted by a fatal error and not immediately lead to process
  termination.  This can cause dropped locks.

  (c) You are using $db->fd to lock the database _AFTER_ you have
  tied the database. This fatally flawed and does lead to database
  corruption. (This method was promoted in the old DB_File docs and in the
  Camel book.) See the ``Why you shouldn't use "fd" to lock a database''
  section in the new DB_File docs.

  (d) You have thought of some reason I have not. :-)

(2) You are using mod_perl. This ends the discussion right there --
it is _imperative_ to use a DB_File locking wrapper with mod_perl. See
http://perl.apache.org/guide/dbm.html#mod_perl_and_dbm for more info.


    -- IS THIS THE CORRECT LOCKING WRAPPER FOR MY APPLICATION?

There are three locking wrappers for DB_File in CPAN right now. Each one
implements locking differently and has different goals in mind. It is
therefore worth knowing the difference, so that you can pick the right
one for your application.

Here are the three locking wrappers:

Tie::DB_Lock -- DB_File wrapper which creates copies of the database file
for read access, so that you have kind of a multiversioning concurrent
read system. However, updates are still serial. Use for databases where
reads may be lengthy and consistency problems may occur.

Tie::DB_LockFile -- DB_File wrapper that has the ability to lock and
unlock the database while it is being used. Avoids the tie-before-flock
problem by simply re-tie-ing the database when you get or drop a
lock. Because of the flexibility in dropping and re-acquiring the lock
in the middle of a session, this can be massaged into a system that will
work with long updates and/or reads if the application follows the hints
in the POD documentation.

DB_File::Lock (this module) -- extremely lightweight DB_File wrapper
that simply flocks a lockfile before tie-ing the database and drops the
lock after the untie.  Allows one to use the same lockfile for multiple
databases to avoid deadlock problems, if desired. Use for databases where
updates are reads are quick and simple flock locking semantics are enough.

(This text duplicated in the POD documentation, by the way.)


    -- INSTALL

To install the module, do the usual:

   perl Makefile.PL
   make
   make test
   make install


The test files in this module require a valid fork command.  If your
platform does not have fork(2), then please accept failures on the
test phase of this module.


___________________

Copyright (c) 1999-2000 David R. Harris. All rights reserved. 
This program is free software; you can redistribute it and/or modify it 
under the same terms as Perl itself.