| 1 |
|
| 2 |
README |
| 3 |
|
| 4 |
DB_File::Lock version 0.05 |
| 5 |
by David Harris <[email protected]> |
| 6 |
|
| 7 |
|
| 8 |
-- WHAT DOES THIS MODULE DO? |
| 9 |
|
| 10 |
This module provides a wrapper for the DB_File module, adding locking. |
| 11 |
|
| 12 |
When you need locking, simply use this module in place of DB_File and |
| 13 |
add an extra argument onto the tie command specifying if the file should |
| 14 |
be locked for reading or writing. |
| 15 |
|
| 16 |
The alternative is to write code like: |
| 17 |
|
| 18 |
open(LOCK, "<$db_filename.lock") or die; |
| 19 |
flock(LOCK, LOCK_SH) or die; |
| 20 |
tie(%db_hash, 'DB_File', $db_filename, O_RDONLY, 0600, $DB_HASH) or die; |
| 21 |
... then read the database ... |
| 22 |
untie(%db_hash); |
| 23 |
close(LOCK); |
| 24 |
|
| 25 |
This module lets you write |
| 26 |
|
| 27 |
tie(%db_hash, 'DB_File', $db_filename, O_RDONLY, 0600, $DB_HASH, 'read') or die; |
| 28 |
... then read the database ... |
| 29 |
untie(%db_hash); |
| 30 |
|
| 31 |
This is better for two reasons: |
| 32 |
|
| 33 |
(1) Less cumbersome to write. |
| 34 |
|
| 35 |
(2) A fatal exception in the code working on the database which does |
| 36 |
not lead to process termination will probably not close the lockfile |
| 37 |
and therefore cause a dropped lock. |
| 38 |
|
| 39 |
|
| 40 |
-- WHY MIGHT ONE NEED A LOCKING WRAPPER MODULE? |
| 41 |
|
| 42 |
There can be many reasons: |
| 43 |
|
| 44 |
(1) You have an application which is going to modify a DB_File database |
| 45 |
and it's possible that multiple instances of the application will be run |
| 46 |
at the same time. If you don't do locking then you can easily corrupt |
| 47 |
the database file. |
| 48 |
|
| 49 |
(a) You get tired of writing your locking code manually and want it |
| 50 |
handled for you by a nice module. You know, it's kind of cumbersome |
| 51 |
writing all that stuff out. :-) |
| 52 |
|
| 53 |
(b) You are running in an environment (such as mod_perl or code which |
| 54 |
may be inside an eval { .. } error trapper) where your code may be |
| 55 |
interrupted by a fatal error and not immediately lead to process |
| 56 |
termination. This can cause dropped locks. |
| 57 |
|
| 58 |
(c) You are using $db->fd to lock the database _AFTER_ you have |
| 59 |
tied the database. This fatally flawed and does lead to database |
| 60 |
corruption. (This method was promoted in the old DB_File docs and in the |
| 61 |
Camel book.) See the ``Why you shouldn't use "fd" to lock a database'' |
| 62 |
section in the new DB_File docs. |
| 63 |
|
| 64 |
(d) You have thought of some reason I have not. :-) |
| 65 |
|
| 66 |
(2) You are using mod_perl. This ends the discussion right there -- |
| 67 |
it is _imperative_ to use a DB_File locking wrapper with mod_perl. See |
| 68 |
http://perl.apache.org/guide/dbm.html#mod_perl_and_dbm for more info. |
| 69 |
|
| 70 |
|
| 71 |
-- IS THIS THE CORRECT LOCKING WRAPPER FOR MY APPLICATION? |
| 72 |
|
| 73 |
There are three locking wrappers for DB_File in CPAN right now. Each one |
| 74 |
implements locking differently and has different goals in mind. It is |
| 75 |
therefore worth knowing the difference, so that you can pick the right |
| 76 |
one for your application. |
| 77 |
|
| 78 |
Here are the three locking wrappers: |
| 79 |
|
| 80 |
Tie::DB_Lock -- DB_File wrapper which creates copies of the database file |
| 81 |
for read access, so that you have kind of a multiversioning concurrent |
| 82 |
read system. However, updates are still serial. Use for databases where |
| 83 |
reads may be lengthy and consistency problems may occur. |
| 84 |
|
| 85 |
Tie::DB_LockFile -- DB_File wrapper that has the ability to lock and |
| 86 |
unlock the database while it is being used. Avoids the tie-before-flock |
| 87 |
problem by simply re-tie-ing the database when you get or drop a |
| 88 |
lock. Because of the flexibility in dropping and re-acquiring the lock |
| 89 |
in the middle of a session, this can be massaged into a system that will |
| 90 |
work with long updates and/or reads if the application follows the hints |
| 91 |
in the POD documentation. |
| 92 |
|
| 93 |
DB_File::Lock (this module) -- extremely lightweight DB_File wrapper |
| 94 |
that simply flocks a lockfile before tie-ing the database and drops the |
| 95 |
lock after the untie. Allows one to use the same lockfile for multiple |
| 96 |
databases to avoid deadlock problems, if desired. Use for databases where |
| 97 |
updates are reads are quick and simple flock locking semantics are enough. |
| 98 |
|
| 99 |
(This text duplicated in the POD documentation, by the way.) |
| 100 |
|
| 101 |
|
| 102 |
-- INSTALL |
| 103 |
|
| 104 |
To install the module, do the usual: |
| 105 |
|
| 106 |
perl Makefile.PL |
| 107 |
make |
| 108 |
make test |
| 109 |
make install |
| 110 |
|
| 111 |
|
| 112 |
The test files in this module require a valid fork command. If your |
| 113 |
platform does not have fork(2), then please accept failures on the |
| 114 |
test phase of this module. |
| 115 |
|
| 116 |
|
| 117 |
___________________ |
| 118 |
|
| 119 |
Copyright (c) 1999-2000 David R. Harris. All rights reserved. |
| 120 |
This program is free software; you can redistribute it and/or modify it |
| 121 |
under the same terms as Perl itself. |
| 122 |
|