[Dbix-class] Refreshing object's data from database && row-level locking

Oleg Pronin syber.rus at gmail.com
Sat May 12 02:14:35 GMT 2007


Greetings!

1. Reloading

I thought it would be great if DBIx::Class had this feature:

sub reload {
    my ($self, $attrs) =3D @_;

    my $fresh_me =3D $self->result_source->resultset->find({
        map { $_ =3D> $self->get_column($_) } $self->primary_columns,
    }, $attrs);
    $self->set_columns({$fresh_me->get_columns});
    1;
}

That is to be able for row object to reload its data from database.

If this feature is already in DBIC please ignore this part (i didn't find it
in docs).

2. Locking (PostgreSQL)

the optional $attrs parameter could be very usefull if DBIx::Class supports
row level locking for PostgreSQL.
(SELECT .... WHERE ... FOR UPDATE).

For example

#in txn_do

$user->reload({locking =3D> 'update'});
#go change critical user data

# txn_do end

You get the latest user's data in an isolated transaction and row is locked
in atomic operation!
No surprises from the rest of your application. No race conditions. Great
locking switcher.
(if the rest of application do the same when changing critical data for
user).

I've made a wrapper around ::Storage::DBI that supports locking but it is
very ugly.
The problem is that in '_select' there is no $sql string i could attach '
FOR UPDATE/SHARE' to.
But in '_execute' all non-standart attributes are missing and i can not get
' =3D> 'locking' value.

It would be good for this feature to be supported somewhere in DBIC
internals. (maybe ::Storage::DBI::Pg ?)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.scsys.co.uk/pipermail/dbix-class/attachments/20070512/934=
22f15/attachment.htm


More information about the Dbix-class mailing list