Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/269.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在MySql中查找挂起的锁或事务_Php_Mysql_Transactions_Locking - Fatal编程技术网

Php 在MySql中查找挂起的锁或事务

Php 在MySql中查找挂起的锁或事务,php,mysql,transactions,locking,Php,Mysql,Transactions,Locking,我有一段PHP代码,它不能再执行查询了,因为MySql数据库上有一个挂起的锁或已经打开的事务,我还没有找到它。 cron每小时启动一次脚本。有问题的代码是这样的 CommandHelper::log("Calling beginTransaction for item $item->id"); $transaction = Yii::app()->db->beginTransaction(); CommandHelper::log("Trying to

我有一段PHP代码,它不能再执行查询了,因为MySql数据库上有一个挂起的锁或已经打开的事务,我还没有找到它。 cron每小时启动一次脚本。有问题的代码是这样的

    CommandHelper::log("Calling beginTransaction for item $item->id");
    $transaction = Yii::app()->db->beginTransaction();
    CommandHelper::log("Trying to get update lock for item $item->id");
    try {
       // this line will lock a single row
        $item = Item::model()->findBySql("SELECT * FROM " . $item::model()->getTableSchema()->name . " WHERE id=$item->id FOR UPDATE");
        if(!$item)
            throw new Exception("Item $item->id not found in processItem");
        CommandHelper::log("Obtained update lock for item $item->id");


    $now = DateTimeHelper::getInstance()->getStringNow();
    // the following call executes a few queries and causes an exception
    $this->processExpiredDeferredPayments($item,$now);
    // we won't get here
    CommandHelper::log("Item $item->id query confirmed units");
   ....
   ....

        $transaction->commit();

    } catch (Exception $e) {
        $transaction->rollBack();            
        CommandHelper::log("Transaction error:" . $e->getMessage());
    }
日志每小时打印这些行:

...
...
2017-06-03 14:46:04 Item 164 start processing
2017-06-03 14:46:04 Calling beginTransaction for item 164
2017-06-03 14:46:04 Trying to get update lock for item 164
2017-06-03 14:46:04 Obtained update lock for item 164
2017-06-03 14:46:04 Transaction error:There is already an active transaction
2017-06-03 14:46:04 Item 164 end processing
2017-06-03 14:46:04 Item 160 start processing
2017-06-03 14:46:04 Calling beginTransaction for item 160
2017-06-03 14:46:04 Trying to get update lock for item 160
2017-06-03 14:46:04 Obtained update lock for item 160
2017-06-03 14:46:04 Transaction error:There is already an active transaction
2017-06-03 14:46:04 Item 160 end processing
2017-06-03 14:46:04 Item 182 start processing
2017-06-03 14:46:04 Calling beginTransaction for item 182
2017-06-03 14:46:04 Trying to get update lock for item 182
2017-06-03 14:46:04 Obtained update lock for item 182
2017-06-03 14:46:04 Transaction error:There is already an active transaction
2017-06-03 14:46:04 Item 182 end processing
...
...
这是
show engine innodb status

=====================================
2017-06-03 15:00:16 2af55a655700 INNODB MONITOR OUTPUT
=====================================
Per second averages calculated from the last 10 seconds
-----------------
BACKGROUND THREAD
-----------------
srv_master_thread loops: 139314 srv_active, 0 srv_shutdown, 2478167 srv_idle
srv_master_thread log flush and writes: 2617481
----------
SEMAPHORES
----------
OS WAIT ARRAY INFO: reservation count 139170
OS WAIT ARRAY INFO: signal count 139073
Mutex spin waits 15233, rounds 456961, OS waits 14580
RW-shared spins 123701, rounds 3711390, OS waits 123173
RW-excl spins 89, rounds 42528, OS waits 1410
Spin rounds per wait: 30.00 mutex, 30.00 RW-shared, 477.84 RW-excl
------------
TRANSACTIONS
------------
Trx id counter 233590159
Purge done for trx's n:o < 233589999 undo n:o < 0 state: running but idle
History list length 2636
LIST OF TRANSACTIONS FOR EACH SESSION:
---TRANSACTION 225441219, not started
MySQL thread id 271018, OS thread handle 0x2af55a655700, query id 28702104 172.31.10.242 ebroot init
show engine innodb status
---TRANSACTION 0, not started
MySQL thread id 271017, OS thread handle 0x2af55bfa6700, query id 28698794 172.31.10.242 ebroot cleaning up
---TRANSACTION 233590158, not started
MySQL thread id 1, OS thread handle 0x2af55a550700, query id 28702102 localhost 127.0.0.1 rdsadmin cleaning up
   --------
   FILE I/O
   --------
   ...
   ...
=====================================
2017-06-03 15:00:16 2af55a655700 INNODB监视器输出
=====================================
从最后10秒计算的每秒平均值
-----------------
背景线程
-----------------
srv_主线程循环:139314 srv_活动,0 srv_关闭,2478167 srv_空闲
srv_主线程日志刷新和写入:2617481
----------
信号量
----------
OS等待阵列信息:保留计数139170
OS等待阵列信息:信号计数139073
互斥旋转等待15233次,轮数456961次,操作系统等待14580次
RW共享旋转123701,旋转3711390,操作系统等待123173
RW excl旋转89,旋转42528,操作系统等待1410
每次等待旋转轮数:30.00互斥,30.00 RW共享,477.84 RW不包括
------------
交易
------------
Trx id计数器233590159
trx n:o<233589999的清除完成撤消n:o<0状态:运行但空闲
历史记录列表长度2636
每个会话的事务列表:
---事务22544219,未启动
MySQL线程id 271018,操作系统线程句柄0x2af55a655700,查询id 28702104172.31.10.242 ebroot init
显示引擎innodb状态
---事务0,未启动
MySQL线程id 271017,操作系统线程句柄0x2af55bfa6700,查询id 28698794 172.31.10.242 ebroot清理
---事务233590158,未启动
MySQL线程id 1,操作系统线程句柄0x2af55a550700,查询id 28702102 localhost 127.0.0.1 rdsadmin清理
--------
文件I/O
--------
...
...
这是显示完整进程列表的输出:

这里哪个是挂起的事务或锁,以及如何终止它

为什么PHP脚本结束时它没有自动关闭? 我认为,即使一个PHP线程有一个Mysql连接,当线程退出时,Mysql连接会自动关闭,它启动的事务/锁应该自动提交或回滚,不是吗? 谢谢

好的,我找到了问题

在processExpiredDeferredPayments方法中,我忘记了我正在打开另一个事务,因为已经打开了一个事务,Mysql引发了一个异常

现在它可以使用Mysql 5.16和5.35。现在不需要升级


谢谢大家

我注意到innodb状态中的一些事务显示它们被卡在“清理”中。这听起来很像,所以请看我的答案。如果相同的错误影响到您,那么升级MySQL可能会有所帮助。