开发者

If PHP's mysql extension supposedly does not support transactions like mysqli, why do I see people using it for transactions online?

开发者 https://www.devze.com 2023-04-10 19:04 出处:网络
I was reading about mysql transactions and I was under the impression that you had to use either mysqli or PDO in order to create transactions.However, I see all over stack exchange and other sites ex

I was reading about mysql transactions and I was under the impression that you had to use either mysqli or PDO in order to create transactions. However, I see all over stack exchange and other sites examples that use the mysql extension like this:

mysql_query("START TRANSACTION");
$rollback=0

if (!mysql_query($query1)){
$rollback=1
}

if (!mysql_query($query2)){
$rollback=1
}

if (!mysql_query($query3)){
$rollback=1
}

if ($rollback == 1){
mysql_query("ROLLBACK");
}
else{开发者_JS百科
mysql_query("COMMIT");
}

What is the difference between doing it this way and using the "special" mysqli specific functions mysqli::rollback and mysqli::commit?

Also, what happens if the php script crashes (IE my app server crashes etc), does the DB server automatically rollback the transaction after a set timeperiod?

Similarly, what happens if the DB server crashes before mysql_query("COMMIT")? Would the transaction be "rolled back"?

Thanks!


It's also perfectly legal and is using MySQL statements directly to implement transaction.

And frankly, I don't see any improvements by using the mysqli syntax. It's a failed abstraction since it maps 1:1 on MySQL only. It would make more sense to use the higher level syntax in PDO, since it will map with a different syntax according to the underlying DB.

However, as Hakre says: mysqli is preferred for performance and interoperability reasons, it's the recommended mysql lib in PHP.

Similarly, what happens if the DB server crashes before mysql_query("COMMIT")? Would the transaction be "rolled back"?

It's easier that an application program crashes rather than a DB server. Any non committed transaction will be rolled back.

Also a transaction can be rolled back because it can't acquire a lock within a certain timeout.

The timeout in seconds an InnoDB transaction may wait for a row lock before giving up. The default value is 50 seconds. A transaction that tries to access a row that is locked by another InnoDB transaction will hang for at most this many seconds before issuing the following error:

ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction

0

精彩评论

暂无评论...
验证码 换一张
取 消