Gossamer Forum
Home : Products : Links 2.0 : Discussions :

When to use FLOCK?

Quote Reply
When to use FLOCK?
I would need advice when to use flock. I see subs in Links2.0 where there flock is used,
in other subs there isn't flock used when a database is opened.
I quoted 3 examples below (2 from add.cgi, 1 from db_utils.pl).
Question:
When to use flock?
- Always?
- Only when writing to db?

If always is the answer there is a missing flock in example 1. in add.cgi.


-- add.cgi --
example 1.
# Set date variable to today's date.
$in{$db_cols[$db_modified]} = &get_date;

open (ID, "<$db_links_id_file_name") or &cgierr("error in process_form. unable to open id file: $db_links_id_file_name. Reason: $!");
$in{$db_key} = <ID> + 1; # Get next ID number
close ID;
.
.
example 2.
# Update the counter.
open (ID, ">$db_links_id_file_name") or &cgierr("error in get_defaults. unable to open id file: $db_links_id_file_name. Reason: $!");
flock(ID, 2) unless (!$db_use_flock);
print ID $in{$db_key}; # update counter.
close ID; # automatically removes file lock

--db_utils.pl--
example 3.
sub get_defaults {
# --------------------------------------------------------
# Returns a hash of the defaults used for a new record.

my %default;

foreach $field (keys %db_defaults) {
$db_defaults{$field} =~ /^\s*$/ and ($default{$field} = $in{$field}) and next;
(ref $db_defaults{$field} eq 'CODE') ?
($default{$field} = &{$db_defaults{$field}}) : ($default{$field} = $db_defaults{$field});
}
if ($db_key_track) {
open (ID, "<$db_id_file_name") or &cgierr("error in get_defaults. unable to open id file: $db_id_file_name.\nReason: $!");
if ($db_use_flock) { flock(ID, 1); }
$default{$db_key} = <ID> + 1; # Get next ID number
close ID;
}
return %default;
}

Thanks,
Webmaster33
Quote Reply
Re: When to use FLOCK? In reply to
Flocking should be used primarily for writing or updating database files.

Reading from files like the database id file does not really call for using flock.

But if you are concerned about it, add the flock codes to the reading database code lines in your scripts.

Also, try looking at www.perl.com for answers relating to Perl codes.

Regards,

------------------
Eliot Lee
Anthro TECH,L.L.C
www.anthrotech.com
Be sure to visit the Resource Center for FAQ's, Modifications and Extra Goodies!!
----------------------







[This message has been edited by Eliot (edited January 22, 2000).]
Quote Reply
Re: When to use FLOCK? In reply to
Thanks, Eliot!
I was just curious if I need to worry, if I don't use flock at db read.
I supposed, that is necessary to use when writing to db, but unnecessary if I just read the db.
Your answer justified my thoughts.

Regards,
Webmaster33
Quote Reply
Re: When to use FLOCK? In reply to
Good.

Regards,

------------------
Eliot Lee
Anthro TECH,L.L.C
http://www.anthrotech.com
Be sure to visit the Resource Center for FAQ's, Modifications and Extra Goodies!!
----------------------