2

我们正在使用 zend_db_table 并且我们遇到了一些问题,因为 Zend Framework 抱怨两个事务处于活动状态:

[message:protected] => There is already an active transaction
[string:Exception:private] => 
[code:protected] => 0
[file:protected] => /var/www/vhosts/test.local/private/library/Zend/Db/Adapter/Pdo/Abstract.php
[line:protected] => 305
[trace:Exception:private] => Array

这是控制器中的代码:

     public function convertAction()
{
    $this->setNoRender();

    // If the quote is a copy of a previous one, fetch all the datas
    $quoteId = Zend_Filter::filterStatic($this->getRequest()->getParam('qte_id'), 'int');
    $quoteTable = new Model_QuotesTable();
    $quoteRow = $quoteTable->findById($quoteId);
    if (count($quoteRow)) {
        $clonedId = $quoteRow->convertToJob();
        $this->flashMessageRedirect('Quotation successfully converted', '/jobs/edit/job_id/' . $clonedId);
    } else {
        $this->flashMessageRedirect('Unable to find the quote to be converted', '/quotes');
    }
}

这是在扩展 zend_db_table_abstract 的 QuotesTableRow 中调用此函数:

    public function convertToJob()
{
    $db = $this->_getTable()->getAdapter();
    $db->beginTransaction();

    $jobsTable = new Model_JobsTable();

    try {

        /*
         * Update the status of the old row to match the $status passed into this function
         */
        $this->qte_status = "Accepted";
        $this->save();

        /*
         * Create new row with the same details as above
         */

        $newRow = $jobsTable->createRow();

        $newRow->job_title = $this->qte_title;
        $newRow->job_cus_id = $this->qte_cus_id;
        $newRow->job_enq_id = $this->qte_enq_id;
        $newRow->job_qte_id = $this->qte_id;
        $newRow->job_title = $this->qte_title;
        $newRow->job_description = $this->qte_description;
        $newRow->job_work_location_id = $this->qte_work_location_id;
        $newRow->job_work_category_id = $this->qte_work_category_id;
        $newRow->job_work_type_id = $this->qte_work_type_id;
        $newRow->job_cus_code = $this->qte_cus_code;
        $newRow->job_cus_name = $this->qte_cus_name;
        $newRow->job_wt_ref_code = $this->qte_wt_ref_code;
        $newRow->job_wt_description = $this->qte_wt_description;
        $newRow->job_wl_code = $this->qte_wl_code;
        $newRow->job_wl_description = $this->qte_wl_description;
        $newRow->job_wc_ref_code = $this->qte_wc_ref_code;
        $newRow->job_wc_description = $this->qte_wc_description;
        $newRow->job_qte_title = $this->qte_title;
        $newRow->job_datetime_created = date('Y-m-d H:i:s');

        $newRowId = $newRow->save();

        $db->commit();
        return $newRowId;
    } 
    catch (Exception $e) { 
        $db->rollback(); 

        echo('<pre>');
        print_r($e);
        echo('</pre>');
        exit();

        throw new Exception($e->getMessage());
        return false;
    }
}

此外,它似乎与我们不在的模型有关,因为如果我们使用与 Model_JobsTable() 相关的 save() 函数评论该行,脚本正在运行,而当我们评论另一个保存时它返回相同的错误()。

4

2 回答 2

6

这个错误是从 MySQL 返回的,ZF 只是告诉你错误信息。

您是否在同一个请求中启动两个事务?这可以解释为什么您收到此错误消息,或者您可能有一个处于事务中间的中止连接并且它没有回滚或自动提交。

每个数据库连接只应启动一个事务。如果您需要两个模型在单个请求中具有活动事务,那么您需要获得 2 个单独的数据库连接。

请参阅Bill Karwin 关于这个问题的这个(很好的)答案

您可以运行查询SHOW ENGINE InnoDB STATUS;以获取活动事务的列表。如果您有一个打开的并且没有来自 PHP/ZF 的活动事务,那么请尝试关闭该事务,否则您将不得不查看您的代码并查看两个事务是如何在同一个请求中启动的。

于 2012-08-02T17:50:01.217 回答
3

感谢您的回答,我们找到了解决方案。问题是,我们使用了这个save()函数两次;用更改第save()一个insert(),解决了问题:

public function convertToJob()
{

    $db = $this->_getTable()->getAdapter();
    $db->beginTransaction();

    $jobsTable = new Model_JobsTable();

    try {

        /*
         * Create new row with the same details as above
         */

        $data = array(
            'job_cus_id'            =>  $this->qte_cus_id,
            'job_enq_id'            =>  $this->qte_enq_id,
            'job_qte_id'            =>  $this->qte_id,
            'job_title'             =>  $this->qte_title,
            'job_description'       =>  $this->qte_description,
            'job_work_location_id'  =>  $this->qte_work_location_id,
            'job_work_category_id'  =>  $this->qte_work_category_id,
            'job_work_type_id'      =>  $this->qte_work_type_id,
            'job_cus_code'          =>  $this->qte_cus_code,
            'job_cus_name'          =>  $this->qte_cus_name,
            'job_wt_ref_code'       =>  $this->qte_wt_ref_code,
            'job_wt_description'    =>  $this->qte_wt_description,
            'job_wl_code'           =>  $this->qte_wl_code,
            'job_wl_description'    =>  $this->qte_wl_description,
            'job_wc_ref_code'       =>  $this->qte_wc_ref_code,
            'job_wc_description'    =>  $this->qte_wc_description,
            'job_qte_title'         =>  $this->qte_title,
            'job_datetime_created'  =>  date('Y-m-d H:i:s')
        );

        $newRowId = $jobsTable->insert($data);

        /*
         * Update the status of the old row to match the $status passed into this function
         */

        $this->qte_status = "Accepted";
        $this->save();

        $db->commit();

        return $newRowId;
    } 
    catch (Exception $e) {
        throw new Exception($e->getMessage());
        return false;
    }
}
于 2012-08-03T09:18:42.643 回答