473,698 Members | 2,551 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 2235
..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
3940
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
1725
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
8712
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
5141
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
3254
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
5763
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
3814
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
8020
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
1907
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
8680
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
8609
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
9030
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8899
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
8871
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...
1
6528
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
4371
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...
1
3052
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
3
2007
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.