CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
How to Perfectly LOCK the database & make it safe ?

 



haider
New User

Feb 28, 2002, 8:37 PM

Post #1 of 8 (741 views)
How to Perfectly LOCK the database & make it safe ? Can't Post

Hello Friend,

I saw on perl websites that databases not using file locking are dangerous. So I saw some examples to solve this.
Example:
Open (Datafile,etc.);
flock Datafile, 2;


But I know that FlatDataFiles are not real good for database so I want to use the following DBF system:

Use Fctnl; Use AnyDBM_File;
$db = "$Config::path/usersdb";

tie(%acc, 'AnyDBM_File', $db , $flags, 0666) || &error("Cannot open database-- $db");
while ( ($key, $value) = each(%acc) ) {
do required functions;
}
untie(%acc);



but when I looked into the code there is no perfect file Locking available which is necessary for data security. Can you provide me with some neat & real useful code which can lock the database file ($db) untill the $db get completely released by one call ?



PLEASE HELP ME,
if u can't do much then please do atleast tell me:
1) How to perfectly lock datase from being used by other processes during read/write?
2) How to get the whole database in one variable?
3) How to replace the whole database with that vairiable at once ?
4) How much data this type of database can take safely & is it better than Flat File Databases or not ?
4) Can you suggest any tutorial/site URL which can teach me these things for FREE ?

THANKS A LOT... THANKS A LOT...


Any HELP will be appreciated ... thanks!




PerlKid
stranger

Mar 1, 2002, 1:55 AM

Post #2 of 8 (737 views)
Re: [haider] How to Perfectly LOCK the database & make it safe ? [In reply to] Can't Post

Could be wrong, but I believe that still a flat file database you're using...

1) the Fcntl handles the file locking stuff... I think if you look through the rest of that script you'll see that the $flags variable has the locking method used in it.
2) you really don't want to have your entire database loaded into memory unless you have a really good reason. mainly because 99% of the time you are only going to need a small percentage of records and it'd be a waste of time and memory.
3) refer to #2
4) 1MB or so. some people can get away with larger files but its a real performance loss and fidelity risk
5) hm... cpan.org, perl.com, activestate.com...

You should really consider an SQL database system such as MySQL, PostgreSQL, or Oracle. SQL allowes you to simplify your Perl code greatly, and it's much faster and easier to use.
--Philip
FuzzyLogic at PerlMad dot com


randor
User

Mar 1, 2002, 4:49 AM

Post #3 of 8 (733 views)
Re: [haider] How to Perfectly LOCK the database & make it safe ? [In reply to] Can't Post

i agree, BUT, if you do need to use a flat file system.. try this:



#add this at the top:
use Fcntl qw(:flock);

Then when you goto use a file, like so:

open(FH, ">/write/to/your/file") or die $!;
flock(FH, LOCK_EX);
print FH "here i am";
close FH;


or:

open(FH, "/read/from/your/file") or die $!;
flock(FH, LOCK_SH);
@info = <FH>;
close FH;

and if you want to do both:

open(FH, "+</read/from/your/file") or die $!;
flock(FH, LOCK_EX) or die $!; # you can run error tests also
@info = <FH>;
# do stuff with your info
seek(FH, 0, 0);
truncate(FH, 0); # be sure that you have removed old
print FH "here";

close FH;




hope this helps a little.
I Hope this helps...

perl programmers don't die.. they just start writing a new script.


yapp
User

Mar 1, 2002, 7:25 AM

Post #4 of 8 (730 views)
Re: [randor] How to Perfectly LOCK the database & make it safe ? [In reply to] Can't Post

You should re-open the file indeed. Otherwise, your lock is useless aswell.
Change the open in a sysopen to create the file when it didn't exist.

[perl]sysopen(FH, "/read/from/your/file", O_RDWR|O_CREAT) or die $!;
flock(FH, LOCK_EX) or die $!; # you can run error tests also
@info = <FH>
# do stuff with your info
seek(FH, 0, 0);
truncate(FH, 0); # be sure that you have removed old
print FH "here";
close FH; [/perl] I don't know how to make your code better, it's very nice (I'm suggesting this code aswell to all ppl having corrupted flatdatabase problems) Btw, do you really need to check the flock or die? win98 doesn't need locking, but dies here because flock is not implemented. If you remove the or die ONLY for that line, everything will work aswell.

Yet Another Perl Programmer

_________________________________
~~> [url=http://www.codingdomain.com]www.codingdomain.com <~~
More then 3500 X-Forum [url=http://www.codingdomain.com/cgi-perl/downloads/x-forum]Downloads! Cool


haider
New User

Mar 1, 2002, 10:31 AM

Post #5 of 8 (723 views)
Re: [PerlKid] How to Perfectly LOCK the database & make it safe ? [In reply to] Can't Post

Thanks a lot PerlKid (Philip) but I still need to know:

1) the Fcntl handles the file locking stuff... I think if you look through the rest of that script you'll see that the $flags variable has the locking method used in it.
A) >>> If I use fcntl with $flags = O_CREAT | O_RDWR; will it keep the database safely locked untill I enter a untie(%acc); command ?

2) you really don't want to have your entire database loaded into memory unless you have a really good reason. mainly because 99% of the time you are only going to need a small percentage of records and it'd be a waste of time and memory.
B) >>> Actually, I want to do something like this:

$flag= "read | write"; # not correct format just eg.

tie(%acc, 'AnyDBM_File', $db , $flags, 0666) || &error("Cannot open database-- $db");

while ( ($key, $value) = each(%acc) ) {


# add 1 to all values

$key = "$value + 1";


}

untie(%acc);

But don't know if it will write all the records back safely and correctly?

4) 1MB or so. some people can get away with larger files but its a real performance loss and fidelity risk
C) >>> what do you mean by 1MB ? is it the whole size of the database or only the key+value limits?

I want to add about 100-1200 records in that database & hope the limit is not for the whole database size, its only for one 'key+value' length ?... right

You should really consider an SQL database system such as MySQL, PostgreSQL, or Oracle. SQL allowes you to simplify your Perl code greatly, and it's much faster and easier to use.
D) >>> If the Any_DB system I want to use can take about 2000+ records easily then I will prefer to use it as its very easy & clean but if not I will prefer to use MySql as its available with most hosts but the problem is that I don't understand its coding tricks & I can't find any good free & easy tutorial anywhere ?

E) And Can u convert this code to mySql type code that can write the database while updating?:

open MySql Connection; # not correct format just Example.

while ( ($key, $value) = each(%acc) ) {


# add 1 to all values

$key = "$value + 1";


}

Close MySql connection; # not correct format just Example.

Thanks again for your Help, Dear Philip;


haider
New User

Mar 1, 2002, 2:31 PM

Post #6 of 8 (719 views)
Re: [randor] How to Perfectly LOCK the database & make it safe ? [In reply to] Can't Post

Hello & thanks Randor,

Sorry I replied little late but I was busy in finding out that if Any_DBM DB system support the method you used but unfortunately I failed to find out that this code which you mentioned is compatible with it.

Have you ever used tie etc. ?

or do you think that the code you mentioned can be applied.

I don't think that its a flat database file as when I look at it I see ascii characted codes. I am really confused. What's your opinion ?

Using MySql looks pretty complicated, Do you have any better ideas for a easy & reliable database system ?

Thanks for your support,

Haider Abbas


PerlKid
stranger

Mar 2, 2002, 1:07 AM

Post #7 of 8 (713 views)
Re: [haider] How to Perfectly LOCK the database & make it safe ? [In reply to] Can't Post


Quote
Using MySql looks pretty complicated, Do you have any better ideas for a easy & reliable database system ?


are you serious? SQL is easy to pick up on... Suppose in a normal flat file database, I want to do boolean searches. I have to actually process every single line of the file, split up every single word of the query and check that against every single field. That normaly requires quite a bit of excess coding. in MySQL, I can just write out what I want in plain English ("select * from foo where bar='quux' or baz='blech'"), with the assistance of code to create my query (using split and join for example), and the driver does all the dirty work for me. For a whole process (connecting, selecting, processing, disconnecting), can be as little as 5 lines of code (for a single returned record).
--Philip
FuzzyLogic at PerlMad dot com


randor
User

Mar 2, 2002, 6:38 AM

Post #8 of 8 (710 views)
Re: [haider] How to Perfectly LOCK the database & make it safe ? [In reply to] Can't Post

haider,

no, i do not use tie, so i am not to familiar with its style, as for MySql, there is a pretty good amount of tutorials on it here: http://www.mysql.com i recommend MySql up to a point. See Mysql works great to a certain load, but once it hits its limit, it looses speed rapidly. but for 95% of its uses it will work fine.
I Hope this helps...

perl programmers don't die.. they just start writing a new script.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives