MySQL InnoDB事务回滚不起作用(MySQL InnoDB transaction rollback is not working)
我遇到了从Yii框架(使用PDO)发送到InnoDB MySql数据库的事务的问题,问题是:回滚不起作用,更新语句立即提交。
- 我试图明确设置autoCommit,没有运气。
- 查了Mysql的通用日志,只有一个Connection正在打开,发送了一个Start事务,后来只有一个回滚。
- 在通用日志中,“开始事务”,“回滚”和更新语句都来自同一客户端线程。
- 在整个日志中没有发现任何提交。
- 尝试从mysql客户端(MySQLWorkbench)回滚示例和回滚工作!
问题在于MySql在从Yii应用程序发送它时立即提交,我不知道为什么。
没有特别的原因,我只是尝试在my.cnf中启用log_bin并且回滚工作!!
有人能解释发生了什么吗?
我使用MySql 5.6.25,PHP 5.6.10和Yii 1.1.14。
更新:
事实证明,我犯了一个错误,我认为在启用log_bin后回滚工作,但它没有。
所以现在我回到了原来的问题,回滚不起作用,这里是源代码:
$transaction = Yii::app()->db->beginTransaction(); try { $data = array(); // fill some data here.. $model = Model::createOrUpdate($data); $errors = $model->getErrors(); } catch (Exception $e) { $errors []= $e->getMessage(); } if (empty($errors)) { $msg = 'Success message!'; $transaction->commit(); echo CJSON::encode(array('success', $msg)); } else { if ($transaction->active) { $transaction->rollback(); } echo CJSON::encode(array('error', implode(', ', $errors))); } Yii::app()->end();
I had a problem with a transaction sent from Yii framework (using PDO) to InnoDB MySql database, the problem was: rollback was not working, update statement was committing right away.
- I tried setting autoCommit explicitly, with no luck.
- Checked Mysql general log, only one Connection is being open, with one Start transaction sent, and later only one rollback.
- In the general log, "Start transaction", "rollback" and the update statement were all from the same client thread.
- No commit was found in the whole log.
- Tried a rollback example from a mysql client (MySQLWorkbench) and rollback worked!
The problem was just that MySql was committing right away when its sent from the Yii app and I didn't know why.
For no particular reason, I just tried enabling log_bin in my.cnf and the rollback worked!!
Can someone please explain what just happened?
I'm using MySql 5.6.25, with PHP 5.6.10 and Yii 1.1.14.
Update:
Turns out that I have made a mistake, I thought the rollback worked after enabling log_bin, but it didn't.
So now I'm back to the original problem, the rollback is not working, and here is the source code:
$transaction = Yii::app()->db->beginTransaction(); try { $data = array(); // fill some data here.. $model = Model::createOrUpdate($data); $errors = $model->getErrors(); } catch (Exception $e) { $errors []= $e->getMessage(); } if (empty($errors)) { $msg = 'Success message!'; $transaction->commit(); echo CJSON::encode(array('success', $msg)); } else { if ($transaction->active) { $transaction->rollback(); } echo CJSON::encode(array('error', implode(', ', $errors))); } Yii::app()->end();
原文:https://stackoverflow.com/questions/39944305
最满意答案
所以这就是我想出来的,我走了数据库唯一的路线进行一些测试,虽然响应很快,但它没有很好地扩展,连接很快就被吃掉了。 所以我决定写一些快速的缓存逻辑。 到目前为止,它工作得很好,似乎可以让我扩展我想要的聊天。
$cacheFile = 'cache/chat_'.$_GET['last'].'.json'; if (file_exists($cacheFile) && filemtime($cacheFile) + QUERY_REFRESH_RATE > time()) { readfile($cacheFile); } else { require_once("../../../../wp-load.php"); $timestampMin = gmdate("Y-m-d H:i:s", (time() - 7200)); $sql= "/*qc=on*/" . "SELECT * FROM ". DB_TABLE ."chat_posts WHERE ID > ". $_GET['last'] . " AND timestamp > '".$timestampMin."' ORDER BY ID;"; $posts = $wpdb->get_results($sql); $json = json_encode($posts); echo $json; file_put_contents($cacheFile,$json); }
它的优点在于它允许我对消息运行我的格式化功能,例如将URL解析为实际链接等,而且开销更少。
So this is what I came up with, I went the DB only route for a few tests and while response was snappy, it didn't scale well and connections quickly got eaten up. So I decided to write a quick little bit of caching logic. So far it has worked wonderfully and seems to allow me to scale my chat as big as I want.
$cacheFile = 'cache/chat_'.$_GET['last'].'.json'; if (file_exists($cacheFile) && filemtime($cacheFile) + QUERY_REFRESH_RATE > time()) { readfile($cacheFile); } else { require_once("../../../../wp-load.php"); $timestampMin = gmdate("Y-m-d H:i:s", (time() - 7200)); $sql= "/*qc=on*/" . "SELECT * FROM ". DB_TABLE ."chat_posts WHERE ID > ". $_GET['last'] . " AND timestamp > '".$timestampMin."' ORDER BY ID;"; $posts = $wpdb->get_results($sql); $json = json_encode($posts); echo $json; file_put_contents($cacheFile,$json); }
Its also great in that it allows me to run my formatting functions against messages such as parsing URL's into actual links and such with much less overhead.
相关问答
更多-
通过本地客户端程序获取服务器mysql数据库信息[2023-04-27]
ajax -
看起来WebSockets在像你这样的情况下提供更好的性能。 Web Socked 小握手头 握手后的全双工通信。 建立连接后,每传输的请求/响应仅添加2个字节 HTTP HTTP头与每个请求一起发送 另一方面,WebSocket是一项相对较新的技术。 调查网络浏览器支持潜在的网络相关问题是明智的。 参考: http://websocket.org/quantum.html http://www.youtube.com/watch?v=Z897fkPn7Rw http://en.wikipedia.org/ ...
-
在PHP中执行长时间运行的过程时要小心,因为PHP的zend引擎和GC不适合长时间运行的进程。 因此,我强烈建议使用像齿轮工这样的合适的工作经理。 gearman确实有一个PHP扩展 。 使用作业管理器可以让您完全控制每个过程。 你可以开始/停止进程和taks。 另一种选择是使用队列(如amqp )来更清晰地处理这些任务。 哪一个更适合您的用例,我会让您决定。 Goto the following link for the exact solution to your problem: PHP auto-k ...
-
在这种情况下,建议在try...catch函数中使用mkdir并捕获错误(如果是这种情况)。 另一方面, mkidr将返回一个布尔值:如果目录创建成功,则返回true如果是mkidr则返回false 。 1.version try { mkdir("/var/www/html/test", 0700, true); } catch ($ex Exception) { echo $ex->getMessage(); } 2.version if (!mkdir("/var/www/html/ ...
-
关于减少AJAX快速调用的PHP脚本的服务器开销的建议(Advice on reducing server overhead of rapidly called PHP script from AJAX)[2022-04-17]
所以这就是我想出来的,我走了数据库唯一的路线进行一些测试,虽然响应很快,但它没有很好地扩展,连接很快就被吃掉了。 所以我决定写一些快速的缓存逻辑。 到目前为止,它工作得很好,似乎可以让我扩展我想要的聊天。 $cacheFile = 'cache/chat_'.$_GET['last'].'.json'; if (file_exists($cacheFile) && filemtime($cacheFile) + QUERY_REFRESH_RATE > time()) { readfile($ca ... -
保护从ajax [duplicate]调用的php脚本的执行(Protect the execution of my php script called from ajax [duplicate])[2023-10-15]
if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') { // code that has to be executed in response to ajax req. } ref: 仅限AJAX访问 if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SER ... -
同一脚本的多个实例确实可以同时运行。 需要注意的是,性能会降低同时运行的脚本数量。 此外,您应该注意,如果您正在与数据库交互,并且您LOCK表以进行写入,则无法异步完成并且将导致形成队列(尽管是次要的,取决于正在执行的操作)。 证明 您可以通过创建一个PHP脚本来检查这一点,该脚本在某处调用exec()并执行另一个PHP脚本(或者确实是同一个脚本 - 但要注意不要创建无限循环)。 也许将时间戳吐出到文件或数据库,以便您可以充分地看到它发生。 Multiple instances of the same sc ...
-
首先,你是说你没有推得太多的数据。 对我来说,你在拉。 这与您可能知道的不完全相同。 您可以使用Websockets,长轮询或服务器发送的事件。 ( 阅读这篇文章 ) First, you are saying that you are not pushing too much data. For me, you are pulling. Which is not exactly the same thing as you might know. You can use Websockets, long-p ...
-
通过AJAX获取在服务器上调用PHP文件的域(Getting the domain that calls an PHP file on your server through AJAX)[2022-07-06]
“我认为引用者将是metromapper.org,因为该域在加载后调用了yourmapper.com脚本” 这实际上是不正确的。 首先,你不应该依赖HTTP_REFERER,因为它是大多数(不是所有)浏览器传递的自愿参数,并且很容易被欺骗。 我可以使用CURL发送您的网站请求,如果我愿意,看起来像推荐人是whitehouse.gov。 那里没有安全措施。 话虽如此。 浏览器将该参数设置为将用户引用到当前加载页面的页面。 不是脚本。 因此,您看到结果的原因是因为用户通过stackoverflow.com上的链 ... -
使用$ .ajax从服务器上的文本文件读取数据以调用php脚本(Read Data from text file on server using $.ajax to call a php script)[2023-02-25]
$ .ajax中的类型应为大写字母 type: 'GET' function readText() { var url = "readdata.php"; var result = ""; $.ajax({ url: url, type: 'GET', dataType: 'text', success: function (data) { ...