我应该使用什么事务隔离级别

时间:2021-06-01 07:06:40

I am working on a custom application that allows users to insert / update / delete / retrieve data from a database.

我正在开发一个自定义应用程序,允许用户插入/更新/删除/检索数据库中的数据。

Stored procedures I use affects a few tables so I use a transaction and I want to be able to lock these tables to prevent unintended things from happening when multiple users are inserting , deleting, updating, or retrieving (CRUD) data.

我使用的存储过程影响了几个表,因此我使用了一个事务,我希望能够锁定这些表,以防止在多个用户插入,删除,更新或检索(CRUD)数据时发生意外事件。

My question is what would be the best isolation level to use here, I have read the MSDN on isolation levels and tried to make sense of it as well as searching around, If someone who has been here done that could comment quickly that would be greatly appreciated.

我的问题是在这里使用的最佳隔离级别是什么,我已经在隔离级别上阅读了MSDN,并尝试理解它以及搜索,如果有人在这里完成了可以快速评论,那将是非常赞赏。

1 个解决方案

#1


0  

The default .Net transaction level is serializable. Why do you need another transaction level? Are you under the impression that without an explicit transaction, other connections will be able to change the data out from under you? If so, that is incorrect -- all actions take place in an explicit or implicit transaction, and the only time you can get inconsistent results is if you explicitly set the transaction isolation level to something which allows it.

默认的.Net事务级别是可序列化的。为什么需要其他交易级别?您是否认为没有明确的交易,其他连接将能够从您下面更改数据?如果是这样,那是不正确的 - 所有操作都发生在显式或隐式事务中,并且只有当您明确将事务隔离级别设置为允许它的事务时,才能获得不一致的结果。

Edit: As pointed out in a comment, the default for the database engine is READ COMMITTED, but the default for the .Net transactions/connections is SERIALIZABLE, with a caveat -- if the transaction level is changed, it will not be "dropped" for a pooled connection. This means that you can never be absolutely sure what it is unless you set it yourslf. For most cases, you can probably get away with assuming SERIALIZABLE and leave it at that.

编辑:正如评论中所指出的,数据库引擎的默认值是READ COMMITTED,但.Net事务/连接的默认值是SERIALIZABLE,但需要注意 - 如果事务级别发生更改,则不会“丢弃” “用于汇集连接。这意味着除非你设置它,否则你永远不能完全确定它是什么。在大多数情况下,您可以放弃假设SERIALIZABLE并将其保留。

#1


0  

The default .Net transaction level is serializable. Why do you need another transaction level? Are you under the impression that without an explicit transaction, other connections will be able to change the data out from under you? If so, that is incorrect -- all actions take place in an explicit or implicit transaction, and the only time you can get inconsistent results is if you explicitly set the transaction isolation level to something which allows it.

默认的.Net事务级别是可序列化的。为什么需要其他交易级别?您是否认为没有明确的交易,其他连接将能够从您下面更改数据?如果是这样,那是不正确的 - 所有操作都发生在显式或隐式事务中,并且只有当您明确将事务隔离级别设置为允许它的事务时,才能获得不一致的结果。

Edit: As pointed out in a comment, the default for the database engine is READ COMMITTED, but the default for the .Net transactions/connections is SERIALIZABLE, with a caveat -- if the transaction level is changed, it will not be "dropped" for a pooled connection. This means that you can never be absolutely sure what it is unless you set it yourslf. For most cases, you can probably get away with assuming SERIALIZABLE and leave it at that.

编辑:正如评论中所指出的,数据库引擎的默认值是READ COMMITTED,但.Net事务/连接的默认值是SERIALIZABLE,但需要注意 - 如果事务级别发生更改,则不会“丢弃” “用于汇集连接。这意味着除非你设置它,否则你永远不能完全确定它是什么。在大多数情况下,您可以放弃假设SERIALIZABLE并将其保留。