Berkeley DB / DB File Concurrence Problem 
Author Message
 Berkeley DB / DB File Concurrence Problem

Hi Folks,

I've been using Berkeley DB 1.86 with Perl DB File as the data engine
for
several CGI sites I've built.  Recently, trying to improve robustness,
I've
started using $db->fd and locking the DB Files with the file handle I
can
then generate.  Ideally, I'd like each of the data objects to be able to
create
a DB_File object that points to the correct db file and lock the file
only
when reading or writing.

The Problem:

when I have multiple objects pointing at the same db file, such as

$db1 = tie ( %HASH1, 'DB_file' , "$file_name1", O_RDWR, undef, $DB_HASH
)
$db2 = tie ( %HASH2, 'DB_file' , "$file_name1", O_RDWR, undef, $DB_HASH
)
$db3 = tie ( %HASH3, 'DB_file' , "$file_name1", O_RDWR, undef, $DB_HASH
)

if I print from any of the objects like so:
my $value1;
print $db1->fetch ( 'key', $value1 )

I get the correct return value from each.

However, if I write to the database like so:
$value2 = 'some value';
$db2->put ( 'key', $value2 );

and then perform a fetch of 'key' from each db object, $db2 fetches the
correct
vslue but $db1 and $db3 do not reflect the updates.

Is there some way to make this work?  I've written put methods that sync

the db object and the db file everytime a write is performed, but this
only
updates the db file to reflect the db object from which the sync was
called,
it does not update all other objects that point to the db file.

Any help would be greatly appreciated.

Thanks.

Michael Jones



Fri, 23 Feb 2001 03:00:00 GMT  
 Berkeley DB / DB File Concurrence Problem
Thanks Corey,

I ended up going this route:

package AgeGroup;

use ArchObject;


new {

     $self = $type->SUPER::new ( 'fields' = $fields );
     return $self;

Quote:
}

sub set_age_group { Instance Method Code... }

package ArchObject;

new {

     $type   = shift;

     $self = {};
     bless ( $self, $type );

     $self->{ 'fields' } = $PARAMS{ 'fields' };

     foreach ( keys %PARAMS ) {

         $field_name = $_;

         if ( $self->{ 'fields' }->{ "$_" } ) {

             $self->set ( "$field_name", $PARAMS{ $field_name } );

         }

     }

     return $self;

Quote:
}

sub set {

    $self = shift;
    $field_name = shift;
    $value = shift;

    if ( my $set = $self->{ 'fields' }->{ "$field_name" }->{ 'set' } ) {

        &$set ( $self, $value );

    } else {

        $self->{ "$field_name" } = $value;

    }

Quote:
}

I'm not sure if the above is the most elegant solution, but it works
in the inheriting object (AgeGroup) where the other methods I tried
did not.

Thanks for the input, MJ.

Quote:
> Date: Thu, 10 Sep 1998 09:31:33 -0400


> Subject: Reply to comp.lang.perl.misc posting

> Michael -
> You posted a question about passing a method by reference on Aug 27.
> I too had this question and found the answer below:

> http://www.perl.com/CPAN-local/doc/FAQs/FAQ/PerlFAQ.html#How_can_I_pa...

> What it said:
> -----------------------------------------------------------------

> Passing Methods
>     To pass an object method into a subroutine, you can do this:

>         call_a_lot(10, $some_obj, "methname")
>         sub call_a_lot {

>             for (my $i = 0; $i < $count; $i++) {
>                 $widget->$trick();
>             }
>         }

>     or you can use a closure to bundle up the object and its method
> call and arguments:


>         func($whatnot);
>         sub func {
>             my $code = shift;
>             &$code();
>         }

>     You could also investigate the can method in the UNIVERSAL class
> (part of the standard perl distribution).

> corey

_______________________________________________________________________

   "Brought low by an ending so cliche it could have oozed from the
       powerbook of the laziest Hollywood hack!" - Sideshow Bob


_______________________________________________________________________



Mon, 26 Feb 2001 03:00:00 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Berkeley DB / DB File Concurrence Problem

2. Berkeley DB, DB File on multiple platforms

3. Problem adding to Berkeley DB file using DB_File

4. Berkeley::DB, File exists problem on solaris 2.7

5. Berkeley DB file interface, bug report

6. problems building BerkleyDB.pm for Berkeley DB 3.0.55

7. problem with Perl & Berkeley DB

8. fixes for file size limits with DB_File/Berkeley DB

9. db-3.0.55 and Berkeley-0.10 installation problems

10. Perl 5.005_002 and berkeley db problems

11. Berkeley DB problems

12. Assistance request for Berkeley DB / DB_FIle problem

 

 
Powered by phpBB® Forum Software