TransactionScope 是否支持原子数据库执行?

发布于 2024-11-25 11:12:30 字数 738 浏览 2 评论 0原文

我目前正在开发一个项目,其中使用 ADO 实体框架在 .Net 中实现 DAL。

数据库结构并不简单,我希望能够确保某些数据库操作是原子的。

我知道您可以通过创建存储过程并使用数据库TRANSACTION来做到这一点(如这篇文章)。 (我正在使用 SQL Server)

问题是,我想尽可能地将逻辑保留在软件方面,并且我正在考虑使用 .Net TransactionScope。尽管我知道从软件的角度来看它运行良好(在提交整个范围之前不会向数据库提交任何内容),但我怀疑它是否仍能确保数据库原子执行。

有人知道这个吗?

更具体地说,通常代码看起来像这样:

using (TransactionScope scope = new TransactionScope())
{
  /*
   * Do some opreations such as reads, write, insert, deletes
   */
   scope.Commit()
}

我想确保的是,括号内的所有内容都是“原子地”完成的(我猜是“isolation=SERIALIZABLE”)。我的意思是,当范围内的代码执行时,我不希望数据库的状态能够改变。

I am at the moment developping a project where the DAL is implemented in .Net using the ADO Entity Framework.

The DB structure is not straightforward and I would like to be able to ensure that some DB operations are atomic.

I know that you can do that by creating a STORED PROCEDURE and using a DB TRANSACTION (as mentioned in this SO post). (I'm using SQL Server)

The thing is, I would like to keep the logic on the software side as much as possible, and I was considering using .Net TransactionScope. Although I understand that it works well from a software point of view (nothing is committed to the DB until the whole scope is committed), I doubt it still ensures DB atomic execution.

Does somebody know about this?

More specifically, usually the code would look like this:

using (TransactionScope scope = new TransactionScope())
{
  /*
   * Do some opreations such as reads, write, insert, deletes
   */
   scope.Commit()
}

What I'd like to make sure, is that everything within the brackets is done "atomically" (isolation=SERIALIZABLE I guess). What I mean by that is that I don't want the state of the DB to be able to change when the code within the scope is executing.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

再可℃爱ぅ一点好了 2024-12-02 11:12:30

TransactionScope 在事务中运行更新(也可能进行读取,具体取决于您的事务隔离级别)

数据库提供程序已经是此事务的一部分,因为它们在事务范围内使用 - 因此事务范围实际上是一个数据库事务(如果事务中涉及多个数据库/消息队列/等,则可能更多)

编辑:实体框架详细信息

实体框架使用底层提供程序的连接 BeginTransaction 方法。在 SqlConnection 的情况下,它使用 SqlServer 的默认值,因此将使用 ReadCommissed

因此,如果您不使用事务范围,它将默认为 Read Commissed 而不是 Serialized

TransactionScope runs the updates in a transaction (and possibly the reads as well depending on your transaction isolation level)

The database providers are already part of this transaction as they are used within the transaction scope - therefore the transaction scope is, effectively, a database transaction (and possibly more if you have multiple databases / message queues / etc involved in the transaction)

EDIT: Entity Framework Details

Entity framework uses the underlying provider's connection BeginTransaction method. In the case of SqlConnection it uses the default for SqlServer and so will use ReadCommitted

So if you don't use a transaction scope it will default to Read Committed rather than Serializable

贪恋 2024-12-02 11:12:30

“数据库原子执行”是什么意思?有多种事务隔离级别。

与 SQL Server 通信时,.NET TransactionScope 会登记一个 SQL Server 事务,默认情况下该事务为 Serialized 隔离级别。

What do you mean by "DB atomic execution"? There are various transaction isolation levels.

When talking to SQL Server a .NET TransactionScope enlists a SQL Server transaction, which by default will be a Serializable isolation level.

你的呼吸 2024-12-02 11:12:30

事务始终是原子的,无论是在 SQL 中启动还是在客户端中启动(例如 TransactionScope)

A Transaction is always atomic, whether started in SQL, on in the client (eg TransactionScope)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文