MySQL InnoDB-对事务感到困惑 [英] MySQL InnoDB - Confused about transactions

查看:102
本文介绍了MySQL InnoDB-对事务感到困惑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用MySQL多年了,但是没有使用InnoDB引擎的丰富经验.

I've been using MySQL for many years but don't have a lot of experience using the InnoDB engine.

我现在正在对其进行一些测试,因为我将要使用它&从我所读的内容来看,如果THAT事务中的任何查询有任何问题,都不应允许任何事情通过".

I'm running some tests on it now as I'm going to be using it & from what I've read it's not supposed to allow anything to "go through" if there are any problems with any of the queries in THAT transaction.

然后我的问题是,为什么在下面的代码中...当第三个查询明显存在问题时,它仍将前两个查询输入数据库吗?

My question is then why, in the below code.... does it still input the first two queries into the database when there is clearly a problem with the third query?

$query = "BEGIN";
mysql_query($query);

$query = "INSERT INTO list_columns(lid,column_name) VALUES(8,'test')";
mysql_query($query);

$query = "INSERT INTO list_columns(lid,column_name) VALUES(8,'test')";
mysql_query($query);

$query = "INSERT INT list_columns(lid,column_name) VALUES(8,'test')";
mysql_query($query);

$query = "COMMIT";
mysql_query($query);

编辑:我了解使用ROLLBACK&全部.....但是我认为事务的整个目的是这样,如果事务中的任何查询根本没有任何问题,那么将不执行任何查询...或者只有多次插入的情况例如,在一个查询中......如果其中一个插入出现问题,那么将不会插入?

I understand about using ROLLBACK & all..... but I thought the whole purpose of transactions was so that if there was any problems at all with any of the queries in the transaction then NONE of them would be executed... or this only the case with multiple inserts in the one query for example.... if one of the inserts presents a problem, then none will be inserted?

推荐答案

要添加到Konerak所说的话...这是使用

To add to what Konerak said... this is a simplified process with PDO instead of mysql for example:

$pdo = new PDO($mysqldsn, $user, $pass);
$pdo->beginTransaction();

try {

  $pdo->query("INSERT INTO list_columns(lid,column_name) VALUES(8,'test')");
  $pdo->query("INSERT INTO list_columns(lid,column_name) VALUES(8,'test')");
  $pdo->query("INSERT INT list_columns(lid,column_name) VALUES(8,'test')");
  $pdo->commit();

} catch (PDOException $e) {

  $pdo->rollBack();
  throw $e;
}

这篇关于MySQL InnoDB-对事务感到困惑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆