Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 715983
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T05:14:54+00:00 2026-05-14T05:14:54+00:00

I cant believe this, i always throught the below would be concurrency safe. I

  • 0

I cant believe this, i always throught the below would be concurrency safe.

I write to a row in one transaction and i am able to read the dirty value from another transaction/command/connection! Why is this possible (not my main question) isnt this not desired and cause more troubles!?!

Anyways, i expected that once i write to a row nothing else will be able to read to the row until the transaction is finished. And at least if the row can be still read that the clean (original) value will be read. (but maybe that would cause problems as well if the transaction doesnt use the newly commited data from the other transaction when it is ran)

I would like count to == 11. I thought this would be safe in all variants of sql. What can i do to either 1) Not read the dirty value but clean 2) Have that row be locked until the transaction is finished?

    static MySqlConnection MakeConn()
    {
        string connStr = "server=192.168.126.128;user=root;database=TestDB;port=3306;password=a;";
        MySqlConnection conn = new MySqlConnection(connStr);
        conn.Open();
        return conn;
    }
    static Semaphore sem1 = new Semaphore(1, 1);
    static Semaphore sem2 = new Semaphore(1, 1);
    static void Main2()
    {
        Console.WriteLine("Starting Test");

        //
        sem1.WaitOne(); Console.WriteLine("1W");
        sem2.WaitOne(); Console.WriteLine("2W");
        Thread oThread = new Thread(new ThreadStart(fn2));
        oThread.Start();

        var conn = MakeConn();
        var cmd = new MySqlCommand(@"
            CREATE TABLE IF NOT EXISTS Persons
            (
            P_Id int NOT NULL,
            name varchar(255),
            count int,
            PRIMARY KEY (P_Id)
            )", conn);
        cmd.ExecuteNonQuery();

        cmd.CommandText = "delete from Persons; insert into Persons(name, count) VALUES('E', '4');";
        cmd.ExecuteNonQuery();

        cmd.CommandText = "select count from Persons;";
        var count = (int)cmd.ExecuteScalar();

        Console.WriteLine("Finish inserting. v={0}", count);
        sem2.Release(); Console.WriteLine("2R");
        sem1.WaitOne(); Console.WriteLine("1W");


        Console.WriteLine("Starting transaction");
        using (var tns = conn.BeginTransaction())
        {
            cmd.CommandText = "update Persons set count=count+1";
            cmd.ExecuteNonQuery();
            cmd.CommandText = "select count from Persons;";
            count = (int)cmd.ExecuteScalar();
            Console.WriteLine("count is {0}", count);

            sem2.Release(); Console.WriteLine("2R");
            sem1.WaitOne(); Console.WriteLine("1W");

            count += 5; //10

            cmd.CommandText = "update Persons set count=" + count.ToString();
            cmd.ExecuteNonQuery();
            cmd.CommandText = "select count from Persons;";
            count = (int)cmd.ExecuteScalar();
            Console.WriteLine("count is {0}", count);

            tns.Commit();
        }
        Console.WriteLine("finished transaction 1");
        sem2.Release(); Console.WriteLine("2R");
        sem1.WaitOne(); Console.WriteLine("1W");

        cmd.CommandText = "select count from Persons;";
        count = (int)cmd.ExecuteScalar();
        Console.WriteLine("count is {0}", count);

        sem2.Release(); Console.WriteLine("2R");
        //sem1.WaitOne(); Console.WriteLine("1W");

    }
    static void fn2()
    {
        int count;
        Console.WriteLine("Starting thread 2");
        sem2.WaitOne(); Console.WriteLine("1W");
        var conn = MakeConn();
        var cmd = new MySqlCommand("", conn);
        sem1.Release(); Console.WriteLine("1R");
        sem2.WaitOne(); Console.WriteLine("2W");

        using (var tns = conn.BeginTransaction())
        {
            cmd.CommandText = "update Persons set count=count+1";
            cmd.ExecuteNonQuery();
            cmd.CommandText = "select count from Persons;";
            count = (int)cmd.ExecuteScalar();
            Console.WriteLine("count is {0}", count);

            sem1.Release(); Console.WriteLine("1R");
            sem2.WaitOne(); Console.WriteLine("2W");

            tns.Commit();
        }
        Console.WriteLine("finished transaction 2");
        sem1.Release(); Console.WriteLine("1R");
        sem2.WaitOne(); Console.WriteLine("2W");

        cmd.CommandText = "select count from Persons;";
        count = (int)cmd.ExecuteScalar();
        Console.WriteLine("count is {0}", count); //should be 11. 4 + 1x2(one each thread) += 5 from first thread == 11

        sem1.Release(); Console.WriteLine("1R");
    }

console

Starting Test
1W
2W
Starting thread 2
Finish inserting. v=4
2R
1W
1R
1W
Starting transaction
count is 5
2R
2W
count is 6
1R
1W
count is 10
finished transaction 1
2R
2W
finished transaction 2
1R
1W
count is 10
2R
2W
count is 10
1R
  • 1 1 Answer
  • 2 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T05:14:54+00:00Added an answer on May 14, 2026 at 5:14 am

    INNODB tables support transactions, MYISAM doesn’t not, make sure you are creatinng innodb tables.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I cant believe that I haven't been able to find any documentation on this
I cant believe fastest solution for a multilined JLabel is the following one (text
I can't believe this - someone actually created two accounts on my social networking
I can't believe I'm asking this but, is there any way to get Chrome
I can't believe I'm back to this after working with WPF for 3 months
I can't believe I'm still confused about this but, any way, lets finally nail
I can't believe how unbelievably complicated this has been... I have the following XML...
I can't believe I can't work out how to do this but what can
I believe I've done this before, but I can't seem to remember how:( HttpWebRequest
I believe this is a simple fix but so far I can't find it.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.