<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: SELECT LOCK IN SHARE MODE  and FOR UPDATE</title>
	<atom:link href="http://www.mysqlperformanceblog.com/2006/08/06/select-lock-in-share-mode-and-for-update/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.mysqlperformanceblog.com/2006/08/06/select-lock-in-share-mode-and-for-update/</link>
	<description>Percona&#039;s MySQL &#38; InnoDB performance and scalability blog</description>
	<lastBuildDate>Sat, 11 Feb 2012 16:45:54 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
	<item>
		<title>By: Baron Schwartz</title>
		<link>http://www.mysqlperformanceblog.com/2006/08/06/select-lock-in-share-mode-and-for-update/comment-page-2/#comment-825908</link>
		<dc:creator>Baron Schwartz</dc:creator>
		<pubDate>Mon, 26 Sep 2011 16:31:30 +0000</pubDate>
		<guid isPermaLink="false">http://www.mysqlperformanceblog.com/2006/08/06/select-lock-in-share-mode-and-for-update/#comment-825908</guid>
		<description>Mikhail, please use our forums for asking questions such as this.</description>
		<content:encoded><![CDATA[<p>Mikhail, please use our forums for asking questions such as this.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Mikhail</title>
		<link>http://www.mysqlperformanceblog.com/2006/08/06/select-lock-in-share-mode-and-for-update/comment-page-1/#comment-825799</link>
		<dc:creator>Mikhail</dc:creator>
		<pubDate>Mon, 26 Sep 2011 11:23:02 +0000</pubDate>
		<guid isPermaLink="false">http://www.mysqlperformanceblog.com/2006/08/06/select-lock-in-share-mode-and-for-update/#comment-825799</guid>
		<description>Hi.

I need to select count of specific rows from table, then insert a new record with one field calculated using that value (something like auto_increment but for groups of records). Following queries doesn&#039;t always work:

T1: BEGIN;
T2: BEGIN;

T1: SELECT COUNT(*) INTO @for_later_insert_t1 FROM table1 WHERE field1 = &#039;not_existing_const&#039; AND field2 like &#039;const_prefix_%&#039; FOR UPDATE; -- (table1 has a key - field1, field2) selects 0.

T2: SELECT COUNT(*) INTO @for_later_insert_t2 FROM table1 WHERE field1 = &#039;not_existing_const&#039; AND field2 like &#039;const_prefix_%&#039; FOR UPDATE; -- waits.

T1: INSERT INTO table1 SET field1 = &#039;not_existing_const&#039;, field2 = &#039;const_prefix_1&#039;, field3 = @for_later_insert_t1;
T1: COMMIT;

T2: -- unblocks, @for_later_insert_t2 == 1
T2: INSERT INTO table1 SET field1 = &#039;not_existing_const&#039;, field2 = &#039;const_prefix_1&#039;, field3 = @for_later_insert_t2;
T2: COMMIT;

They doesn&#039;t work:
 * if field2 like &#039;const_prefix_%&#039; is replaced with field2 = &#039;const_prefix_1&#039; (SELECT ... FOR UPDATE in that case isn&#039;t locked, and insert causes deadlock).
 * If the SELECT ... FOR UPDATE in one transaction is using different key.

However those queries work if records with field1 = &#039;not_existing_const&#039;, field2 = &#039;const_prefix_1&#039; already exist.
Why deadlock occurs if there are no such records? 
Is it safe to retry the failed transaction? Or transaction is not meant to solve such problems and better use GET_LOCK()/RELEASE_LOCK()?</description>
		<content:encoded><![CDATA[<p>Hi.</p>
<p>I need to select count of specific rows from table, then insert a new record with one field calculated using that value (something like auto_increment but for groups of records). Following queries doesn&#8217;t always work:</p>
<p>T1: BEGIN;<br />
T2: BEGIN;</p>
<p>T1: SELECT COUNT(*) INTO @for_later_insert_t1 FROM table1 WHERE field1 = &#8216;not_existing_const&#8217; AND field2 like &#8216;const_prefix_%&#8217; FOR UPDATE; &#8212; (table1 has a key &#8211; field1, field2) selects 0.</p>
<p>T2: SELECT COUNT(*) INTO @for_later_insert_t2 FROM table1 WHERE field1 = &#8216;not_existing_const&#8217; AND field2 like &#8216;const_prefix_%&#8217; FOR UPDATE; &#8212; waits.</p>
<p>T1: INSERT INTO table1 SET field1 = &#8216;not_existing_const&#8217;, field2 = &#8216;const_prefix_1&#8242;, field3 = @for_later_insert_t1;<br />
T1: COMMIT;</p>
<p>T2: &#8212; unblocks, @for_later_insert_t2 == 1<br />
T2: INSERT INTO table1 SET field1 = &#8216;not_existing_const&#8217;, field2 = &#8216;const_prefix_1&#8242;, field3 = @for_later_insert_t2;<br />
T2: COMMIT;</p>
<p>They doesn&#8217;t work:<br />
 * if field2 like &#8216;const_prefix_%&#8217; is replaced with field2 = &#8216;const_prefix_1&#8242; (SELECT &#8230; FOR UPDATE in that case isn&#8217;t locked, and insert causes deadlock).<br />
 * If the SELECT &#8230; FOR UPDATE in one transaction is using different key.</p>
<p>However those queries work if records with field1 = &#8216;not_existing_const&#8217;, field2 = &#8216;const_prefix_1&#8242; already exist.<br />
Why deadlock occurs if there are no such records?<br />
Is it safe to retry the failed transaction? Or transaction is not meant to solve such problems and better use GET_LOCK()/RELEASE_LOCK()?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jan</title>
		<link>http://www.mysqlperformanceblog.com/2006/08/06/select-lock-in-share-mode-and-for-update/comment-page-1/#comment-716935</link>
		<dc:creator>Jan</dc:creator>
		<pubDate>Sun, 31 Jan 2010 15:09:33 +0000</pubDate>
		<guid isPermaLink="false">http://www.mysqlperformanceblog.com/2006/08/06/select-lock-in-share-mode-and-for-update/#comment-716935</guid>
		<description>another problem i encounter


         	$this-&gt;db-&gt;query(&#039;SET AUTOCOMMIT=0&#039;);	 		
		
		$this-&gt;db-&gt;query(&quot;LOCK TABLES hotels READ, location WRITE&quot;);
		
		$this-&gt;db-&gt;query(&quot;SELECT hotel_id FROM hotels WHERE hotel_id = 1 &quot;);
		$query = $this-&gt;db-&gt;get();
				
		$this-&gt;db-&gt;query(&quot;UPDATE location SET location_id = 5 WHERE location_id = 1&quot;);
				
		$this-&gt;db-&gt;query(&quot;UNLOCK TABLES&quot;);

i can&#039;t access to my hotels table..in my SELECT statement..please help to resolve this one..</description>
		<content:encoded><![CDATA[<p>another problem i encounter</p>
<p>         	$this-&gt;db-&gt;query(&#8216;SET AUTOCOMMIT=0&#8242;);	 		</p>
<p>		$this-&gt;db-&gt;query(&#8220;LOCK TABLES hotels READ, location WRITE&#8221;);</p>
<p>		$this-&gt;db-&gt;query(&#8220;SELECT hotel_id FROM hotels WHERE hotel_id = 1 &#8220;);<br />
		$query = $this-&gt;db-&gt;get();</p>
<p>		$this-&gt;db-&gt;query(&#8220;UPDATE location SET location_id = 5 WHERE location_id = 1&#8243;);</p>
<p>		$this-&gt;db-&gt;query(&#8220;UNLOCK TABLES&#8221;);</p>
<p>i can&#8217;t access to my hotels table..in my SELECT statement..please help to resolve this one..</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jan</title>
		<link>http://www.mysqlperformanceblog.com/2006/08/06/select-lock-in-share-mode-and-for-update/comment-page-1/#comment-716754</link>
		<dc:creator>Jan</dc:creator>
		<pubDate>Sun, 31 Jan 2010 10:16:38 +0000</pubDate>
		<guid isPermaLink="false">http://www.mysqlperformanceblog.com/2006/08/06/select-lock-in-share-mode-and-for-update/#comment-716754</guid>
		<description>i got it...it says their 

LOCK TABLES ... IN [EXCLUSIVE&#124;SHARE] MODE syntax does not exist in 5.1

the version im using now is 5.1 :)...what a mess im doing with my code...its not the code is the problem..my version...lol..:)</description>
		<content:encoded><![CDATA[<p>i got it&#8230;it says their </p>
<p>LOCK TABLES &#8230; IN [EXCLUSIVE|SHARE] MODE syntax does not exist in 5.1</p>
<p>the version im using now is 5.1 <img src='http://www.mysqlperformanceblog.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> &#8230;what a mess im doing with my code&#8230;its not the code is the problem..my version&#8230;lol..:)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jan</title>
		<link>http://www.mysqlperformanceblog.com/2006/08/06/select-lock-in-share-mode-and-for-update/comment-page-1/#comment-716752</link>
		<dc:creator>Jan</dc:creator>
		<pubDate>Sun, 31 Jan 2010 10:10:15 +0000</pubDate>
		<guid isPermaLink="false">http://www.mysqlperformanceblog.com/2006/08/06/select-lock-in-share-mode-and-for-update/#comment-716752</guid>
		<description>darseq,,

i&#039;ve tried EXCLUSIVE and SHARED LOCK , and i found out..it wont work..i kinda get this error [#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near &#039;IN SHARE MODE&#039; at line 1]

my syntax is correct i use this one...LOCK TABLE hotels IN SHARE MODE and LOCK TABLE hotels IN EXCLUSIVE MODE...

..i test my code in xampp...try to query it.

and when i replace it with this one ..LOCK TABLE hotels WRITE..it works ...also with READ..so strange....

i&#039;ve read one of this article it says

so the transactional locks are converted into
standard READ&#124;WRITE locks with a warning and then work as expected from the conventional
locks (or, if sql_mode is set to STRICT*, an attempt to acquire a transactional lock
returns an error).

heres the link if you want to see it..
http://bugs.mysql.com/bug.php?id=46663</description>
		<content:encoded><![CDATA[<p>darseq,,</p>
<p>i&#8217;ve tried EXCLUSIVE and SHARED LOCK , and i found out..it wont work..i kinda get this error [#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'IN SHARE MODE' at line 1]</p>
<p>my syntax is correct i use this one&#8230;LOCK TABLE hotels IN SHARE MODE and LOCK TABLE hotels IN EXCLUSIVE MODE&#8230;</p>
<p>..i test my code in xampp&#8230;try to query it.</p>
<p>and when i replace it with this one ..LOCK TABLE hotels WRITE..it works &#8230;also with READ..so strange&#8230;.</p>
<p>i&#8217;ve read one of this article it says</p>
<p>so the transactional locks are converted into<br />
standard READ|WRITE locks with a warning and then work as expected from the conventional<br />
locks (or, if sql_mode is set to STRICT*, an attempt to acquire a transactional lock<br />
returns an error).</p>
<p>heres the link if you want to see it..<br />
<a href="http://bugs.mysql.com/bug.php?id=46663" rel="nofollow">http://bugs.mysql.com/bug.php?id=46663</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: darseq</title>
		<link>http://www.mysqlperformanceblog.com/2006/08/06/select-lock-in-share-mode-and-for-update/comment-page-1/#comment-716264</link>
		<dc:creator>darseq</dc:creator>
		<pubDate>Sat, 30 Jan 2010 09:57:03 +0000</pubDate>
		<guid isPermaLink="false">http://www.mysqlperformanceblog.com/2006/08/06/select-lock-in-share-mode-and-for-update/#comment-716264</guid>
		<description>No problem, Keep in mind also that the
transaction isolation level must be set
to at least repeatable read to ensure
serializability. Lower levels allow for
more concurrency but then you will have
to check whether your transactions keep
the database in a consistent state. What
is considered &#039;consistent&#039; is defined by
you as a programmer.</description>
		<content:encoded><![CDATA[<p>No problem, Keep in mind also that the<br />
transaction isolation level must be set<br />
to at least repeatable read to ensure<br />
serializability. Lower levels allow for<br />
more concurrency but then you will have<br />
to check whether your transactions keep<br />
the database in a consistent state. What<br />
is considered &#8216;consistent&#8217; is defined by<br />
you as a programmer.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: jan</title>
		<link>http://www.mysqlperformanceblog.com/2006/08/06/select-lock-in-share-mode-and-for-update/comment-page-1/#comment-715565</link>
		<dc:creator>jan</dc:creator>
		<pubDate>Thu, 28 Jan 2010 23:49:33 +0000</pubDate>
		<guid isPermaLink="false">http://www.mysqlperformanceblog.com/2006/08/06/select-lock-in-share-mode-and-for-update/#comment-715565</guid>
		<description>i appreaciate so much your help darseq.:)


i&#039;ll now continue my coding ..i&#039;ve understand it clearly now...:)...

if i will encounter a problem..i will post again here..:)...

hope it would be a success^_^..</description>
		<content:encoded><![CDATA[<p>i appreaciate so much your help darseq.:)</p>
<p>i&#8217;ll now continue my coding ..i&#8217;ve understand it clearly now&#8230;:)&#8230;</p>
<p>if i will encounter a problem..i will post again here..:)&#8230;</p>
<p>hope it would be a success^_^..</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: darseq</title>
		<link>http://www.mysqlperformanceblog.com/2006/08/06/select-lock-in-share-mode-and-for-update/comment-page-1/#comment-715366</link>
		<dc:creator>darseq</dc:creator>
		<pubDate>Thu, 28 Jan 2010 13:25:01 +0000</pubDate>
		<guid isPermaLink="false">http://www.mysqlperformanceblog.com/2006/08/06/select-lock-in-share-mode-and-for-update/#comment-715366</guid>
		<description>So the practical explaination is:

SELECT ... FOR UPDATE sets a XLOCK on the accessed record.
SELECT ... LOCK IN SHARE MODE sets a SLOCK on the accessed record.

In both cases you will get serializability, but to improve
concurrency, one should only set a XLOCK if the transaction
actually updates/writes to the record.</description>
		<content:encoded><![CDATA[<p>So the practical explaination is:</p>
<p>SELECT &#8230; FOR UPDATE sets a XLOCK on the accessed record.<br />
SELECT &#8230; LOCK IN SHARE MODE sets a SLOCK on the accessed record.</p>
<p>In both cases you will get serializability, but to improve<br />
concurrency, one should only set a XLOCK if the transaction<br />
actually updates/writes to the record.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: darseq</title>
		<link>http://www.mysqlperformanceblog.com/2006/08/06/select-lock-in-share-mode-and-for-update/comment-page-1/#comment-715357</link>
		<dc:creator>darseq</dc:creator>
		<pubDate>Thu, 28 Jan 2010 13:04:11 +0000</pubDate>
		<guid isPermaLink="false">http://www.mysqlperformanceblog.com/2006/08/06/select-lock-in-share-mode-and-for-update/#comment-715357</guid>
		<description>Locks can be set on multiple levels of granularity.
If one chooses to lock tables, all other users are
blocked for as long as the transaction takes to
finish its job. If one chooses to lock records
instead, other records in the same table can still
be accessed, but access time may be slower (the
dbms requires more resources to lock records than
to lock a single table).

To summarize: If you transactions are fast and no
user interaction happens in between, it is probably
best to quickly lock the entire table and do the
work. If transactions may be slow or user dialogs
are placed in between, you may be better of using
records locks. If using records locks, than innodb
knows about two kind of locks: Exclusive and shared.
Last time I checked, innodb does not support record
&#039;update&#039; locks (which I think is kind of flawed).</description>
		<content:encoded><![CDATA[<p>Locks can be set on multiple levels of granularity.<br />
If one chooses to lock tables, all other users are<br />
blocked for as long as the transaction takes to<br />
finish its job. If one chooses to lock records<br />
instead, other records in the same table can still<br />
be accessed, but access time may be slower (the<br />
dbms requires more resources to lock records than<br />
to lock a single table).</p>
<p>To summarize: If you transactions are fast and no<br />
user interaction happens in between, it is probably<br />
best to quickly lock the entire table and do the<br />
work. If transactions may be slow or user dialogs<br />
are placed in between, you may be better of using<br />
records locks. If using records locks, than innodb<br />
knows about two kind of locks: Exclusive and shared.<br />
Last time I checked, innodb does not support record<br />
&#8216;update&#8217; locks (which I think is kind of flawed).</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: jan</title>
		<link>http://www.mysqlperformanceblog.com/2006/08/06/select-lock-in-share-mode-and-for-update/comment-page-1/#comment-715176</link>
		<dc:creator>jan</dc:creator>
		<pubDate>Thu, 28 Jan 2010 04:42:32 +0000</pubDate>
		<guid isPermaLink="false">http://www.mysqlperformanceblog.com/2006/08/06/select-lock-in-share-mode-and-for-update/#comment-715176</guid>
		<description>there is no ULOCK in innodb...:(...

okay..i&#039;ll just read more about lock compatibility...so it means to say ill be using shared/exclusive rather than write or read...

is it ok ill put the lock on the query for example this one

SELECT pep_id, pep_name FROM person WHERE id= $id LOCK IN SHARE MODE;

OR ill just do it like this

function InsertData {

  $query = &quot;LOCK TABLE person IN SHARE MODE&quot;;

  $query = INSERT INTO person(pep_id, pep_name) VALUES (&#039;1&#039;,&#039;John&#039;);
}

Thanks Again for the reply Darseq:)</description>
		<content:encoded><![CDATA[<p>there is no ULOCK in innodb&#8230;:(&#8230;</p>
<p>okay..i&#8217;ll just read more about lock compatibility&#8230;so it means to say ill be using shared/exclusive rather than write or read&#8230;</p>
<p>is it ok ill put the lock on the query for example this one</p>
<p>SELECT pep_id, pep_name FROM person WHERE id= $id LOCK IN SHARE MODE;</p>
<p>OR ill just do it like this</p>
<p>function InsertData {</p>
<p>  $query = &#8220;LOCK TABLE person IN SHARE MODE&#8221;;</p>
<p>  $query = INSERT INTO person(pep_id, pep_name) VALUES (&#8217;1&#8242;,&#8217;John&#8217;);<br />
}</p>
<p>Thanks Again for the reply Darseq:)</p>
]]></content:encoded>
	</item>
</channel>
</rss>

