473,573 Members | 3,126 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

is this okay?

pseudocode:

function generate_unique _id()
{
mkdir("lock");

if successful
{
$id = 10 digit random number;
make sure that $id does not exist in our table
if it does, generate a new one

insert $id into our table;

rmdir("lock");
}
else try mkdir again
}

do you see any race conditions?

this idea could be dangerous if the script died before rmdir("lock")..
any solution?

[I can't use auto-increment .. long story]

Mike

Oct 22 '05 #1
2 1343
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

siliconmike wrote:
mkdir("lock");


Why don't you use the flock() mechanism instead??
- --
- ----------------------------------
Iván Sánchez Ortega -i-punto-sanchez--arroba-mirame-punto-net

Un hombre con pereza es un reloj sin cuerda.- Balmes.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (GNU/Linux)

iD8DBQFDWhrc3jc Q2mg3Pc8RAubuAJ 9UpWw+7wqBftaSQ +Q+QXZ07tC7ZgCf RXI0
hQFGVcJJc7zopnu pH4160dc=
=gGrZ
-----END PGP SIGNATURE-----
Oct 22 '05 #2
On 21 Oct 2005 17:58:41 -0700, "siliconmik e" <si*********@ya hoo.com> wrote:
pseudocode:

function generate_unique _id()
{
$id = 10 digit random number;
make sure that $id does not exist in our table
if it does, generate a new one

insert $id into our table;


You mention a "table", so I assume this is going into a database.

If so, simply add a unique constraint to the column, and leave the locking to
the database.

Don't check if it exists with a SELECT since that introduces a race condition;
attempt the INSERT, and catch the unique key violation if there is one.
--
Andy Hassall :: an**@andyh.co.u k :: http://www.andyh.co.uk
http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool
Oct 22 '05 #3

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

Similar topics

6
6408
by: Joseph | last post by:
hi 1) i plan on having an awt canvas component (to draw graphs) on a JFrame with other swing components..will this be okay? i've read that swing and awt aren't compatible.. 2)Also, if i have a function which simply loops infinately that is part of a form, what happens when a form event (eg button click) occurs? will the event handler...
1
2933
by: Niels Dekker - no reply address | last post by:
The book "C++ Coding Standards" by Herb Sutter and Andrei Alexandrescu warns against potential memory leaks when having multiple calls to operator new within a single statement. (Item 13, page 25.) These leaks would still be there when wrapping the result of each "new" in a temporary smart pointer object. Is there a simular risk when...
14
5405
by: Dave | last post by:
My web site is not particularly theme-based, but it contains an Art Gallery I'd like to display in a different perspective. I would like to horizontally scroll it, rather than vertically - as if the viewer is virtually "walking" through the gallery. I'm just attempting this to have this part of the page be different. Is horizontal scrolling...
1
1635
by: TheKval | last post by:
I'm trying to pull records on a Macola table linked to Access. If enter a value in the criteria field of the query form, the quer executes okay. If I change the criteria to a parameter, ala, , I get the ODBC... call failed error. The data ite is a 3 character text field. I've tried to us the parameter AFTE trimming the table field. no...
12
2342
by: Michael B Allen | last post by:
Is it legit to modify static data like the following code? #include <stdlib.h> #include <stdio.h> struct tbl { int i; char *s; };
1
1278
by: Adam Smith | last post by:
Hello. I'm wondering if it's okay to construct the same object within static constructor: public class Proc { public Dataholder d;//some other class //I've created static ArrayList al; int val;
6
2229
by: Patrick Coghlan | last post by:
I want to create about 4 forms with the same dimensions and background colours, similar to the forms one has to traverse when installing various software packages. I'm using Visual Studio and C++ (changing to C# soon) and can create separate forms, but I really just want to clone the first one, have the user select different options on each...
11
2247
by: Madison Kelly | last post by:
Hi all, I am new to the list and I didn't want to seem rude at all so I wanted to ask if this was okay first. I have a program I have written in perl which uses a postgresSQL database as the backend. The program works but the performance is really bad. I have been reading as much as I can on optimizing performance but still it isn't very...
35
3001
by: Frederick Gotham | last post by:
(Before I begin, please don't suggest to me to use "std::vector" rather than actual arrays.) I understand that an object can have resources (e.g. dynamically allocated memory), and so we have to copy an object properly via copy- construction rather than using memcpy. However, is it okay to move an object manually (i.e. by using memcpy or...
13
1865
by: hanaa | last post by:
Hello, I am making an interface to a database(MySQL) of questions. Is it possible to connect to the database using mysql_connect() just once, in a session and use the connection thereafter in the following pages and until the user logs out? If it is, Is it okay to do so.. I just read that the session files are stored in some directory on the...
0
8016
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
8200
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7773
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6411
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
5584
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
5289
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
3731
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2197
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
1297
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.