开发者

When should I use transactions in my queries?

开发者 https://www.devze.com 2022-12-18 15:10 出处:网络
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'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.

I get an imp开发者_如何学Pythonression 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?

thanks


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.


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.


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


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.


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


Mostly use for multiple same curd cases, where you want save changes for valid entries, for a multi steps form with multiple input for each step of those form s submit cases, and form submit after getting an external api feedback.. mainly when you want rollback your query for some return value.


When fiddling with a sensitive database manually, it could be also a good idea to use a transaction as a safety / validation step to validate that the changes you made to a table were correct and as intended. This could save you the trouble of saving bad data by mistake.

Example:

I update table A and set field x to a wrong value unintentionally overriding the previous correct value.

If I were inside a transaction I would be able to select my row and check my change, spot the error and rollback. If my row looked correct then I would commit

If I were not inside a transaction, I would have lost the correct value.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号