// Collection of code snippets by Arne Vajhøj // (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) using System; using System.Data; using System.Threading; using MySql.Data.MySqlClient; namespace E { public class Test { private const int NINS = 1000; private IsolationLevel isolvl; public Test(IsolationLevel isolvl) { this.isolvl = isolvl; } public void Run() { using(MySqlConnection con = new MySqlConnection("Database=TestInnoDB;Data Source=localhost;User Id=;Password=")) { con.Open(); int retries = 0; for(int i = 0; i < NINS; i++) { MySqlTransaction tx = con.BeginTransaction(isolvl); MySqlCommand sel = new MySqlCommand("SELECT * FROM tt", con); sel.Transaction = tx; MySqlDataReader rdr = sel.ExecuteReader(); while(rdr.Read()) { // nothing; } rdr.Close(); MySqlCommand ins = new MySqlCommand("INSERT INTO tt(f2) VALUES('X')", con); ins.Transaction = tx; bool done = false; while(!done) { try { ins.ExecuteNonQuery(); done = true; } catch (MySqlException) { retries++; } } tx.Commit(); } Console.WriteLine("retries=" + retries); } } } public class Program { private const int NTHR = 5; public static void TestLevel(IsolationLevel isolvl) { using(MySqlConnection con = new MySqlConnection("Database=TestInnoDB;Data Source=localhost;User Id=;Password=")) { con.Open(); MySqlCommand cre = new MySqlCommand("CREATE TABLE tt (f1 INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY, f2 VARCHAR(10)) TYPE=INNODB", con); cre.ExecuteNonQuery(); } DateTime dt1 = DateTime.Now; Test[] tst = new Test[NTHR]; Thread[] thr = new Thread[NTHR]; for(int i = 0; i < thr.Length; i++) { tst[i] = new Test(isolvl); thr[i] = new Thread(new ThreadStart(tst[i].Run)); } for(int i = 0; i < thr.Length; i++) { thr[i].Start(); } for(int i = 0; i < thr.Length; i++) { thr[i].Join(); } DateTime dt2 = DateTime.Now; Console.WriteLine(isolvl + " : " + (dt2 - dt1).TotalSeconds); using(MySqlConnection con = new MySqlConnection("Database=TestInnoDB;Data Source=localhost;User Id=;Password=")) { con.Open(); MySqlCommand drp = new MySqlCommand("DROP TABLE tt", con); drp.ExecuteNonQuery(); } } public static void Main(string[] args) { TestLevel(IsolationLevel.RepeatableRead); TestLevel(IsolationLevel.Serializable); } } }