如果对查询使用多个连接,mysql事务将正常工作吗? [英] Will a mysql transaction work if multiple connections are used for the queries?

查看:78
本文介绍了如果对查询使用多个连接,mysql事务将正常工作吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这就是...

//我开始交易 $ mysqli =新的mysqli(...);

// I start transaction $mysqli = new mysqli(...);

//开始交易 $ mysqli-> autocommit(FALSE);

// Start transaction $mysqli->autocommit(FALSE);

  • 这是一个带有其自己的数据库连接的精选准备好的stmt
  • 这是一个插入准备好的stmt,具有自己的数据库连接
  • 这是带有上述db conn的Slect准备的stmt
  • 插入新的conn
  • 插入新的conn
  • 插入新的conn

如果每个准备好的查询都返回true,它将返回($ mysqli-> commit())否则为$ mysqli-> rollback

if every prepared query returs true it will($mysqli->commit()) else $mysqli->rollback

我不知道交易会自动提交的原因

I dont know for what reason that transaction will autocommit

我已经用3个多重连接完成了一些事务,并且它们回滚了……这个没有... mysql是否可以支持具有单个连接查询的事务?

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?

已解决...如果来自不同表的查询是相互依赖的(如我的情况),并且您需要强大的数据访问安全性,则为打开的每个连接将autocommit设置为false,为每个失败的业务逻辑将var设置为false,如果一切都会失败,回滚每组查询,否则会返回evry查询... 是的,事务是按连接执行的,当我说mysql对某些多个conn事务进行了回滚时,还有其他方法可以管理回滚

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

重新ofc不能像正常事务一样工作...在系统崩溃的情况下,数据将一直存储到崩溃时刻为止

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

推荐答案

事务内部的所有查询必须位于同一连接上.如果您有三个连接,则将打开三个事务(假设它们未处于自动提交模式).

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).

如果您实际上需要分布式事务,则必须使用 MySQL的XA交易,但是只有在要处理多个数据库中的交易(或者其他一些非数据库交易)时才真正需要XA交易.

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事务将正常工作吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆