简体   繁体   English

单个事务中的多个读取查询

[英]Multiple read queries in a single transaction

This answer claims that a transaction is useful for multiple read statements as well.这个答案声称事务对于多个读取语句也很有用。 Is the following test case faulty or does it require related tables to return consistent results?下面的测试用例是错误的还是需要相关的表才能返回一致的结果?

In console #1 execute the following:在控制台 #1 中执行以下命令:

set transaction isolation level serializable;
begin transaction;
select * from TableA;
waitfor delay '00:00:15';
select * from TableB;
commit transaction;

In console #2, during that 15 second window, execute following:在控制台 #2 中,在那 15 秒 window 中,执行以下操作:

set transaction isolation level serializable;
begin transaction;
insert into TableB(col) values('b');
commit transaction;

After 15 seconds has passed, console #1 returns with a row with 'b' in it. 15 秒后,控制台 #1 返回一行,其中包含'b' Why?为什么? I thought console #1 would return no results or the transaction #1 would abort, because TableB was modified.我认为控制台 #1 不会返回任何结果,或者事务 #1 会中止,因为TableB已被修改。

I've tried SQL Server LocalDB 2017 on Windows and SQL Server 2019 RC1 on Linux. I've tried SQL Server LocalDB 2017 on Windows and SQL Server 2019 RC1 on Linux. I ran my sql commands with DataGrip, but the original use case I had with Entity Framework 6.我使用 DataGrip 运行了我的 sql 命令,但我使用 Entity Framework 6 的原始用例。

The console #1 execution Return the 'b' Record against TableB because the console #2 Add the Record in TableB before the Selection Query of TableB in console #1.控制台#1 执行返回针对TableB 的'b' 记录,因为控制台#2 在控制台#1 中TableB 的选择查询之前添加了TableB 中的记录。 Transaction lock only those Tables which is exist in Executed Queries.事务仅锁定已执行查询中存在的那些表。 console #1 will not get the 'b' record if WAIT add after the selection of TableB.如果在选择 TableB 后等待添加,则控制台 #1 将不会获得“b”记录。 Query will be abort if structure of table is changed like Drop the column and add the New column before selection Query but If the * is not used.如果表的结构发生更改,例如删除列并在选择查询之前添加新列,但如果未使用 *,则查询将被中止。 There is no error generate if the Drop column is not exists by Name in selected Query.如果在所选查询中按名称不存在 Drop 列,则不会生成错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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