首页 \ 问答 \ MySQL InnoDB事务回滚不起作用(MySQL InnoDB transaction rollback is not working)

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
更新时间:2023-10-02 21:10

最满意答案

所以这就是我想出来的,我走了数据库唯一的路线进行一些测试,虽然响应很快,但它没有很好地扩展,连接很快就被吃掉了。 所以我决定写一些快速的缓存逻辑。 到目前为止,它工作得很好,似乎可以让我扩展我想要的聊天。

$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.

相关问答

更多

相关文章

更多

最新问答

更多
  • sp_updatestats是否导致SQL Server 2005中无法访问表?(Does sp_updatestats cause tables to be inaccessible in SQL Server 2005?)
  • 如何创建一个可以与持续运行的服务交互的CLI,类似于MySQL的shell?(How to create a CLI that can interact with a continuously running service, similar to MySQL's shell?)
  • AESGCM解密失败的MAC(AESGCM decryption failing with MAC)
  • Zurb Foundation 4 - 嵌套网格对齐问题(Zurb Foundation 4 - Nested grid alignment issues)
  • 湖北京山哪里有修平板计算机的
  • SimplePie问题(SimplePie Problem)
  • 在不同的任务中,我们可以同时使用多少“上下文”?(How many 'context' we can use at a time simultaneously in different tasks?)
  • HTML / Javascript:从子目录启用文件夹访问(HTML/Javascript: Enabling folder access from a subdirectory)
  • 为什么我会收到链接错误?(Why do I get a linker error?)
  • 如何正确定义析构函数(How to properly define destructor)
  • 垂直切换菜单打开第3级父级。(Vertical toggle menu 3rd level parent stay opened. jQuery)
  • 类型不匹配 - JavaScript(Type mismatch - JavaScript)
  • 为什么当我将模型传递给我的.Net MVC 4控制器操作时,它坚持在部分更新中使用它?(Why is it that when I pass a Model to my .Net MVC 4 Controller Action it insists on using it in the Partial Update?)
  • 在使用熊猫和statsmodels时拉取变量名称(Pulling variable names when using pandas and statsmodels)
  • 如何开启mysql计划事件
  • 检查数组的总和是否大于最大数,反之亦然javascript(checking if sum of array is greater than max number and vice versa javascript)
  • 使用OpenGL ES绘制轮廓(Drawing Outline with OpenGL ES)
  • java日历格式(java Calendar format)
  • Python PANDAS:将pandas / numpy转换为dask数据框/数组(Python PANDAS: Converting from pandas/numpy to dask dataframe/array)
  • 如何搜索附加在elasticsearch索引中的文档的内容(How to search a content of a document attached in elasticsearch index)
  • LinQ to Entities:做相反的查询(LinQ to Entities: Doing the opposite query)
  • 从ExtJs 4.1商店中删除记录时会触发哪些事件(Which events get fired when a record is removed from ExtJs 4.1 store)
  • 运行javascript后如何截取网页截图[关闭](How to take screenshot of a webpage after running javascript [closed])
  • 如何使用GlassFish打印完整的堆栈跟踪?(How can I print the full stack trace with GlassFish?)
  • 如何获取某个exe应用程序的出站HTTP请求?(how to get the outbound HTTP request of a certain exe application?)
  • 嗨,Android重叠背景片段和膨胀异常(Hi, Android overlapping background fragment and inflate exception)
  • Assimp详细说明typedef(Assimp elaborated type refers to typedef)
  • 初始化继承类中不同对象的列表(initialize list of different objects in inherited class)
  • 使用jquery ajax在gridview行中保存星级评分(Save star rating in a gridview row using jquery ajax)
  • Geoxml3 groundOverlay zIndex(Geoxml3 groundOverlay zIndex)