事务不做回滚
Transactions not doing the rollback
我有更新用户的功能:
function update_user_set_common($date_of_birth, $mobile, $street, $housenumber, $addition, $postal_code, $location, $country)
{
try {
$this->mysqli->begin_transaction();
$this->update_user_set_date_of_birth($date_of_birth);
$this->update_user_set_mobile($mobile);
$this->update_user_set_address($street, $housenumber, $addition, $postal_code, $location, $country);
$this->mysqli->commit();
return true;
} catch (Exception $e) {
$this->mysqli->rollback();
echo "Error: " . $e;
return false;
}
}
如您所见,我打开了一个交易。当一切正常时它应该提交,如果发生错误它应该回滚。
这是我的更新查询的示例:
function update_user_set_date_of_birth($date_of_birth)
{
return $this->update_user_set_singlefield(COL_USER_DATE_OF_BIRTH, $date_of_birth);
}
function update_user_set_singlefield($field, $value)
{
if ($update_stmt = $this->mysqli->prepare("UPDATE " . TABLE_USER . " SET " . $field . " = ? WHERE " . COL_USER_ID . " = ?")) :
if (!$update_stmt->bind_param("ss", $value, $this->USER_ID)) :
$update_stmt->close();
return false;
endif;
if (!$update_stmt->execute()) :
$update_stmt->close();
return false;
else :
$update_stmt->close();
return true;
endif;
else :
return false;
endif;
}
所以现在 update_user_set_mobile
失败但没有回滚。 update_user_set_date_of_birth
之前的语句仍然执行,不会改回来。
为什么没有回滚?
编辑
示例创建 table 以显示我使用 innodb:
CREATE TABLE `USER` (
`USER_ID` bigint(20) NOT NULL PRIMARY KEY AUTO_INCREMENT,
`USER_E_MAIL` varchar(255) COLLATE utf8_bin NOT NULL,
`USER_PASSWORT` varchar(255) COLLATE utf8_bin NOT NULL,
`USER_PASSWORT_SALT` varchar(255) COLLATE utf8_bin NOT NULL,
`USER_FIRSTNAME` varchar(255) COLLATE utf8_bin NOT NULL,
`USER_LASTNAME` varchar(255) COLLATE utf8_bin NOT NULL,
`USER_DATE_OF_BIRTH` varchar(200) COLLATE utf8_bin DEFAULT NULL,
`USER_MOBILE` varchar(255) COLLATE utf8_bin DEFAULT NULL,
`ADDRESS_ID` bigint(20) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
关闭自动提交而不是 mysqli->begin_transaction();
$this->mysqli->autocommit(false);
我有更新用户的功能:
function update_user_set_common($date_of_birth, $mobile, $street, $housenumber, $addition, $postal_code, $location, $country)
{
try {
$this->mysqli->begin_transaction();
$this->update_user_set_date_of_birth($date_of_birth);
$this->update_user_set_mobile($mobile);
$this->update_user_set_address($street, $housenumber, $addition, $postal_code, $location, $country);
$this->mysqli->commit();
return true;
} catch (Exception $e) {
$this->mysqli->rollback();
echo "Error: " . $e;
return false;
}
}
如您所见,我打开了一个交易。当一切正常时它应该提交,如果发生错误它应该回滚。
这是我的更新查询的示例:
function update_user_set_date_of_birth($date_of_birth)
{
return $this->update_user_set_singlefield(COL_USER_DATE_OF_BIRTH, $date_of_birth);
}
function update_user_set_singlefield($field, $value)
{
if ($update_stmt = $this->mysqli->prepare("UPDATE " . TABLE_USER . " SET " . $field . " = ? WHERE " . COL_USER_ID . " = ?")) :
if (!$update_stmt->bind_param("ss", $value, $this->USER_ID)) :
$update_stmt->close();
return false;
endif;
if (!$update_stmt->execute()) :
$update_stmt->close();
return false;
else :
$update_stmt->close();
return true;
endif;
else :
return false;
endif;
}
所以现在 update_user_set_mobile
失败但没有回滚。 update_user_set_date_of_birth
之前的语句仍然执行,不会改回来。
为什么没有回滚?
编辑
示例创建 table 以显示我使用 innodb:
CREATE TABLE `USER` (
`USER_ID` bigint(20) NOT NULL PRIMARY KEY AUTO_INCREMENT,
`USER_E_MAIL` varchar(255) COLLATE utf8_bin NOT NULL,
`USER_PASSWORT` varchar(255) COLLATE utf8_bin NOT NULL,
`USER_PASSWORT_SALT` varchar(255) COLLATE utf8_bin NOT NULL,
`USER_FIRSTNAME` varchar(255) COLLATE utf8_bin NOT NULL,
`USER_LASTNAME` varchar(255) COLLATE utf8_bin NOT NULL,
`USER_DATE_OF_BIRTH` varchar(200) COLLATE utf8_bin DEFAULT NULL,
`USER_MOBILE` varchar(255) COLLATE utf8_bin DEFAULT NULL,
`ADDRESS_ID` bigint(20) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
关闭自动提交而不是 mysqli->begin_transaction();
$this->mysqli->autocommit(false);