473,545 Members | 2,469 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Atomicity of a SELECT/UPDATE

Hi,
I want to get any one of a number of rows that matches some criteria and
update it so that it is marked as 'mine'. How can I do this safely?

Given something like this table:

create table tbl_new (
t_value varchar(16) primary key not null,
dt_used datetime,
l_pid int(9)
);

I want to get any t_value WHERE dt_used is null and then set dt_used to
prevent anyone else getting the same t_value.

If I use locking (but I'm using C and I don't see any locking functions)
(nor a START TRANSACTION?) I could:

lock the tables,
select t_value from tbl_new where dt_used is null;
update tbl_new set dt_used = now() where t_value = 'whatever';
unlock the tables;

Without locking I could do something like this: (assume my-pid is unique
between all users of this application at any one moment)
while (1)
{
select t_value from tbl_new where dt_used is null;
update tbl_new set dt_used = now(), l_pid = <my-pid> where t_value =
'<whatever>' and l_pid is null;
select l_pid from tbl_new where t_value = '<whatever>';
if (l_pid == <my-pid>)
break;
// Else someone grabbed that record before us, go round and do it again
}

This sounds long winded to me. Anyone got a better suggestion?
Thanks,
Andy.

--
MySQL General Mailing List
For list archives: http://lists.mysql.com/mysql
To unsubscribe: http://lists.mysql.com/my***********...ie.nctu.edu.tw

Jul 19 '05 #1
0 1331

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
9601
by: Arthur | last post by:
Hey Everyone, Is this an Oracle bug? Here is my cursor statement: CURSOR tax_portal_cursor IS SELECT * FROM web_payment@remotedb WHERE caps_code IN ( SELECT * FROM TABLE(CAST(l_caps_codes AS myTableType)) ) AND processed_datetime IS NULL
0
1276
by: Andy Jackman | last post by:
Hi, I want to get any one of a number of rows that matches some criteria and update it so that it is marked as 'mine'. How can I do this safely? Given something like this table: create table tbl_new ( t_value varchar(16) primary key not null, dt_used datetime, l_pid int(9)
17
4976
by: kalamos | last post by:
This statement fails update ded_temp a set a.balance = (select sum(b.ln_amt) from ded_temp b where a.cust_no = b.cust_no and a.ded_type_cd = b.ded_type_cd and a.chk_no = b.chk_no group by cust_no, ded_type_cd, chk_no)
10
1638
by: Lars | last post by:
Hi, I need a way to read a numeric field and then increment it by one in such a way that even if two users did this at the exact same time, they would still each get their own unique value. I would prefer a method not requiring me to lock the whole table. What is the best way to accomplish this? I am using MySQL 3.23.58 and I use an...
4
2238
by: 001 | last post by:
Hello, The select statement needs only 1 second to complete the query. But the update statement spends 30 minutes. Why? SELECT STATEMENT: declare @IDate smalldatetime select @IDate=col001 from USDay select * from USDay A
1
3653
by: Grant McLean | last post by:
Hi First a simple question ... I have a table "access_log" that has foreign keys "app_id" and "app_user_id" that reference the "application_type" and "app_user" tables. When I insert into "access_log", the referential integrity triggers generate these queries: SELECT 1 FROM ONLY "public"."application_type" x
19
8350
by: Steve | last post by:
ASP error number 13 - Type mismatch with SELECT...FOR UPDATE statement I got ASP error number 13 when I use the SELECT...FOR UPDATE statement as below. However, if I use SELECT statement without FOR UPDATE, it is fine and no error. I also tried Set objRs = objConn.Execute("SELECT * FROM EMP UPDATE OF EMPNO"), but it still couldn't help. ...
3
2663
by: eholz1 | last post by:
Hello PHP programmers. I had a brilliant idea on one of my pages that selects some data from my mysql database. I first set the page up to display some info and an image, just one item, with a row of data, etc. then I thought it would be nice to do a select, and perhaps an update (the title of the image) on the same page.
3
1247
by: kirk | last post by:
I have a class with a custom event that starts a timer when it's holding subscriptions and stops the timer otherwise. The timer repeatedly calls an API which takes a string. For each call the return value of the API is compared against the last obtained value from it and if different, I fire my custom event. The string that the API is...
0
7490
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7682
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
6009
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5351
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5069
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3465
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1911
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1037
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
734
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.