Zend框架中的数据库事务:它们是孤立的吗? [英] Database transactions in Zend Framework: Are they isolated?
问题描述
使用Zend Framework,我需要(1)从MySQL数据库读取记录,并且(2)立即写回到该记录以指示它已被读取。我不希望其他进程或查询能够在步骤(1)和(2)之间读取或写入相同的记录。
Using Zend Framework, I need to (1) read a record from a MySQL database, and (2) immediately write back to that record to indicate that it has been read. I don't want other processes or queries to be able to read from or write to the same record in between steps (1) and (2).
使用事务处理这些步骤。如果我使用以下方法,将满足我的要求?
I was considering using a transaction for these steps. If I use the following methods, will that fulfil my requirements?
Zend_Db_Adapter_Abstract::beginTransaction()
Zend_Db_Adapter_Abstract::commit()
Zend_Db_Adapter_Abstract::rollBack()
推荐答案
p>预设您正在使用 InnoDB引擎查看您是否将在以下地址发出交易:
Presupposing you are using the InnoDB engine for tables that you will issue transactions on:
如果要求您首先需要读取行并且独占锁定,在您更新它之前,您应该发出< a href =http://dev.mysql.com/doc/refman/5.5/en/innodb-locking-reads.html =noreferrer> SELECT ... FOR UPDATE
查询。像:
If the requirement is that you first need to read the row and exclusively lock it, before you are going to update it, you should issue a SELECT ... FOR UPDATE
query. Something like:
$db->beginTransaction();
try
{
$select = $db->select()
->forUpdate() // <-- here's the magic
->from(
array( 'a' => 'yourTable' ),
array( 'your', 'column', 'names' )
)
->where( 'someColumn = ?', $whatever );
$result = $this->_adapter->fetchRow( $select );
/*
alter data in $result
and update if necessary:
*/
$db->update( 'yourTable', $result, array( 'someColumn = ?' => $whatever ) );
$db->commit();
}
catch( Exception $e )
{
$db->rollBack();
}
或只需发出'raw' SELECT ...当然还有
$ UP
和 UPDATE
SQL语句。
Or simply issue 'raw' SELECT ... FOR UPDATE
and UPDATE
SQL statements on $db
of course.
这篇关于Zend框架中的数据库事务:它们是孤立的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!