Hey, not sure if anyone is interested in this... but I didn't want to ask my host to give me lock tables permission, so I instead wrote an atomic db_next_id replacement that does without it....

Enjoy!

function db_next_id($name) {
	$name = db_prefix_tables($name);
	$result = db_query("update {sequences} set id = last_insert_id(id+1) where name = '%s'", $name);

	if(!db_affected_rows($result)) {
		db_query("insert into {sequences} (id, name) values (0, '%s')", $name);	
		db_query("update {sequences} set id = last_insert_id(id+1) where name = '%s'", $name);
	}
	return mysql_insert_id();
}

Nick.

Comments

smoogle’s picture

You really shouldn't just outright reply like that.

My solution is completely concurrency safe and doesn't involve a silly loop.

paddy_deburca’s picture

The entire post is interesting and it shows that there are a lot of people with similar problems and a lot of people thinking of workable solutions.

In the comment http://drupal.org/node/1190#comment-55811 killes explains that the LOCK statement is there for a good reason and that replacing it may work on low-traffic websites.

Also in the comment http://drupal.org/node/1190#comment-57331 killes mentions a possible alternative that is currently being discussed on the devel list.

I too suffer form this problem with Drupal. This is why I am interested in getting a working solution. I have one that works for me - but it is a low-traffic website.

Paddy.

http://deburca.org, and http://amadain.net