Exclusive Locks with RDBO?

4 views
Skip to first unread message

Timmy Chan

unread,
Sep 15, 2009, 6:09:33 PM9/15/09
to rose-db...@googlegroups.com
Hey Rose,

I'm currently doing this for obtaining locks:

sub get_lock {
    my ( $class, %params ) = @_;

    my $id = $params{'id'};
    my $db  = $params{'db'};
    my $ret = Objects::Manager->get_objects_from_sql(
        args => [$id],
        sql  => 'select * from table where id = ? for update',
        db   => $db,
    );

    return $$ret[0];
}

Is there an easier/cleaner way to do this?

Thanks!

John Siracusa

unread,
Sep 15, 2009, 6:51:55 PM9/15/09
to rose-db...@googlegroups.com

The Manager (and the rest of RDBO, for that matter) has no way to
"SELECT ... FOR UPDATE." How about something like this instead?

sub get_lock {
my ( $class, %params ) = @_;
my $id = $params{'id'};
my $db = $params{'db'};

$db->dbh->do('select * from table where id = ? for update', undef, $id);
}

-John

Graham Barr

unread,
Sep 15, 2009, 8:39:21 PM9/15/09
to rose-db...@googlegroups.com

I use a combination of get_objects_sql and get_objects_from_sql so my
method can do any search other than just by id

something akin to,

sub get_objects_locked {
my $class = shift;

my ($sql, $args) = $class->get_objects_sql(@_);
return $class->get_objects_from_sql(
@_,
args => $args,
sql => $sql . " FOR UPDATE",
);
}

Graham.

Timmy Chan

unread,
Oct 16, 2009, 3:21:39 PM10/16/09
to rose-db...@googlegroups.com
I tried doing a hybrid:

sub get_lock {
    my ( $self, $db ) = @_;

    my $obj = Object::Manager->get_lock( { db => $db, id => $self->id, } );
    $self = $obj;
}

Later I tried to take that object and save, but it's running into lock issues, so the db does not appear to be the same.. is there a better way to refresh data in this way?

Timmy Chan

unread,
Oct 16, 2009, 3:32:43 PM10/16/09
to rose-db...@googlegroups.com
Changing the code to:

sub get_lock {
   my ( $self, $db ) = @_;

   my $obj = Object::Manager->get_lock( { db => $db, id => $self->id, } );
   $self = $obj;
   $self->db( $db );
}

works, but I'm not certain I'm not fooling myself into thinking it works 100% - the goal is to get a lock, and refresh the object from the database.  Thanks.

John Siracusa

unread,
Oct 16, 2009, 3:58:59 PM10/16/09
to rose-db...@googlegroups.com
On Fri, Oct 16, 2009 at 3:32 PM, Timmy Chan <timmy....@gmail.com> wrote:
> Changing the code to:
> sub get_lock {
>    my ( $self, $db ) = @_;
>    my $obj = Object::Manager->get_lock( { db => $db, id => $self->id, } );
>    $self = $obj;
>    $self->db( $db );
> }
> works, but I'm not certain I'm not fooling myself into thinking it works
> 100%

You just have to make sure the object is using the same database
connection as the one you got the lock with. The code above looks
like it's doing that, so it should work fine.

-John

Timmy Chan

unread,
Oct 16, 2009, 4:02:55 PM10/16/09
to rose-db...@googlegroups.com
This is the Object::Manager->get_lock method:

sub get_lock {
    my ( $class, $params ) = @_;

    my $id = $params->{'id'};
    my $db  = $params->{'db'};

    my $ret = Flip::Game::Manager->get_objects_from_sql(
        args => [ $id, ],
        sql  => 'select * from '
          . Object->meta->table
          . ' where id = ? for update',
        db => $db,
    );

    return $$ret[0];
}

Am I missing something?  Shouldn't this already have the same database connection handle?

Thanks.

John Siracusa

unread,
Oct 16, 2009, 4:19:48 PM10/16/09
to rose-db...@googlegroups.com
On Fri, Oct 16, 2009 at 4:02 PM, Timmy Chan <timmy....@gmail.com> wrote:
> Shouldn't this already have the same database connection handle?

Yeah, it looks like it should. I think your issue might be the "$self
= $obj" part. If you're trying to actually replace the calling
object with a new one, you'd have to do something like "$_[0] = $obj"

-John

Timmy Chan

unread,
Oct 16, 2009, 4:22:25 PM10/16/09
to rose-db...@googlegroups.com
That makes sense.  That was the suspect part of the code that I wasn't so sure about, but now that you explain it that way, I know which way to debug this.  Thanks!
Reply all
Reply to author
Forward
0 new messages