MySQL Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsDatabasesMySQL Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 21st, 2012, 10:20 AM
sigurboy sigurboy is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 6 sigurboy User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 48 m 49 sec
Reputation Power: 0
Transaction and Select For Update help

Hello,

Can someone tell me the differences between the following, besides the fact that with the transaction you can rollback if something bad happens? Does the transaction prevent another client from reading/writing the count until its done?

1. Transaction way:
start transaction;
select count from mutable where id = 2;
// calculate new value of count, which ends up being 100
update mytable set count = 100 where id = 2 limit 1;
commit;


2. Select for Update way:
select count from mutable where id = 2 for update;
// calculate new value of count, which ends up being 100
update mytable set count = 100 where id = 2 limit 1;

Thanks!

Reply With Quote
  #2  
Old November 23rd, 2012, 07:01 AM
sr sr is offline
Problem Solver
Dev Shed Specialist (4000 - 4499 posts)
 
Join Date: Jan 2001
Location: Stockholm, Sweden
Posts: 4,426 sr User rank is Colonel (50000 - 60000 Reputation Level)sr User rank is Colonel (50000 - 60000 Reputation Level)sr User rank is Colonel (50000 - 60000 Reputation Level)sr User rank is Colonel (50000 - 60000 Reputation Level)sr User rank is Colonel (50000 - 60000 Reputation Level)sr User rank is Colonel (50000 - 60000 Reputation Level)sr User rank is Colonel (50000 - 60000 Reputation Level)sr User rank is Colonel (50000 - 60000 Reputation Level)sr User rank is Colonel (50000 - 60000 Reputation Level)sr User rank is Colonel (50000 - 60000 Reputation Level)sr User rank is Colonel (50000 - 60000 Reputation Level)sr User rank is Colonel (50000 - 60000 Reputation Level) 
Time spent in forums: 3 Weeks 4 Days 7 h 6 m 58 sec
Reputation Power: 532
Actually the second option you have written is flawed. A SELECT ... FOR UPDATE doesn't work without a transaction. So basically you have written two transactional ways where you in one doesn't lock the rows in the SELECT and in the second you do.

So your two options should have looked like:
Quote:
1. Transaction way:
start transaction;
select count from mutable where id = 2;
// calculate new value of count, which ends up being 100
update mytable set count = 100 where id = 2 limit 1;
commit;


2. Select for Update way:
start transaction;
select count from mutable where id = 2 for update;
// calculate new value of count, which ends up being 100
update mytable set count = 100 where id = 2 limit 1;
commit;

And the difference in the default transaction mode REPEATABLE READ is that the second way will lock the record earlier (already in the select and not in the update).

But logically it doesn't matter if they both are trying to update the same value. Since in both ways the second thread will end up waiting for the first thread to commit and then the second transaction will fail and roll back.

But if you change transaction mode some of this behavior might change.
__________________
/Stefan

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMySQL Help > Transaction and Select For Update help

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap