【问题标题】:Can we lock tables while executing a query in Zend-Db我们可以在 Zend-Db 中执行查询时锁定表吗
【发布时间】:2009-10-20 11:17:23
【问题描述】:

我说的是做这样的事情:

LOCK TABLE 页面写入;

SELECT * FROM page WHERE col = 'value';

插入页面(col1, col2) VALUES('val1', val2);

解锁表格;

【问题讨论】:

    标签: zend-framework zend-db


    【解决方案1】:

    我没有看到真正的 Zend DB 方法来锁定表,但也许只是这样做:

    //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:

    // 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();
    }
    

    我最近遇到了同样的问题,并且交易效果很好。绝对是要走的路。

    【讨论】:

    • 我想我必须这样做......感谢您的回复。
    【解决方案2】:

    Zend_Db 事务保证锁!请参阅此处了解如何进行正确的隔离交易。

    Database transactions in Zend Framework: Are they isolated?

    【讨论】:

      【解决方案3】:

      $this->_db->getConnection()->exec('LOCK TABLES page');

      【讨论】:

      • 如果您使用“mysqli”作为存储引擎,这将不起作用:“调用未定义的方法 mysqli::exec()”
      【解决方案4】:

      我认为您需要使用适配器,但我不确定锁定是否有效。

      $model->getAdapter()->query($sql, $bind);

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-05-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-10-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多