Read locks

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Read locks

Florian Hoenig
Hey,

I haven't been using Zend_Db_Table down to it's full feature set yet, since
it's still changing a lot, so this might be a newbie question:

I have concurrent access to a table and one thread takes out a row-set and
iterates over it, doing some time-consuming operations and then eventually
writes a row with ->save().

The other thread is doing the exact same thing just cascaded in time.

What's the best-practice here for read-locking this selected rowset so no
read-after-read conflict occurs ?

Many thanks,

Florian

Reply | Threaded
Open this post in threaded view
|

RE: Read locks

Bill Karwin from Zend
I'd recommend opening a transaction before the first read, and then
commit after the save().  It's not usually a good idea to lock anything
in a database if you don't have to.  But with transaction isolation, you
should be able to get repeatable read consistency.

See
http://framework.zend.com/manual/en/zend.db.html#zend.db.adapter.transac
tions

However, if you use a non-transactional storage technology such as
MySQL's MyISAM tables, it may be the only option to use table locking to
achieve read consistency.  Zend_Db has no explicit feature for this, so
you may have to do it by accessing the internal connection object:

  $db->getConnection()->exec('LOCK TABLES mytable READ');
  ...do your work against mytable...
  $db->getConnection()->exec('UNLOCK TABLES');

Regards,
Bill Karwin

> -----Original Message-----
> From: Florian Hoenig [mailto:[hidden email]]
> Sent: Tuesday, April 03, 2007 1:47 AM
> To: [hidden email]
> Subject: [fw-db] Read locks
>
> Hey,
>
> I haven't been using Zend_Db_Table down to it's full feature set yet,
> since
> it's still changing a lot, so this might be a newbie question:
>
> I have concurrent access to a table and one thread takes out a row-set
and
> iterates over it, doing some time-consuming operations and then
eventually
> writes a row with ->save().
>
> The other thread is doing the exact same thing just cascaded in time.
>
> What's the best-practice here for read-locking this selected rowset so
no
> read-after-read conflict occurs ?
>
> Many thanks,
>
> Florian