gpt4 book ai didi

servicestack - ServiceStack.OrmLite是否支持乐观并发

转载 作者:行者123 更新时间:2023-12-02 03:45:35 28 4
gpt4 key购买 nike

我很惊讶地发现没有关于这个主题的文档,有人知道 OrmLite 是否支持开放式并发吗?欢迎提供任何文档或示例引用。

最佳答案

这真的不取决于 OrmLite。这取决于隔离级别和您的数据库设置。

例子:

using (var trans = Db.BeginTransaction(IsolationLevel.ReadCommitted) as SqlTransaction)
{
try
{
//do something
trans.Commit();
}
catch (Exception ex)
{
trans.Rollback();
throw ex;
}
}
}

来自 MS SQL Sever Docs

READ COMMITTED
Specifies that statements cannot read data that has been modified but not committed by other transactions. This prevents dirty reads. Data can be changed by other transactions between individual statements within the current transaction, resulting in nonrepeatable reads or phantom data. This option is the SQL Server default. The behavior of READ COMMITTED depends on the setting of the READ_COMMITTED_SNAPSHOT database option:

If READ_COMMITTED_SNAPSHOT is set to ON, the Database Engine uses row versioning to present each statement with a transactionally consistent snapshot of the data as it existed at the start of the statement. Locks are not used to protect the data from updates by other transactions.

或者,您可以使用 BeginTransaction(IsolationLevel.Snapshot) 并获得乐观并发,因为您可以避免锁定,但 SQL Server 使用 rowversion在提交之前避免修改在当前事务之外修改的数据。

来自其他MS SQL Server docs :

Transactions running under snapshot isolation take an optimistic approach to data modification by acquiring locks on data before performing the modification only to enforce constraints. Otherwise, locks are not acquired on data until the data is to be modified. When a data row meets the update criteria, the snapshot transaction verifies that the data row has not been modified by a concurrent transaction that committed after the snapshot transaction began. If the data row has been modified outside of the snapshot transaction, an update conflict occurs and the snapshot transaction is terminated. The update conflict is handled by the Database Engine and there is no way to disable the update conflict detection.

关于servicestack - ServiceStack.OrmLite是否支持乐观并发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17405600/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com