简体   繁体   English

如果对查询使用多个连接,mysql事务将正常工作吗?

[英]Will a mysql transaction work if multiple connections are used for the queries?

So here it is ... 所以这里......

// I start transaction $mysqli = new mysqli(...); //我开始交易$ mysqli = new mysqli(...);

// Start transaction $mysqli->autocommit(FALSE); //开始事务$ mysqli-> autocommit(FALSE);

  • here is a select prepared stmt with its own db connection 这是一个选择准备好的带有自己的数据库连接的stmt
  • here is an insert prepared stmt with its own db connection 这是一个插入准备好的带有自己的数据库连接的stmt
  • here is an slect prepared stmt with the above db conn 这是与上面的db conn一起准备的stmt
  • insert new conn 插入新的conn
  • insert new conn 插入新的conn
  • insert new conn 插入新的conn

if every prepared query returs true it will($mysqli->commit()) else $mysqli->rollback 如果每个准备好的查询都返回true,它将返回($ mysqli-> commit())否则为$ mysqli-> rollback

I dont know for what reason that transaction will autocommit 我不知道该交易会自动提交的原因

I've done some transaction with 3 multiple connections and they rolledback...this one doesnt... Is mysql made to support a transaction that has queries with individual connections? 我已经用3个多个连接完成了一些事务,但它们回滚了……这不是... mysql是否支持通过单个连接查询的事务?

Solved... if queries from different tables are interdependent like in my case and you need strong data access security , set autocommit to false for for each connection that you open , set a var to false for each failed bussness logic, if anything fails , rollback every set of queries , else comit evry query... Yes the transaction is executed per conection, had something else that managed the rollback when i said that mysql did the rollback for some multiple conn transactions 解决...如果像我这样的情况下来自不同表的查询是相互依存的,并且您需要强大的数据访问安全性,请为您打开的每个连接将autocommit设置为false,为每个失败的业务逻辑将var设置为false,如果任何失败,回滚每组查询,否则返回evry查询...是的,事务是按连接执行的,当我说mysql对某些多个conn事务进行了回滚时,还有其他东西可以管理回滚

Re-edit : ofc this wont work like a normal transaction ... in case of a system crash data will be stored untill the moment of the crash 重新编辑:ofc不会像正常事务一样工作...万一发生系统崩溃,数据将一直存储到崩溃时刻

All queries inside of a transaction must be on the same connection. 事务内的所有查询必须位于同一连接上。 If you have three connections, you have three transactions open (assuming they're not in autocommit mode). 如果您有三个连接,则将打开三个事务(假设它们未处于自动提交模式)。

If you actually need distributed transactions, you'll have to use MySQL's XA Transactions —but that should only really be required if you're dealing with transactions in more than one database (or, alternatively, some other non-database transactions). 如果您确实需要分布式事务,则必须使用MySQL的XA事务 -但是只有在要处理多个数据库中的事务(或者其他一些非数据库事务)时才真正需要这样做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM