简体   繁体   English

如何使用 FireDAC 和 SQL Server 避免不需要的事务和锁

[英]How to avoid unwanted transactions and locks using FireDAC and SQL Server

From analyzing table locks in SQL Server, my Win32 application built in RAD Studio XE7 starts numerous transactions while each FDQuery is active.通过分析 SQL Server 中的表锁,我在 RAD Studio XE7 中构建的 Win32 应用程序在每个 FDQuery 处于活动状态时启动了大量事务。 Sometimes this causes application problems and locks with dozens of users.有时这会导致应用程序问题并锁定数十个用户。 Especially with triggered tables.尤其是触发表。

For my test, I used simple FDConnection and FDQuery as Select * from Customer with default settings, and concluded that FDQuery1.Active:=True causes the start of a Customer table transaction.在我的测试中,我使用简单的 FDConnection 和 FDQuery 作为Select * from Customer并使用默认设置,并得出结论FDQuery1.Active:=True会导致 Customer 表事务的启动。 The transaction disappears when FDQuery1.Active:=false .FDQuery1.Active:=false时,事务消失。

I would like to inhibit the starting of transactions in FDQuery for read-only, as lists of data for grid or reports.我想禁止在 FDQuery 中以只读方式启动事务,作为网格或报告的数据列表。

But I can't find a way to find the appropriate tuning of FDQuery.但是我找不到找到适当调整 FDQuery 的方法。

By default, SQL Server does not implement versioning of data blocks.默认情况下,SQL Server 不实现数据块的版本控制。 So, to return a consistent set of rows, it guarantee that no other sessions makes changes do data during execution of a query, using shared locks.因此,为了返回一组一致的行,它使用共享锁保证在执行查询期间没有其他会话更改数据。

Using "WITH(NOLOCK)" disable shared locks, but can result in an inconsistent result set.使用“WITH(NOLOCK)”禁用共享锁,但会导致结果集不一致。

The only one solution is to use READ_COMMITED_SNAPSHOT isolation level, which store changed data to temp, used to return consistent result sets without locking updates.唯一的一种解决方案是使用 READ_COMMITED_SNAPSHOT 隔离级别,将更改的数据存储到 temp,用于在不锁定更新的情况下返回一致的结果集。

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

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