简体   繁体   English

如何为Mysql事务赋予多个连接原子?

[英]How Atomic are Mysql Transactions given multiple connections?

I checked related questions, the answer is kinda "yes, very atomic". 我检查了相关问题,答案有点 “是的,非常原子”。

But Im going more specific, because those questions were not that specific and so the answers: 但是我要更具体一些,因为这些问题不是那么具体,因此答案如下:

  1. MySQL/INNODB MySQL的/ INNODB
  2. Several connections at the "same" time “相同”时间的几个连接
  3. A transaction probably WRITING the same tables (even more, the same queries). 一个事务可能在写相同的表(甚至更多,相同的查询)。
  4. Only one database 只有一个数据库
  5. No lock table will be done at all 根本不会做锁表
  6. . No nested transactions 无嵌套交易
  7. No memcache or any othe similar system, everything as simple as possible. 没有内存缓存或其他类似系统,一切都尽可能简单。
  8. I believe we are using "serializable isoloation, not low level" 相信我们使用的是“可序列化的隔离,而不是低级”

In this scenario, are we garanteed by Mysql engine that data integrity will be mantained, or going to specific cases: 在这种情况下,我们是否由Mysql引擎保证将保持数据完整性,或针对特定情况:

  • Those "at the same time " writings will be queued? 那些“同时”的著作会排队吗?

  • Rollback in fact will work as spected. 实际上,回滚将按预期工作。

  • readings at "the same time" will find a consistency int the state for those writings? “同时”阅读会发现这些作品的状态是否一致?

My simple concerns is that if "commits" and "rollbacks" inside transcations of different connections are atomic or they just make a mess :-) 我的简单担心是,如果不同连接的事务内部的“提交”和“回滚”是原子的,或者它们只是一团糟:-)

Thanks. 谢谢。

The "commit" will end a transaction. “提交”将结束交易。

It's the transaction isolation level that really determines whether 事务隔离级别真正决定了是否

  • all statements executed in the context of a transaction are based on a consistent point-in-time snapshot at the beginning of the transaction (REPEATABLE READ and SERIALIZABLE), or whether 在事务上下文中执行的所有语句都基于事务开始时的一致时间点快照(REPEATABLE READ和SERIALIZABLE),或者是否

  • each statement within the transaction will see changes committed by other transactions ala Oracle (READ COMMITTED), or whether 事务中的每个语句将看到其他事务(例如Oracle)提交的更改(READ COMMITTED),或者是否

  • each statement will see changes made by other transactions that are not yet committed ala SQL Server dirty reads (READ UNCOMMITTED) 每个语句将看到尚未提交的其他事务所做的更改,例如SQL Server脏读(READ UNCOMMITTED)

To answer your questions: 要回答您的问题:

The "at the same time writings" will be applied when locks can be obtained. 当获得锁时,将应用“同时写入”。 If no other session holds incompatible locks, then locks are obtained, and changes can be applied. 如果没有其他会话持有不兼容的锁,则将获得锁,并且可以应用更改。

Rollback will work as expected, that is, a transaction ended with a ROLLBACK statement will revert any changes that were applied, the locked rows will be returned to the state they were in at the beginning of the transaction, and locks will be released. 回滚将按预期工作,也就是说,以ROLLBACK语句结尾的事务将还原已应用的所有更改,锁定的行将返回到事务开始时的状态,并且锁将被释放。 (This also includes any DML changes applied by triggers.) Note that this applies ONLY to InnoDB. (这还包括触发器应用的所有DML更改。)请注意,这仅适用于InnoDB。 Any changes applied to MyISAM tables will have already been committed as if by an Oracle-style autonomous transaction. 应用于MyISAM表的任何更改都已经被提交,就好像由Oracle样式的自主事务一样。

The "at the same time reads" will each be from a consistent snapshot (with REPEATABLE READ or SERIALIZABLE transaction isolation level.) 每个“同时读取”都来自一致的快照(具有REPEATABLE READ或SERIALIZABLE事务隔离级别。)

NOTE The "commits" and "rollbacks" don't happen "inside transactions", they CONCLUDE a transaction, they mark the end of a transaction. 注意 “提交”和“回滚”不会在“内部事务”中发生,它们包含一个事务,它们标志着事务的结束。 The consistent snapshot at the start of the transaction is gone; 事务开始时的一致快照消失了; the next transaction will get its own consistent snapshot. 下一个事务将获得其自己的一致快照。 Those operations aren't best described as "atomic", but they won't, in and of themselves, make a mess. 最好不要将这些操作描述为“原子”操作,但它们本身不会造成混乱。

It's the InnoDB locking mechanism that prevents conflicting changes. InnoDB锁定机制可防止发生冲突的更改。 If two simultaneous sessions are attempting to make a change to the same row (one session overwriting changes made by the other), at least one of the transactions will wait to obtain the lock held by the other transaction. 如果两个同时进行的会话正试图对同一行进行更改(一个会话覆盖另一行所做的更改),则至少一个事务将等待获取另一事务持有的锁。 When the lock is released by the first transaction, the second transaction can obtain the lock, and proceed with its changes; 当第一个事务释放该锁时,第二个事务可以获得该锁,并继续进行更改。 it is free to overwrite changes just made by the other session. 您可以随意覆盖其他会话所做的更改。

Whether things wind up as a mess really depends on the design of the transactions, and what qualifies as a mess. 事情是否像一团糟一样最终取决于交易的设计以及什么才是一团糟。

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

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