- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我正在努力克服它,但我就是无法理解使用 PDO 和 MySQL 在 PHP 中处理事务背后的逻辑。
我知道这个问题会很长,但我认为这是值得的。
考虑到我阅读了很多关于 MySQL 事务的文章,服务器如何处理它们,它们与锁和其他隐式提交语句的关系等等,不仅在 SO 上,而且在 MySQL 和 PHP 手册上:
给出这段代码:
架构:
CREATE TABLE table_name (
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
table_col VARCHAR(100) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
CREATE TABLE `another_table` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`another_col` varchar(100) COLLATE utf8_unicode_ci DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
test1.php(使用 PDO::setAttribute(PDO::ATTR_AUTOCOMMIT, 0)
):
<?php
// PDO
define('DB_HOST', 'localhost');
define('DB_USER', 'user');
define('DB_PASS', 'password');
define('DB_NAME', 'db_name');
/**
* Uses `$this->pdo->setAttribute(PDO::ATTR_AUTOCOMMIT,0);`
*/
class Database {
private $host = DB_HOST;
private $user = DB_USER;
private $pass = DB_PASS;
private $dbname = DB_NAME;
private $pdo;
public $error;
private $stmt;
public function __construct($host=NULL,$user=NULL,$pass=NULL,$dbname=NULL) {
if ($host!==NULL)
$this->host=$host;
if ($user!==NULL)
$this->user=$user;
if ($pass!==NULL)
$this->pass=$pass;
if ($dbname!==NULL)
$this->dbname=$dbname;
// Set DSN
$dsn = 'mysql:host=' . $this->host . ';dbname=' . $this->dbname;
// Set options
$options = array(
PDO::ATTR_PERSISTENT => false,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
);
// Create a new PDO instanace
$this->pdo = new PDO($dsn, $this->user, $this->pass, $options);
$this->pdo->exec("SET NAMES 'utf8'");
}
public function cursorClose() {
$this->stmt->closeCursor();
}
public function close() {
$this->pdo = null;
$this->stmt = null;
return true;
}
public function beginTransaction() {
$this->pdo->setAttribute(PDO::ATTR_AUTOCOMMIT,0);
return $this->pdo->beginTransaction();
}
public function commit() {
$ok = $this->pdo->commit();
$this->pdo->setAttribute(PDO::ATTR_AUTOCOMMIT,1);
return $ok;
}
public function rollback() {
$ok = $this->pdo->rollback();
$this->pdo->setAttribute(PDO::ATTR_AUTOCOMMIT,1);
return $ok;
}
public function bind($param, $value, $type = null){
if (is_null($type)) {
switch (true) {
case is_int($value):
$type = PDO::PARAM_INT;
break;
case is_bool($value):
$type = PDO::PARAM_BOOL;
break;
case is_null($value):
$type = PDO::PARAM_NULL;
break;
default:
$type = PDO::PARAM_STR;
}
}
$this->stmt->bindValue($param, $value, $type);
}
public function runquery() {
$this->stmt->execute();
}
public function execute($nameValuePairArray = NULL) {
try {
if (is_array($nameValuePairArray) && !empty($nameValuePairArray))
return $this->stmt->execute($nameValuePairArray);
else
return $this->stmt->execute();
}
catch(PDOException $e) {
$this->error = $e->getMessage();
}
return FALSE;
}
public function lastInsertId() {
return $this->pdo->lastInsertId();
}
public function insert($table, $data) {
if (!empty($data)){
$fields = "";
$values = "";
foreach($data as $field => $value) {
if ($fields==""){
$fields = "$field";
$values = ":$field";
}
else {
$fields .= ",$field";
$values .= ",:$field";
}
}
$query = "INSERT INTO $table ($fields) VALUES ($values) ";
$this->query($query);
foreach($data as $field => $value){
$this->bind(":$field",$value);
}
if ($this->execute()===FALSE)
return FALSE;
else
return $this->lastInsertId();
}
$this->error = "No fields during insert";
return FALSE;
}
public function query($query) {
$this->stmt = $this->pdo->prepare($query);
}
public function setBuffered($isBuffered=false){
$this->pdo->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, $isBuffered);
}
public function lockTables($tables){
$query = "LOCK TABLES ";
foreach($tables as $table=>$lockType){
$query .= "{$table} {$lockType}, ";
}
$query = substr($query,0, strlen($query)-2);
$this->query($query);
return $this->execute();
}
public function unlockTables(){
$query = "UNLOCK TABLES";
$this->query($query);
return $this->execute();
}
}
$db = NULL;
try {
$db = new Database();
$db->beginTransaction();
// If I call `LOCK TABLES` here... No implicit commit. Why?
// Does `$this->pdo->setAttribute(PDO::ATTR_AUTOCOMMIT,0);` prevent it?
$db->lockTables(array('another_table' => 'WRITE'));
$db->insert('another_table', array('another_col' => 'TEST1_ANOTHER_TABLE'));
$db->unlockTables();
// If I insert a row, other MySQL clients do not see it. Why?
// I called `LOCK TABLES` above and as the MySQL manual says:
//
// LOCK TABLES is not transaction-safe and implicitly commits any active transaction before attempting to lock the tables.
//
$db->insert('table_name', array('table_col' => 'TEST1_TABLE_NAME'));
//...
// If I rollback for some reason, everything rolls back, but shouldn't the transaction
// be already committed with the initial `LOCK TABLES`?
// So I should expect to get a PDOException like "There's no active transaction" or something similar, shouldn't I?
//$db->rollback();
// If I commit instead of the above `$db->rollback()` line, everything is committed, but only now other clients see the new row in `table_name`,
// not straightforward as soon I called `$db->insert()`, whereas I guess they should have seen the change
// even before the following line because I am using `LOCK TABLES` before (see `test2.php`).
$db->commit();
}
catch (PDOException $e) {
echo $e->getMessage();
}
if (!is_null($db)) {
$db->close();
}
test2.php(没有 PDO::setAttribute(PDO::ATTR_AUTOCOMMIT, 0)
行的数据库(已注释掉)):
<?php
// PDO
define('DB_HOST', 'localhost');
define('DB_USER', 'user');
define('DB_PASS', 'password');
define('DB_NAME', 'db_name');
/**
* Does not use `$this->pdo->setAttribute(PDO::ATTR_AUTOCOMMIT,0);`
*/
class Database {
private $host = DB_HOST;
private $user = DB_USER;
private $pass = DB_PASS;
private $dbname = DB_NAME;
private $pdo;
public $error;
private $stmt;
public function __construct($host=NULL,$user=NULL,$pass=NULL,$dbname=NULL) {
if ($host!==NULL)
$this->host=$host;
if ($user!==NULL)
$this->user=$user;
if ($pass!==NULL)
$this->pass=$pass;
if ($dbname!==NULL)
$this->dbname=$dbname;
// Set DSN
$dsn = 'mysql:host=' . $this->host . ';dbname=' . $this->dbname;
// Set options
$options = array(
PDO::ATTR_PERSISTENT => false,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
);
// Create a new PDO instanace
$this->pdo = new PDO($dsn, $this->user, $this->pass, $options);
$this->pdo->exec("SET NAMES 'utf8'");
}
public function cursorClose() {
$this->stmt->closeCursor();
}
public function close() {
$this->pdo = null;
$this->stmt = null;
return true;
}
public function beginTransaction() {
//$this->pdo->setAttribute(PDO::ATTR_AUTOCOMMIT,0);
return $this->pdo->beginTransaction();
}
public function commit() {
$ok = $this->pdo->commit();
//$this->pdo->setAttribute(PDO::ATTR_AUTOCOMMIT,1);
return $ok;
}
public function rollback() {
$ok = $this->pdo->rollback();
//$this->pdo->setAttribute(PDO::ATTR_AUTOCOMMIT,1);
return $ok;
}
public function bind($param, $value, $type = null){
if (is_null($type)) {
switch (true) {
case is_int($value):
$type = PDO::PARAM_INT;
break;
case is_bool($value):
$type = PDO::PARAM_BOOL;
break;
case is_null($value):
$type = PDO::PARAM_NULL;
break;
default:
$type = PDO::PARAM_STR;
}
}
$this->stmt->bindValue($param, $value, $type);
}
public function runquery() {
$this->stmt->execute();
}
public function execute($nameValuePairArray = NULL) {
try {
if (is_array($nameValuePairArray) && !empty($nameValuePairArray))
return $this->stmt->execute($nameValuePairArray);
else
return $this->stmt->execute();
}
catch(PDOException $e) {
$this->error = $e->getMessage();
}
return FALSE;
}
public function lastInsertId() {
return $this->pdo->lastInsertId();
}
public function insert($table, $data) {
if (!empty($data)){
$fields = "";
$values = "";
foreach($data as $field => $value) {
if ($fields==""){
$fields = "$field";
$values = ":$field";
}
else {
$fields .= ",$field";
$values .= ",:$field";
}
}
$query = "INSERT INTO $table ($fields) VALUES ($values) ";
$this->query($query);
foreach($data as $field => $value){
$this->bind(":$field",$value);
}
if ($this->execute()===FALSE)
return FALSE;
else
return $this->lastInsertId();
}
$this->error = "No fields during insert";
return FALSE;
}
public function query($query) {
$this->stmt = $this->pdo->prepare($query);
}
public function setBuffered($isBuffered=false){
$this->pdo->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, $isBuffered);
}
public function lockTables($tables){
$query = "LOCK TABLES ";
foreach($tables as $table=>$lockType){
$query .= "{$table} {$lockType}, ";
}
$query = substr($query,0, strlen($query)-2);
$this->query($query);
return $this->execute();
}
public function unlockTables(){
$query = "UNLOCK TABLES";
$this->query($query);
return $this->execute();
}
}
$db = NULL;
try {
$db = new Database();
$db->beginTransaction();
// If I call `LOCK TABLES` here... There's an implicit commit.
$db->lockTables(array('another_table' => 'WRITE'));
$db->insert('another_table', array('another_col' => 'TEST2_ANOTHER_TABLE'));
$db->unlockTables();
// If I insert a row, other MySQL clients see it straightforward (no need to reach `$db->commit()`).
// This is coherent with the MySQL manual:
//
// LOCK TABLES is not transaction-safe and implicitly commits any active transaction before attempting to lock the tables.
//
$db->insert('table_name', array('table_col' => 'TEST2_TABLE_NAME'));
//...
// If I rollback for some reason, the row does not rollback, as the transaction
// was already committed with the initial `LOCK TABLES` statement above.
//
// I cannot rollback the insert into table `table_name`
//
// So I should expect to get a PDOException like "There's no active transaction" or something similar, shouldn't I?
$db->rollback();
// If I commit instead of the above `$db->rollback()` line, I guess nothing happens, because the transaction
// was already committed and as I said above, and clients already saw the changes before this line was reached.
// Again, this is coherent with the MySQL statement:
//
// LOCK TABLES is not transaction-safe and implicitly commits any active transaction before attempting to lock the tables.
//
//$db->commit();
}
catch (PDOException $e) {
echo $e->getMessage();
}
if (!is_null($db)) {
$db->close();
}
我还有以下疑惑和悬而未决的问题:
使用 InnoDB
,两者之间有区别吗PDO::beginTransaction()
和 PDO::setAttribute(PDO::ATTR_AUTOCOMMIT, 0)
当我们在 PHP 和/或带有普通 MySQL 语句 SET AUTOCOMMIT = 0;
和 START TRANSACTION;
的 MySQL?如果是,那是什么?
如果您查看我的 PHP 示例,在 Database::beginTransaction()
包装器方法中,我同时使用了 PDO::beginTransaction()
和 PDO::setAttribute(PDO::ATTR_AUTOCOMMIT, 0)
在文件 test1.php 并且不要在文件中使用 PDO::setAttribute(PDO::ATTR_AUTOCOMMIT, 0)
test2.php。 我发现当我使用 PDO::setAttribute(PDO::ATTR_AUTOCOMMIT, 0)
时会发生奇怪的事情:
PDO::setAttribute(PDO::ATTR_AUTOCOMMIT, 0)
行在Database
(test1.php)里面一个 使用 LOCK TABLES
语句的交易,LOCK TABLES
不 似乎隐式提交事务,因为如果我连接 对于另一个客户端,在代码到达 $db->commit();
行之前,我看不到插入的行,而 MySQL手册说:
LOCK TABLES is not transaction-safe and implicitly commits any active transaction before attempting to lock the tables.
因此我们可以说 PDO::setAttribute(PDO::ATTR_AUTOCOMMIT, 0)
(在 MySQL 上是SET AUTOCOMMIT = 0;
) 事务未隐式提交像 LOCK TABLES
这样的语句?那么我会说有一个MySQL 手册和 PHP PDO 实现之间的不一致(我不是提示,我只是想明白);
没有 Database
(test2.php) 中的 PDO::setAttribute(PDO::ATTR_AUTOCOMMIT, 0)
行,该代码似乎与 MySQL 的行为一致手动 LOCK TABLES 不是事务安全的并且是隐式的
:一旦它到达
在尝试锁定之前提交任何事件事务
表。LOCK TABLES
查询,就会有一个隐式提交,所以在 $db->insert('table_name', array('table_col ' => 'TEST2_TABLE_NAME'));
其他客户端甚至在到达 $db->commit();
;
我刚才描述的以下行为的解释是什么?当我们使用 PHP 的 PDO
并在我们的事务中包含 implicit-commit
语句时,事务如何工作?
我的PHP版本是7.0.22
,MySQL版本是5.7.20
。
感谢关注。
最佳答案
https://dev.mysql.com/doc/refman/5.7/en/innodb-autocommit-commit-rollback.html说:
If autocommit mode is disabled within a session with SET autocommit = 0, the session always has a transaction open. A COMMIT or ROLLBACK statement ends the current transaction and a new one starts.
因此,当您在一个 session (称之为 session 1)中设置 autocommit=0 时,这将隐式打开一个事务,并使其无限期打开。
默认的事务隔离级别是REPEATABLE-READ。因此,在 session 1 明确提交或回滚之前,您的 session 不会看到来自其他 session 工作的已提交更改的刷新 View 。
您在另一个 session 2 中的 LOCK TABLES 确实导致隐式提交,但 session 1 看不到结果,因为它仍然只能看到数据的孤立 View ,因为它有自己的事务快照。
关于PHP PDO MySQL 以及它如何真正处理 MySQL 事务?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47810114/
PHP查询: prepare('SELECT * FROM Locations WHERE user_id = :id LIMIT 0, 5'); $query->bindParam(":id
到目前为止我一直在使用 PDO->bindParam 但是在阅读手册时我发现 PDO->bindValue 我可以告诉 PDO->bindValue 按值传递,而 PDO->bindParam 按引用
我使用PDO进行MySQL数据库连接、选择、更新和删除。 但是我在选择带有特殊字符的行时遇到问题,例如,我想选择带有“Judge-Fürstová Mila”的页面标题, 表页, id titl
我使用的是 PHP 5.2.9 和 Apache 2.2.11 以及 mysql 5.1.32 为什么我不能禁用 PDO::ATTR_EMULATE_PREPARES ? 下面是代码: false)
当 PDO::ATTR_PERSISTENT = true 时,PDO::exec 和 PDO::query 之间似乎存在差异。当使用PDO::exec时,连接将不会被重用,并最终导致MySQL出现“
我正在尝试调整 Zend Skeleton App 以使用 ODBC 连接。我能够在 Zend 之外设置 PDO 连接(同一个表、服务器、所有内容),如下所示: new PDO('odbc:DRIVE
我正在使用 XAMPP 3.2.1 在 Windows 7 上使用 PDO,但在使其正常工作时遇到问题,即使它可以在我的共享托管服务器。 settings.php db.php setAttribu
我正在尝试使用以下代码从 get 变量中获取结果,我使用了另一个代码(在下面列出)并且它有效但是在使用它时无法逃脱,我不知道我有什么做错了,但我需要帮助,我刚刚开始 PDO,所以是的,我是个白痴 :D
我有这个代码 try { $dbh = new PDO('mysql:host=localhost;dbname=db_informations', 'root', ''); $dbh
在我的本地开发机器 (MAMP) 上,我的 MySQL 查询返回正确的大小写。但是,在我的共享托管服务器上,使用 fetch(PDO::FETCH_ASSOC); 时,我的结果是小写的 我试过以下方法
我正在使用这个 PDO 连接: try{ $db=new PDO(" mysql:host=localhost; dbname=...", "dbu...", "pass", array(PDO::M
我需要将 PDO 连接从 controller 传递到 cart 类, function __construct($connection) { $this->cart = new cart($
位于 PHP.net 的 PDO 交易示例表明 PDO::exec() 是事务处理的,但是没有这样的例子使用 PDO::query()。 事务是否涵盖 PDO::query()? 此外,据我所知,PD
几天来我一直在使用来自 http://www.mamp.info 的 MAMP Pro 3.07 . 很棒的工具,一切正常。 但是现在我遇到了问题,对于一个新项目,我需要连接到 MSSQL。 我在 w
我正在尝试在我的 PHP 代码中使用 PDO 模块来连接到数据库。我已经阅读并搜索了类似的主题,但我无法弄清楚我做错了什么。请帮我解决问题。 Apache版本:Apache/2.2.21 (Win32
这个问题不太可能帮助任何 future 的访问者;它只与一个小的地理区域、一个特定的时间点或一个非常狭窄的情况有关,这些情况并不普遍适用于互联网的全局受众。为了帮助使这个问题更广泛地适用,visit
我使用 PDO 连接到我的数据库,我不知道哪种方法比更新、删除和插入、PDO::exec 或 PDO::excute 的另一种方法更好。我应该使用哪个? 最佳答案 虽然这两种方法具有相似的命名(exe
如何在 Codeigniter 中设置 PDO 数据库句柄的属性(PDO::ATTR_ERRMODE)? 最佳答案 我认为更好的选择是使用 MY_Model(然后您对其进行扩展,然后它在整个应用程序中
从 mysql 切换到 PDO 我注意到 fetch 方法改变了 PDO 对象本身!例如: $res=$ps->fetchAll(); echo($res[0]['Mail']); //it'ok /
嗨,我正在使用 PDO 为 mysql 开发一个基本的数据库连接类,但是当我 var dump 返回结果时,它告诉我我有 2 个对象:object(PDO)[2]。不应该只有一个吗? 这是迄今为止我的
我是一名优秀的程序员,十分优秀!