简体   繁体   English

查询时临时锁定MYSQL表?

[英]Temporarily lock MYSQL table while querying?

I'm very new to working with database. 我对使用数据库非常陌生。 I'm currently using Google Cloud SQL and accessing the database via PHP PDO. 我目前正在使用Google Cloud SQL,并通过PHP PDO访问数据库。

I managed to find a website ( http://wiki.hashphp.org/PDO_Tutorial_for_MySQL_Developers ) that had some example code: 我设法找到一个包含一些示例代码的网站( http://wiki.hashphp.org/PDO_Tutorial_for_MySQL_Developers ):

<?php
try {
    $db->beginTransaction(); //<---- 1

    $db->exec("SOME QUERY"); //<---- 2

    $stmt = $db->prepare("SOME OTHER QUERY?");
    $stmt->execute(array($value));

    $stmt = $db->prepare("YET ANOTHER QUERY??");
    $stmt->execute(array($value2, $value3));

    $db->commit(); //<---- 3
} catch(PDOException $ex) {
    //Something went wrong rollback!
    $db->rollBack();
    echo $ex->getMessage();
}

According to the above code, if I did '$db->beginTransaction();', would that commence locking the table? 根据上面的代码,如果我执行了'$ db-> beginTransaction();',那将开始锁定表吗? Then '$db->exec("SOME QUERY");' 然后'$ db-> exec(“ SOME QUERY”);' would be as normal. 将是正常的。 And finally, '$db->commit();' 最后,'$ db-> commit();' would save everything above and unlock the table? 将保存上面的所有内容并解锁桌子?

If everything were to run correctly, then should I assume that as line 2 is running, no other call to the table can be finished (they'll be queued) and other calls can only begin once line 3 here is called? 如果一切正常运行,那么我是否应该假设在第2行正在运行时,无法完成对表的其他调用(它们将被排队),而其他调用只能在这里的第3行被调用时开始? I just want to know if my understanding of this process (as well as how the code is written) is accurate. 我只想知道我对此过程的理解(以及代码的编写方式)是否正确。

First, since you are using try/catch, you want to make sure PDO's error mode is exceptions: http://php.net/manual/en/pdo.error-handling.php , by default it is silent. 首先,由于您正在使用try / catch,因此要确保PDO的错误模式是例外: http : //php.net/manual/en/pdo.error-handling.php ,默认情况下它是静默的。

Then, yes, when a query fails, PDO should issue an exception which will stop the execution of the try section. 然后,是的,当查询失败时,PDO应该发出一个异常,该异常将停止try节的执行。 During the catch , you should issue a rollBack() and if everything succeeds, you should issue a commit() . catch期间,应发出rollBack() ,如果一切成功,则应发出commit() So how you have that looks correct to me. 因此,您的状况对我来说看起来是正确的。

After you start a transaction, nothing in that connection will be saved until you run commit() . 启动事务后,在运行commit()之前,该连接中的任何内容都不会保存。 rollBack() ends a transaction and returns to autocommit mode without saving any changes. rollBack()结束事务并返回自动提交模式,而不保存任何更改。

From http://php.net/manual/en/pdo.begintransaction.php : http://php.net/manual/en/pdo.begintransaction.php中

Turns off autocommit mode. 关闭自动提交模式。 While autocommit mode is turned off, changes made to the database via the PDO object instance are not committed until you end the transaction by calling PDO::commit(). 关闭自动提交模式后,在通过调用PDO :: commit()结束事务之前,不会提交通过PDO对象实例对数据库所做的更改。 Calling PDO::rollBack() will roll back all changes to the database and return the connection to autocommit mode. 调用PDO :: rollBack()将回滚对数据库的所有更改,并将连接返回到自动提交模式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM