Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When should I use a SQLTransaction

What is the appropriate time to use a SQLTransaction?

I use them for all of my INSERT, UPDATE and DELETE statements.

Is this the correct usage or am I doing a bit of overkill?

like image 671
Popeye Avatar asked Dec 28 '22 04:12

Popeye


2 Answers

Use a transaction when you want a series of statements to be treated atomically - that is either they all succeed and are committed, or they are all rolled back.

Since simple statements are already atomic you don't need to explicitly create a transaction around each and every individual statement.

like image 198
Mark Byers Avatar answered Jan 11 '23 22:01

Mark Byers


If your commands are only single issued, and not a group, then it is probably overkill. Transactions are used to group sql commands together, a group that must have all members succeed, or none at all.

http://en.wikipedia.org/wiki/Atomicity_%28database_systems%29

like image 35
Josh Avatar answered Jan 11 '23 23:01

Josh