我应该何时在查询中使用事务?

时间:2022-10-19 22:19:04

I'm reading very detailed tutorials on how to use transactions with database types and database engines, but I haven't found a guide that teaches me when and why I should use them.

我正在阅读关于如何使用数据库类型和数据库引擎的事务的非常详细的教程,但是我还没有找到一个指导来告诉我何时以及为什么要使用它们。

I know transactions are usually used for banking, so when we work with money data, but I can imagine they are used in many other ways.

我知道交易通常用于银行业务,所以当我们处理货币数据时,我可以想象它们在许多其他方面被使用。

Today I'm working on a page with various INSERT statements for a relational database, and I wanted to know if this is one of the cases when I should use them.

今天我正在为一个关系数据库编写一个包含各种INSERT语句的页面,我想知道这是否是我应该使用它们的情况之一。

I get an impression that I don't know the cases when the data can be partially lost (apart from coder errors) so I'm always worried about when I should use them.

我有一个印象,我不知道数据可能部分丢失(除了编码错误),所以我总是担心什么时候应该使用它们。

Can someone explain or give some link with these fundamental rules?

有人能解释这些基本规则吗?

I'm using MySQL 5.0.8. Should I use InnoDB for all tables that need transactions? If yes, is InnoDB slower than the common MyISAM but I shouldn't worry about that?

我使用MySQL 5.0.8。我应该对所有需要事务的表使用InnoDB吗?如果是,InnoDB是否比普通的ismyam慢,但是我不需要担心这个?

thanks

谢谢

5 个解决方案

#1


15  

Basically any time you have a unit of work that is either sensitive to outside changes or needs the ability to rollback every change, if an error occurs or some other reason.

基本上,只要您有一个工作单元,该单元对外部更改非常敏感,或者需要能够在发生错误或其他原因时回滚每个更改。

Look here for some excellent answers and their reasons for using them.

这里有一些很好的答案和使用它们的理由。

#2


7  

In addition to what Nick Craver wrote, you would want to use a transaction when you have a series of writes that need to be performed atomically; that is, they should all succeed or none should succeed.

除了Nick Craver所写的之外,当您有一系列需要以原子方式执行的写操作时,您还希望使用事务;也就是说,他们都应该成功,或者不应该成功。

#3


6  

Transactions should be used when there is the possibility that either failure to complete or someone else reading or writing in the middle of your task could cause damage to the data. These include but are not limited to:

当有可能在你的任务中没有完成或其他人的阅读或写作可能会对数据造成损害时,应该使用事务。这些包括但不限于:

  • Reading from a table for subsequent deletion
  • 从表中读取以便后续删除
  • Writing related data to multiple tables
  • 将相关数据写入多个表

#4


3  

You use transactions when you have a group of actions that must be atomic (either all succeed or none succeed) Wrapping these actions in a transaction allows you to rollback actions that have already succeeded when you encounter an error. It also ensures that the data you are working with is consistent as locks will be held until the transaction is complete.

当您有一组操作必须是原子性的(要么全部成功,要么没有成功)时,您可以使用事务来包装这些操作,当遇到错误时,您可以回滚已经成功的操作。它还确保您正在处理的数据是一致的,因为锁将被保存到事务完成为止。

#5


0  

In some frameworks, e.g. Spring, automatic transactions allow to re-execute a transaction if it failed.

在某些框架中,例如Spring,自动事务允许在事务失败时重新执行事务。

#1


15  

Basically any time you have a unit of work that is either sensitive to outside changes or needs the ability to rollback every change, if an error occurs or some other reason.

基本上,只要您有一个工作单元,该单元对外部更改非常敏感,或者需要能够在发生错误或其他原因时回滚每个更改。

Look here for some excellent answers and their reasons for using them.

这里有一些很好的答案和使用它们的理由。

#2


7  

In addition to what Nick Craver wrote, you would want to use a transaction when you have a series of writes that need to be performed atomically; that is, they should all succeed or none should succeed.

除了Nick Craver所写的之外,当您有一系列需要以原子方式执行的写操作时,您还希望使用事务;也就是说,他们都应该成功,或者不应该成功。

#3


6  

Transactions should be used when there is the possibility that either failure to complete or someone else reading or writing in the middle of your task could cause damage to the data. These include but are not limited to:

当有可能在你的任务中没有完成或其他人的阅读或写作可能会对数据造成损害时,应该使用事务。这些包括但不限于:

  • Reading from a table for subsequent deletion
  • 从表中读取以便后续删除
  • Writing related data to multiple tables
  • 将相关数据写入多个表

#4


3  

You use transactions when you have a group of actions that must be atomic (either all succeed or none succeed) Wrapping these actions in a transaction allows you to rollback actions that have already succeeded when you encounter an error. It also ensures that the data you are working with is consistent as locks will be held until the transaction is complete.

当您有一组操作必须是原子性的(要么全部成功,要么没有成功)时,您可以使用事务来包装这些操作,当遇到错误时,您可以回滚已经成功的操作。它还确保您正在处理的数据是一致的,因为锁将被保存到事务完成为止。

#5


0  

In some frameworks, e.g. Spring, automatic transactions allow to re-execute a transaction if it failed.

在某些框架中,例如Spring,自动事务允许在事务失败时重新执行事务。