473,771 Members | 2,372 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Atomicity problem / question

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 ISAM type database.

Thanks,
Lars
Nov 5 '05 #1
10 1660
On Fri, 4 Nov 2005 16:58:28 -0800, "Lars" <no****@dev.nul l> wrote:
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 ISAM type database.

One way: (There are surely many)
Make sure you have an auto-incrementing field. Insert an empty
record, return the new incremented value to the user.

Next user inserts next record, gets next key.

Once they've completed their input, do an update to the empty record
using he auto-incremented number.

--
gburnore at DataBasix dot Com
---------------------------------------------------------------------------
How you look depends on where you go.
---------------------------------------------------------------------------
Gary L. Burnore | ÝÛ³ºÝ³Þ³ºÝ³³ÝÛº ݳ޳ºÝ³Ý³Þ³ºÝ³Ý ÝÛ³
| ÝÛ³ºÝ³Þ³ºÝ³³ÝÛº ݳ޳ºÝ³Ý³Þ³ºÝ³Ý ÝÛ³
Official .sig, Accept no substitutes. | ÝÛ³ºÝ³Þ³ºÝ³³ÝÛº ݳ޳ºÝ³Ý³Þ³ºÝ³Ý ÝÛ³
| ÝÛ 0 1 7 2 3 / Ý³Þ 3 7 4 9 3 0 Û³
Black Helicopter Repair Services, Ltd.| Official Proof of Purchase
=============== =============== =============== =============== ===============
Nov 5 '05 #2
> Make sure you have an auto-incrementing field. Insert an empty
record, return the new incremented value to the user.

Next user inserts next record, gets next key.

Once they've completed their input, do an update to the empty record
using he auto-incremented number.


Thanks for your reply.

Your method would probably work, but I was not totally clear in my original
post: I am looking for is a cleaner method. I would prefer not having to
have a separate table just for generating a unique value (or, rather, one
table for each type of unique value).

Something like this would work if I could lock the record:

UPDATE table SET field = field + 1 WHERE whatever
SELECT field from table WHERE whatever

Short of using a table lock or switching to InnoDB, what is the best way to
accomplish this?

Thanks,
Lars
Nov 5 '05 #3
On Fri, 4 Nov 2005 18:09:10 -0800, "Lars" <no****@dev.nul l> wrote:
Make sure you have an auto-incrementing field. Insert an empty
record, return the new incremented value to the user.

Next user inserts next record, gets next key.

Once they've completed their input, do an update to the empty record
using he auto-incremented number.


Thanks for your reply.

Your method would probably work, but I was not totally clear in my original
post: I am looking for is a cleaner method. I would prefer not having to
have a separate table just for generating a unique value (or, rather, one
table for each type of unique value).

Something like this would work if I could lock the record:

UPDATE table SET field = field + 1 WHERE whatever
SELECT field from table WHERE whatever

Short of using a table lock or switching to InnoDB, what is the best way to
accomplish this?


I didn't say anything at all about a separate table.

The method I mentioned is quite clean. One table, you simply insert
the entire row with nothing in the other fields to get it to give you
the auto-incremented value then use said value to key the update.

--
gburnore at DataBasix dot Com
---------------------------------------------------------------------------
How you look depends on where you go.
---------------------------------------------------------------------------
Gary L. Burnore | ÝÛ³ºÝ³Þ³ºÝ³³ÝÛº ݳ޳ºÝ³Ý³Þ³ºÝ³Ý ÝÛ³
| ÝÛ³ºÝ³Þ³ºÝ³³ÝÛº ݳ޳ºÝ³Ý³Þ³ºÝ³Ý ÝÛ³
Official .sig, Accept no substitutes. | ÝÛ³ºÝ³Þ³ºÝ³³ÝÛº ݳ޳ºÝ³Ý³Þ³ºÝ³Ý ÝÛ³
| ÝÛ 0 1 7 2 3 / Ý³Þ 3 7 4 9 3 0 Û³
Black Helicopter Repair Services, Ltd.| Official Proof of Purchase
=============== =============== =============== =============== ===============
Nov 5 '05 #4
> I didn't say anything at all about a separate table.

The method I mentioned is quite clean. One table, you simply insert
the entire row with nothing in the other fields to get it to give you
the auto-incremented value then use said value to key the update.


Ok. In my case, the table is for storing many different settings &
configuration options. It has the following columns: Category, Item &
Value. Most of the records in the table have nothing to do with unique
values, but a few of them do. E.g. Category = ControlNumbers, Item =
LastPONumber, Value = 1000

If user A and user B simultaneously request the next invoice number, one
should get 1001, the other 1002 and the LastPONumber field will be 1002
afterwards.

If I understand your approach correctly, I would either have to add an
auto-incremented field to this table, or have an extra table just for this
purpose.

Lars


Nov 5 '05 #5
On Fri, 4 Nov 2005 21:40:15 -0800, "Lars" <no****@dev.nul l> wrote:
I didn't say anything at all about a separate table.

The method I mentioned is quite clean. One table, you simply insert
the entire row with nothing in the other fields to get it to give you
the auto-incremented value then use said value to key the update.


Ok. In my case, the table is for storing many different settings &
configuratio n options. It has the following columns: Category, Item &
Value. Most of the records in the table have nothing to do with unique
values, but a few of them do. E.g. Category = ControlNumbers, Item =
LastPONumber , Value = 1000

If user A and user B simultaneously request the next invoice number, one
should get 1001, the other 1002 and the LastPONumber field will be 1002
afterwards.

If I understand your approach correctly, I would either have to add an
auto-incremented field to this table, or have an extra table just for this
purpose.


Right now you do a select for the "LastPONumb er", add one to it and
insert it. CHange the LastPONumber to autoincrementin g. Problem
solved.
--
gburnore at DataBasix dot Com
---------------------------------------------------------------------------
How you look depends on where you go.
---------------------------------------------------------------------------
Gary L. Burnore | ÝÛ³ºÝ³Þ³ºÝ³³ÝÛº ݳ޳ºÝ³Ý³Þ³ºÝ³Ý ÝÛ³
| ÝÛ³ºÝ³Þ³ºÝ³³ÝÛº ݳ޳ºÝ³Ý³Þ³ºÝ³Ý ÝÛ³
Official .sig, Accept no substitutes. | ÝÛ³ºÝ³Þ³ºÝ³³ÝÛº ݳ޳ºÝ³Ý³Þ³ºÝ³Ý ÝÛ³
| ÝÛ 0 1 7 2 3 / Ý³Þ 3 7 4 9 3 0 Û³
Black Helicopter Repair Services, Ltd.| Official Proof of Purchase
=============== =============== =============== =============== ===============
Nov 5 '05 #6
>>> I didn't say anything at all about a separate table.

The method I mentioned is quite clean. One table, you simply insert
the entire row with nothing in the other fields to get it to give you
the auto-incremented value then use said value to key the update.
Ok. In my case, the table is for storing many different settings &
configurati on options. It has the following columns: Category, Item &
Value. Most of the records in the table have nothing to do with unique
values, but a few of them do. E.g. Category = ControlNumbers, Item =
LastPONumbe r, Value = 1000

If user A and user B simultaneously request the next invoice number, one
should get 1001, the other 1002 and the LastPONumber field will be 1002
afterwards.
1. lock tables configuration_s ettings write;
2. update configuration_s ettings set Value=Value+1
where Category = 'ControlNumbers ' and Item = 'LastPONumber';
3. select Value from configuraton_se ttings
where Category = 'ControlNumbers ' and Item = 'LastPONumber';
/* Use Value as your PO number */
4. unlock tables;

If User A is in query 2 or 3, User B will wait at query 1 until User A
finishes query 4.
If I understand your approach correctly, I would either have to add an
auto-incremented field to this table, or have an extra table just for this
purpose.


Right now you do a select for the "LastPONumb er",


No, right now he does a select for Value where Category = 'ControlNumbers ' and
Item = 'LastPONumber'.
add one to it and
insert it. CHange the LastPONumber to autoincrementin g.
You can't change Value in only one row to autoincrementin g.
Problem
solved.


There's a lot of utility in a Keyword = Value approach. For instance,
for an address book application, you might have a table: PersonID,
Attribute, and Value. Attribute might be things like 'First Name',
'Birth Date', 'Home Fax Number', 'Work Email', 'Home Street Address',
etc. You can add more without having to change the schema.
The trouble is, what type is Value? It's got dates, names,
telephone numbers, email addresses, etc. in it. You also end up
doing a lot of joins to get specific attributes (if present) in
specific variables.

Gordon L. Burditt
Nov 5 '05 #7
> 1. lock tables configuration_s ettings write;
2. update configuration_s ettings set Value=Value+1
where Category = 'ControlNumbers ' and Item = 'LastPONumber';
3. select Value from configuraton_se ttings
where Category = 'ControlNumbers ' and Item = 'LastPONumber';
/* Use Value as your PO number */
4. unlock tables; If User A is in query 2 or 3, User B will wait at query 1 until User A
finishes query 4.
Thanks. That is more along the lines I was thinking, but I was not too happy
about a table-level locking since the table is accessed very heavily by many
users. In real life I don't think it would be a problem, though, since it
is only a write lock and only for a split second at a time.
There's a lot of utility in a Keyword = Value approach. For instance,
for an address book application, you might have a table: PersonID,
Attribute, and Value. Attribute might be things like 'First Name',
'Birth Date', 'Home Fax Number', 'Work Email', 'Home Street Address',
etc. You can add more without having to change the schema.
The trouble is, what type is Value? It's got dates, names,
telephone numbers, email addresses, etc. in it.
I find the approach quite flexible for my purposes, since a text field can
store most any type of data. The job of ensuring proper format of dates,
numbers, etc. lie with the business objects of my apps.
You also end up doing a lot of joins to get specific attributes
(if present) in specific variables.

Out of curiosity, could you elaborate on this?

Thanks for your help,
Lars

Nov 5 '05 #8
>Thanks. That is more along the lines I was thinking, but I was not too happy
about a table-level locking since the table is accessed very heavily by many
users. In real life I don't think it would be a problem, though, since it
is only a write lock and only for a split second at a time.


"only a write lock" is a little like "only an atomic bomb".
A write lock prevents others from accessing AT ALL; a read
lock prevents others from writing. But what you need here is
a write lock.
There's a lot of utility in a Keyword = Value approach. For instance,
for an address book application, you might have a table: PersonID,
Attribute, and Value. Attribute might be things like 'First Name',
'Birth Date', 'Home Fax Number', 'Work Email', 'Home Street Address',
etc. You can add more without having to change the schema.
The trouble is, what type is Value? It's got dates, names,
telephone numbers, email addresses, etc. in it.


I find the approach quite flexible for my purposes, since a text field can
store most any type of data. The job of ensuring proper format of dates,
numbers, etc. lie with the business objects of my apps.
You also end up doing a lot of joins to get specific attributes
(if present) in specific variables.

Out of curiosity, could you elaborate on this?


A query for printing business cards with the keyword = value
organization might look like:

SELECT a.value, b.value, c.value, d.value, e.value, f.value, g.value
FROM addrbook a
LEFT JOIN addrbook b on a.personid = b.personid and b.attribute = 'Last Name'
LEFT JOIN addrbook c on a.personid = c.personid and c.attribute = 'Work Street Address'
LEFT JOIN addrbook d on a.personid = d.personid and d.attribute = 'Work City'
LEFT JOIN addrbook e on a.personid = e.personid and e.attribute = 'Work State'
LEFT JOIN addrbook f on a.personid = f.personid and f.attribute = 'Work Zip'
LEFT JOIN addrbook g on a.personid = g.personid and g.attribute = 'Work Phone'
WHERE a.attribute = 'First Name';

(personid, attribute) is a primary key so lookups should be fast,
but it still joins 7 copies of the table. Attribute is a good
candidate for an enum if you're willing to have to change the schema
to add new ones. The corresponding query with a table with a column
for each attribute might be:

SELECT firstname, lastname, workstreetaddre ss, workcity, workstate,
workzip, workphone
FROM addrbook;

Gordon L. Burditt
Nov 5 '05 #9
On Sat, 05 Nov 2005 16:11:48 -0000, go***********@b urditt.org (Gordon
Burditt) wrote:

You can't change Value in only one row to autoincrementin g.


Where did you get that idea?
--
gburnore at DataBasix dot Com
---------------------------------------------------------------------------
How you look depends on where you go.
---------------------------------------------------------------------------
Gary L. Burnore | ÝÛ³ºÝ³Þ³ºÝ³³ÝÛº ݳ޳ºÝ³Ý³Þ³ºÝ³Ý ÝÛ³
| ÝÛ³ºÝ³Þ³ºÝ³³ÝÛº ݳ޳ºÝ³Ý³Þ³ºÝ³Ý ÝÛ³
Official .sig, Accept no substitutes. | ÝÛ³ºÝ³Þ³ºÝ³³ÝÛº ݳ޳ºÝ³Ý³Þ³ºÝ³Ý ÝÛ³
| ÝÛ 0 1 7 2 3 / Ý³Þ 3 7 4 9 3 0 Û³
Black Helicopter Repair Services, Ltd.| Official Proof of Purchase
=============== =============== =============== =============== ===============
Nov 5 '05 #10

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

Similar topics

3
5042
by: Stevey | last post by:
I have the following XML file... <?xml version="1.0"?> <animals> <animal> <name>Tiger</name> <questions> <question index="0">true</question> <question index="1">true</question> </questions>
7
2665
by: nospam | last post by:
Ok, 3rd or is it the 4th time I have asked this question on Partial Types, so, since it seems to me that Partial Types is still in the design or development stages at Microsoft, I am going to ask it differently. FOUR QUESTIONS: The background: I got three (3) files
3
3090
by: Ekqvist Marko | last post by:
Hi, I have one Access database table including questions and answers. Now I need to give answer id automatically to questionID column. But I don't know how it is best (fastest) to do? table before rowID answID qryrow questionID datafield 1591 12 06e 06e 06e question 1593 12 06f 06f 06f question 1594 12 answer to the question 06f
10
3440
by: glenn | last post by:
I am use to programming in php and the way session and post vars are past from fields on one page through to the post page automatically where I can get to their values easily to write to a database or continue to process on to the next page. I am now trying to learn ASP to see if we can replace some of our applications that were written in php with an ASP alternative. However, after doing many searches on google and reading a couple...
10
3737
by: Rider | last post by:
Hi, simple(?) question about asp.net configuration.. I've installed ASP.NET 2.0 QuickStart Sample successfully. But, When I'm first start application the follow message shown. ========= Server Error in '/QuickStartv20' Application. -------------------------------------------------------------------------------- Configuration Error Description: An error occurred during the processing of a configuration file
5
2547
by: John Bokma | last post by:
Hi, I am working on a simple chat program and want to use AJAX. In order to get the new lines added to the chat I need to call a server-side script, say every x seconds. A second way to get those lines is to get them as a reply to the "add this line" request when someone pressed the send button (or just enter). So my idea was to:
53
4093
by: Jeff | last post by:
In the function below, can size ever be 0 (zero)? char *clc_strdup(const char * CLC_RESTRICT s) { size_t size; char *p; clc_assert_not_null(clc_strdup, s); size = strlen(s) + 1;
56
4799
by: spibou | last post by:
In the statement "a *= expression" is expression assumed to be parenthesized ? For example if I write "a *= b+c" is this the same as "a = a * (b+c)" or "a = a * b+c" ?
2
4284
by: Allan Ebdrup | last post by:
Hi, I'm trying to render a Matrix question in my ASP.Net 2.0 page, A matrix question is a question where you have several options that can all be rated according to several possible ratings (from less to more for example). I have a question object that has two properties that contain the collections Options and Ratings. now I want this kind of layout: --- Rating1 Rating2 Rating3 Option 1 () () ...
3
1261
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 using for its input arg is defined by a public property to this class. My question is, how can I...
0
9619
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10260
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9910
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8933
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6712
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5354
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5482
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4007
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
2
3609
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.