質問

I have read a lot of documentation about mysql and transactions and from what I have understood is that when I use repeatable read isolation level in MySQL InnoDB and I start a transaction and I add some rows that when I do a select on those rows they aren't seen by MySQL in that transaction. For example:

function dotransaction() {
  $result = mysqli_query("START TRANSACTION");
  if (!$result) return false;

  $result = mysqli_query("INSERT INTO t (f1,f2) VALUES (1,2)");
  if (!$result) {
    mysqli_query("ROLLBACK");
    return false;
  }

  $result = mysqli_query("INSERT INTO t (f1,f2) VALUES (3,4)");
  if (!$result) {
    mysqli_query("ROLLBACK");
    return false;
  }

  $result = mysqli_query("UPDATE t2 SET f3=(SELECT COUNT(*) AS count FROM t WHERE f1=1) WHERE f4=2");
  if (!$result) {
    mysqli_query("ROLLBACK");
    return false;
  }
}

If i execute the following code in PHP it really updates field f3 to 1 in table t2. But I didn't expect that, because when I started the transaction the transaction reads a snapshot from the database and whereever I use a select statement it uses the first read snapshot and not the modifications to the table. When I take the select statement out of the UPDATE statement it also goves met count=1.

役に立ちましたか?

解決

You understand wrong.

Changes to data made within transaction ARE visible within that transaction, so your SELECT statements will see updated rows.

These changes however are not visible outside of the transaction, so if another user connects to the server at the same time, and queries these rows, he will see them in unaltered state.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top