简体   繁体   中英

Are two UPDATE statements in same BEGIN-END block executed at the exact same time?

If I have a table as follows:

CID COLUMN1
1   Joe
2   John

And I run the following code below. Could a Select statement be executed (at some inopportune precise millisecond about half way through the running of my code) that would return COLUMN1 = John for BOTH rows in the table? Or would Oracle run both update statements in my code "together" so to speak, so this could NEVER happen.

string sqlStr = @"Begin
              Update TABLE1 set COLUMN1 = 'John' where CID = 1;
              Update TABLE1 set COLUMN1 = 'Joe' where CID = 2;
              End;";

OracleConnection conn = new OracleConnection(oracle_connstr);
OracleCommand cmd = new OracleCommand(sqlStr, conn);
try
{
        conn.Open();
        cmd.ExecuteNonQuery();
}
catch (Exception ex)
{

}
finally
{
        if (cmd != null) cmd.Dispose();
        if (conn != null) conn.Dispose();
}

The two UPDATE statements will be run sequentially. So the second UPDATE won't run until the first UPDATE finishes.

Assuming that the two statements are part of the same transaction (which they would be in this example), other sessions are prevented from reading the intermediate state where both rows have a value of "John" by the transaction isolation level. Oracle does not allow dirty reads. Normally, everything in Oracle runs in the read committed transaction isolation level. That means that other sessions would see the values prior to your transaction starting until your transaction committed at which point both statements (and anything else that is part of the transaction) would be complete.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM