MySQL 数据库默认实现事务吗?
我想知道当多个用户尝试同时更新 MySQL 数据库时会发生什么。上网了解交易。
我是否需要为我的每一个数据库 UPDATE 查询定义事务语句。(START TRANSACTION,... COMMIT/ROLLBACK
)?有没有一种自动化的方法来实现这一目标?
I was wondering what happens when several users try to UPDATE a MySQL database at the same time. Went online and learnt about Transactions.
Do I need to define the transactions statements.(START TRANSACTION,... COMMIT/ROLLBACK
) for every one of my database UPDATE queries? Is there an automated way to achieve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
默认情况下,MySQL 启用自动提交,可以使用
将其关闭
设置会话自动提交=off;
对于基于事务的查询(例如银行业务),将执行手动使用 TRANSACTION,这将忽略自动提交设置。例如:
如果任何 UPDATE 语句出现问题,通过执行 ROLLBACK; 事务将中止并且不执行 UPDATE。
当多个用户尝试更新同一个表时,MySQL 会在相关行上放置一个锁,以防止同时写入。第一个 UPDATE 查询将通过提交(成功)或回滚(通常出现问题)来释放锁,然后第二个更新查询才能获取锁并执行 UPDATE。
By default , MySQL has autocommit enabled, which can be toggled off using
set session autocommit=off;
For transaction based queries like banking, manual use of TRANSACTION is executed, which ignores the autocommit setting. e.g:
Should something went wrong in any of the UPDATE statements,by executing
ROLLBACK;
the transaction is aborted and no UPDATE is performed.When several users try to UPDATE the same table, MySQL will place a LOCK on related rows which prevents simultaneous writing. The first UPDATE query will release the lock by either committing (successful) or rolling back (usually something went wrong) before the second update query can acquire the lock and do the UPDATE.