c# - FOR UPDATE inside a transaction doesn't lock rows -


i made test select row, created previously, 2 threads:

create table `customers` (   `id` int(11) not null auto_increment,   primary key (`id`),  ) engine=innodb       var t1 = new thread(new threadstart(delegate()     {         using (var conn = new mysqlconnection("server=localhost;database=test;uid=root;pwd=test;"))         {             conn.open();             using (var trans = conn.begintransaction())             {                 using (var cmd = new mysqlcommand("select id customers id = 8534 update;", conn, trans))                 {                     using (var reader = cmd.executereader())                     {                         console.writeline("enter t1: " + reader.read());                         thread.sleep(2000);                         console.writeline("exit t1");                     }                 }             }         };     }));      var t2 = new thread(new threadstart(delegate()     {         using (var conn = new mysqlconnection("server=localhost;database=test;uid=root;pwd=test;"))         {             conn.open();             using (var cmd = new mysqlcommand("select id customers id = 8534", conn))             {                 console.writeline("enter t2: " + cmd.executescalar());                 console.writeline("exit t2");             }         }     }));      t1.start();     thread.sleep(400);     t2.start();      t1.join();     t2.join(); 

and result is:

enter t1: true enter t2: 8534 exit t2 exit t1 

shouldn't update in thread 1 prevent thread 2 read row until releases transaction?

shouldn't for update in thread 1 prevent thread 2 read row until releases transaction?

no.

but prevent thread 2 writing row (or reading for update clause well).

in default transaction isolation level (that repeatable read), select statements not place locks on rows read.

for select statement lock should instruct lock (by using for update, lock in share mode or setting reader's transaction isolation level serializable).

from docs:

consistent read default mode in innodb processes select statements in read committed , repeatable read isolation levels. consistent read not set locks on tables accesses, , therefore other sessions free modify tables @ same time consistent read being performed on table.

and

innodb uses consistent read select in clauses insert ... select, update ... (select), , create table ... select not specify for update or lock in share mode if innodb_locks_unsafe_for_binlog option set , isolation level of transaction not set serializable. thus, no locks set on rows read selected table. otherwise, innodb uses stronger locks , select part acts read committed, each consistent read, within same transaction, sets , reads own fresh snapshot.


Comments

Popular posts from this blog

java - SNMP4J General Variable Binding Error -

windows - Python Service Installation - "Could not find PythonClass entry" -

Determine if a XmlNode is empty or null in C#? -