How to implement a transaction in .NET (2)

zhaozj2021-02-17  51

Concurrent problem

If there is no lock and multiple users accesses a database, problems may occur when their transactions use the same data simultaneously. Concurrency problems include: Loss or override updates, unrecognized correlation (dirty reading), inconsistent analysis (non-repetitive reading), phantom read. But how to avoid the problem of dirty reading when data reading? Here you briefly introduce:

In the SQL Server database, four isolation levels are provided: read, read, readable, can read. These four isolation levels ensure concurrent data integrity varying degrees:

Isolation level

Dirty

Do not read

Magic image

Not submitted

Yes

Yes

Yes

Read

no

Yes

Yes

Repeatable read

no

no

Yes

Cultural reading

no

no

no

The second item is the default, and in the .NET framework, the isolation level of the transaction is also supported. We can achieve it via System.Data.isolationledLevel: Public Virtual IsolationLevel IsolationLevel {Get;}

 The meaning of its members and corresponding as follows:

Member name

Description

value

Chaos

Supported by .NET Framework.

Unable to rewrite the hanging changes in a higher level of affairs.

16

Readcommitted

Supported by .NET Framework.

The shared lock is kept when you are reading data to avoid dirty reading, but data can be changed before the end of the transaction, resulting in unresentible read or phantom data.

4096

ReadunCommitted

Supported by .NET Framework.

You can read dirty, meaning, do not release shared lock, do not accept exclusive locks.

256

REPEATABLEREAD

Supported by .NET Framework.

Place the lock on all data used in the query to prevent other users from updating this data. Prevent non-repeatable reading, but there can be an illusion line.

65536

Serializable

Supported by .NET Framework.

Place the range lock on the DataSet to prevent you from updating the row by other users before the transaction is completed or inserted into the data set.

1048576

Unspecified

Supported by .NET Framework.

Isolated from the specified isolation level is used, but this level cannot be determined.

-1

 

Obviously, the four isolation levels of the database are mapped here. Here, the ISOLTRANSACTION and the ISOLATIONLEVEL default value of transactions such as OLEDBTRANSACTION are readcommitted. So how do we use it? You can use the following method:

Trans = cnnorthwind.begintransaction (_ISOLATIONLEVEL.SERIALIZABLE)

转载请注明原文地址:https://www.9cbs.com/read-29053.html

New Post(0)