在SQL Server中同一表的不同行同时读取和写入

时间:2022-03-21 01:52:00

My web application runs on Spring (MVC) 4.2.9.RELEASE, Hibernate 5.1.3.Final, Spring Data 1.8.2.RELEASE, and MS SQL Server Enterprise 2014 (two clustered virtual machines and AlwaysOn).

我的Web应用程序在Spring(MVC)4.2.9.RELEASE,Hibernate 5.1.3.Final,Spring Data 1.8.2.RELEASE和MS SQL Server Enterprise 2014(两个集群虚拟机和AlwaysOn)上运行。

The application frequently reads and writes on one table. However, all reads and writes always happen on different rows (records). Put it another way, there is never a time when two or more reads or writes happen on the same row at the same time.

应用程序经常在一个表上读写。但是,所有读取和写入总是发生在不同的行(记录)上。换句话说,永远不会有同时在同一行上发生两次或多次读取或写入的时间。

How can I configure this table through JPA to make sure that a write or read on a row in this table will not block other simultaneous reads and writes on the same table? For example, two writes can perform on the table at the same time since they never writes on the same row. Basically, I would like to increase performance of the website and reduce chances of deadlocks.

如何通过JPA配置此表以确保此表中某行的写入或读取不会阻止同一表上的其他同时读取和写入?例如,两个写入可以同时在表上执行,因为它们从不在同一行上写入。基本上,我想提高网站的性能,减少死锁的机会。

Is this something doable through JPA?

这可以通过JPA来实现吗?

1 个解决方案

#1


1  

Locking is quite complicated. Likely, SQL Server locks your table per page. That means writes to one page do not block reads to other pages.

锁定非常复杂。可能,SQL Server每页锁定您的表。这意味着写入一个页面不会阻止对其他页面的读取。

A best practice is to monitor the logs for deadlocks. If no deadlocks occur, there's no need to customize locking. If deadlocks do occur, you'll have a specific problem to solve. A solution can involve modifying your queries with table hints, configuring SQL Server, or changing the logic. But that's impossible to tell without knowing the details.

最佳做法是监视日志以查找死锁。如果没有发生死锁,则无需自定义锁定。如果确实发生了死锁,那么您将遇到一个特定的问题需要解决。解决方案可能涉及使用表提示修改查询,配置SQL Server或更改逻辑。但如果不了解细节,这是不可能的。

#1


1  

Locking is quite complicated. Likely, SQL Server locks your table per page. That means writes to one page do not block reads to other pages.

锁定非常复杂。可能,SQL Server每页锁定您的表。这意味着写入一个页面不会阻止对其他页面的读取。

A best practice is to monitor the logs for deadlocks. If no deadlocks occur, there's no need to customize locking. If deadlocks do occur, you'll have a specific problem to solve. A solution can involve modifying your queries with table hints, configuring SQL Server, or changing the logic. But that's impossible to tell without knowing the details.

最佳做法是监视日志以查找死锁。如果没有发生死锁,则无需自定义锁定。如果确实发生了死锁,那么您将遇到一个特定的问题需要解决。解决方案可能涉及使用表提示修改查询,配置SQL Server或更改逻辑。但如果不了解细节,这是不可能的。