What happens if I use transactions for write operations but don't use those for read operations? My use case:
- get some data1 from db (without transaction)
- create some data2 using data1 (with transaction)
- get some data3 from db (without transaction)
- create some data4 using data2 and data3 (with transaction)
- If no error commit otherwise rollback.
Is it something wrong that I am not using transaction for the 2 read queries?
CodePudding user response:
Edit/Add/Delete Records
A Transaction is used when you want to ensure that a bunch of row edit/add/delete queries are committed together to the db. In other wards, you want to ensure that all sql commands in that bunch runs successfully or don't commit any of the commands. E.g. you are saving a new record for a users table and a users address table together, but you might not want to write to the users table if the address table record fails for some reason. In this case you would use a transaction for both commands.
Read Records
If you understand the above, you know you don't need transactions for read sql commands.
Was the answer helpful? Consider marking the answer tick and upvoting. Thanks