首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我们可以在Zend-Db中执行查询时锁定表吗

我们可以在Zend-Db中执行查询时锁定表吗
EN

Stack Overflow用户
提问于 2009-10-20 11:17:24
回答 4查看 6.3K关注 0票数 1

我说的是这样做:

锁定表页写入;

SELECT *从页面WHERE col = 'value';

插入页面(col1,col2)值(‘val1’,val2);

解锁表;

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2009-10-22 12:46:41

我没有看到一个实际的Zend DB方法来锁定表,但也许可以这样做:

代码语言:javascript
复制
//Lock Table
$sql = "LOCK TABLE page WRITE";
$db->fetchRow($sql);

//Get your data
$sql = "SELECT * FROM page WHERE col='value'";
$result = $db->fetchAll($sql);

//Make the insert
$data = array( 'col1' => 'val1', 'col2' => 'val2' );
$db->insert('page', $data);

//Unlock tables
$sql = "UNLOCK TABLES";
$db->fetchRow($sql);

可能不是最好的解决方案,而且它还没有经过测试。但这可能对你有用。

更新:我为你找到了一个更好的解决方案。使用transactions

代码语言:javascript
复制
// Start a transaction explicitly.
$db->beginTransaction();

try {
    //Get your data
    $sql = "SELECT * FROM page WHERE col='value'";
    $result = $db->fetchAll($sql);
    //Make the insert
    $data = array( 'col1' => 'val1', 'col2' => 'val2' );
    $db->insert('page', $data);

    // If all succeed, commit the transaction and all changes
    // are committed at once.
    $db->commit();

} catch (Exception $e) {
    // If any of the queries failed and threw an exception,
    // we want to roll back the whole transaction, reversing
    // changes made in the transaction, even those that succeeded.
    // Thus all changes are committed together, or none are.
    $db->rollBack();
    echo $e->getMessage();
}

我最近遇到了同样的问题,交易运行得很好。这绝对是我们要走的路。

票数 10
EN

Stack Overflow用户

发布于 2012-12-25 18:21:41

Zend_Db transactions do not guarantee!有关如何创建适当的独立事务的信息,请参阅此内容。

Database transactions in Zend Framework: Are they isolated?

票数 2
EN

Stack Overflow用户

发布于 2010-11-12 17:15:42

$this->_db->getConnection()->exec(‘锁表页面’);

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1593895

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档