473,738 Members | 2,492 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Race condition when inserting data / MySQL 4.0+, MyISAM tables

Hello

I use a table to cache some informations which need lots of resources to
be composed. The first time the info is needed, it will be composed and
written to the cache table ($db in the example is a PEAR DB object; the
question is the same for other ways of accessing the database):

// Retrieve info if present
$details = $db->getOne("SELE CT contents FROM cache WHERE id=".$id." AND
info='details'" );
// If not present, compose info and write it to the database
if (!is_string($de tails) || trim($details) == '') {
$details = $this->compose_detail s();
$data = array('id' =$id, 'info' ='details', 'contents' =$details);
$db->query("INSER T INTO cache (id, info, contents) VALUES (".$id.",
'details', '".$details."') ");
}

Now I encountered that if several users call a page at the same time
after the cache was flushed, it is possible that between the first line
and the INSERT query the info was entered by another user. This results
in a duplicate key error.

Now I wonder which is the best way to handle this. I see various approaches:
- Use ON DUPLICATE KEY UPDATE (which might fail if MySQL 4.0 is used)
- Try to write some kind of locking mechanism
- Suppress the error message for this special case
- Remove the primary key from the cache table (as it is flushed whenever
items are administrated, duplicate entries might not be a big problem)

Which is the recommended way to handle this? I guess, as MySQL 4.0 and
MyISAM tables are a quite common configuration, there must be some
common practice about this, but I did not find anything by googling...

Thanks for comments!
Markus
Jul 13 '07 #1
4 2241
..oO(Markus)
>[handling a race condition]

Which is the recommended way to handle this? I guess, as MySQL 4.0 and
MyISAM tables are a quite common configuration, there must be some
common practice about this, but I did not find anything by googling...
Is it really necessary to keep the script compatible to outdated
versions of MySQL? The current stable is 5, the old 4.0 is not even
available for download anymore. If it still has to work on a series 4
server, then I would make at least 4.1 a requirement.

I would even drop support for MyISAM and use InnoDB instead, but that's
just a personal decision. I simply need the features of a modern engine,
so I can't take care of older installations. The same goes for PHP for
example -- my scripts require PHP 5.2, PHP 4 is dead (it's official
now). At some point you simply have to draw the line.

Micha
Jul 13 '07 #2
Michael Fesser schrieb:
.oO(Markus)
>[handling a race condition]

Which is the recommended way to handle this? I guess, as MySQL 4.0 and
MyISAM tables are a quite common configuration, there must be some
common practice about this, but I did not find anything by googling...

Is it really necessary to keep the script compatible to outdated
versions of MySQL? The current stable is 5, the old 4.0 is not even
available for download anymore. If it still has to work on a series 4
server, then I would make at least 4.1 a requirement.

I would even drop support for MyISAM and use InnoDB instead, but that's
just a personal decision. I simply need the features of a modern engine,
so I can't take care of older installations. The same goes for PHP for
example -- my scripts require PHP 5.2, PHP 4 is dead (it's official
now). At some point you simply have to draw the line.
Thank you, I see the point; anyway the application is supposed to work
with shared hosting, where MySQL 4.0 is still around. Though, dropping
4.0 support is actually an issue - also because it is not natively
supporting UTF-8. And I use MyISAM simply because it is the only engine
that provides fulltext search.
Jul 13 '07 #3
..oO(Markus)
>And I use MyISAM simply because it is the only engine
that provides fulltext search.
OK, that's one point for MyISAM. But my own focus is on data integrity
(most important: foreign keys and transactions), so that's not an issue
here.

Micha
Jul 13 '07 #4
With MyISAM, you can do one of the methods you described or use LOCK
TABLES.

In your case I would do ON DUPLICATE KEY UPDATE or remove the unique
constraint on the id.

Finally, if you need to prevent more than one thread from executing
the generate function, you can use a semaphore with the key being your
id.

On Jul 13, 10:28 am, Markus <derernst@NO#SP #AMgmx.chwrote:
Hello

I use a table to cache some informations which need lots of resources to
be composed. The first time the info is needed, it will be composed and
written to the cache table ($db in the example is a PEAR DB object; the
question is the same for other ways of accessing the database):

// Retrieve info if present
$details = $db->getOne("SELE CT contents FROM cache WHERE id=".$id." AND
info='details'" );
// If not present, compose info and write it to the database
if (!is_string($de tails) || trim($details) == '') {
$details = $this->compose_detail s();
$data = array('id' =$id, 'info' ='details', 'contents' =$details);
$db->query("INSER T INTO cache (id, info, contents) VALUES (".$id.",
'details', '".$details."') ");

}

Now I encountered that if several users call a page at the same time
after the cache was flushed, it is possible that between the first line
and the INSERT query the info was entered by another user. This results
in a duplicate key error.

Now I wonder which is the best way to handle this. I see various approaches:
- Use ON DUPLICATE KEY UPDATE (which might fail if MySQL 4.0 is used)
- Try to write some kind of locking mechanism
- Suppress the error message for this special case
- Remove the primary key from the cache table (as it is flushed whenever
items are administrated, duplicate entries might not be a big problem)

Which is the recommended way to handle this? I guess, as MySQL 4.0 and
MyISAM tables are a quite common configuration, there must be some
common practice about this, but I did not find anything by googling...

Thanks for comments!
Markus

Jul 14 '07 #5

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

Similar topics

2
3944
by: Simon | last post by:
Hi, I am having a little problem with my PHP - MySQl code, I have two tables (shown below) and I am trying populate a template page with data from both. <disclaimer>Now I would like to say my skills, especially with MySQL are rudimentary</disclaimer> However my code (link below) fails, the nested database call does not return any data and this has me stumped. Any help will be much appreciated. Many thanks in advance
0
1728
by: Luc Foisy | last post by:
Last week many of our server and client servers had a power problem. Not = quite sure how the servers were handled, wasn't on site, but I don't = think some of these servers got shut down gracefully. but anyways that = shouldn't matter to my question I ran myisamchk on the data directories and I get a large report = containing things such as myisamchk: MyISAM file /usr/data/mysql/qbslive/MANIFESTSPOOL.MYI myisamchk: warning: 1 clients...
3
8717
by: saracen44 | last post by:
Hi I have MyISAM tables When I'm deleting parent I want to delete children in the same time. How can I do It? What are possibilities? Thanks
8
5146
by: wlcna | last post by:
mysql v4.0.16: I had been using mysql with innodb and thought that was fine, until i used it for something requiring a few - perhaps slightly involved - joins, and have now seen the performance become totally unacceptable. I have a query that takes over 35 seconds using mysql and innodb, for reasons that are completely a mystery to me, in a result set consisting of only a handful of items.
1
3255
by: Mark Everett | last post by:
Hi, I am currently running out of space on one of my database servers. Is it possible to move the relevant files for tables onto another drive and instuct MySql to use both folders for it's data? So basically can you have mulitple data folders setup and if so how is this configured? On another note I could delete some of the older data but the database
3
5768
by: eieiohh | last post by:
MySQL 3.23.49 PHP 4.3.8 Apache 2.0.51 Hi All! Newbie.. I had a CRM Open Source application installed and running. Windows Xp crashed. I was able to copy the contents of the entire hard drive onto a USB External Hard Drive. I have to assume I also copied the data. I
2
3817
by: saran | last post by:
I am having a problem with MySQL consuming a lot of memory and eventually throwing an Out of Memory error and restarting itself. The symptoms are that swap usage continues to rise until some breaking point. The application is a typical web application w/ 2 web servers running Apache/Tomcat connecting to a dedicated DB server running only MySQL. This seems to occur as a result of running many statements in a single transaction, both...
1
8023
by: Man-wai Chang | last post by:
I have a *damaged* MyISAM stock_take_mst.frm in /var/lib/mysql/abc. And I would like to move it to /var/lib/mysql/test and experiment with it. Is there a MySQL command to detach and attach a table? -- iTech Consulting Co., Ltd. Expert of ePOS solutions Website: http://www.itech.com.hk (IE only) Tel: (852)2325 3883 Fax: (852)2325 8288
3
1908
by: ScarletPimpernal | last post by:
Here are my Storage Engine Status. mysql> SHOW ENGINES; +------------+---------+----------------------------------------------------------------+ | Engine | Support | Comment | +------------+---------+----------------------------------------------------------------+ | MyISAM | DEFAULT | Default engine as of MySQL 3.23 with great performance | | MEMORY | YES |...
0
8968
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
8787
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9473
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...
1
9259
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
8208
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...
1
6750
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6053
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();...
2
2744
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2193
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.