简体   繁体   English

使用Java锁进行数据库并发

[英]Using Java locks for database concurrency

I have the following scenario. 我有以下情况。 I have two tables. 我有两张桌子。 One stores multi values that are counters for transactions. 一个存储作为交易计数器的多个值。 Through a java application the first table value is read, incremented and written to the second table, as well as the new value being written back to the first table. 通过Java应用程序,将读取,递增第一个表值并将其写入第二个表,并将新值写回第一个表。 Obviously there is potential for this to go wrong as it's a multiple user system. 显然,由于这是一个多用户系统,因此有可能出错。 My solution, in Java, to the issue is to provide Locks that have to, well should , be aquired before any action can be taken on either table. 我的解决方案,在Java中,这个问题是提供有,也应该成为获得性可以在任一台在采取任何行动之前锁。 These Locks, ReentrantLocks, are static and there is one for each column in Table 1 as the values are completely independent of each other. 这些锁(即ReentrantLocks)是静态的,表1中的每一列都有一个,因为这些值是完全相互独立的。

Is this a recommended approached? 这是推荐的方法吗?

Cheers. 干杯。

No. Use implicit Database Locks 1 for Database Concurrency. 。对数据库并发使用隐式数据库锁 1 Relational databases support Transactions which are a vital part of ACID : use them. 关系数据库支持事务,这是ACID的重要组成部分:请使用它们。

Java-centric locks will not work cross-VM and as such will not help in multi-User/Server environments. 以Java为中心的锁定将无法在多VM中使用 ,因此在多用户/服务器环境中将无济于事


1 Databases are smart enough to acquire/release locks to ensure Consistency and Isolation and may even use "lock free" implementations such as MVCC. 1数据库足够聪明,可以获取/释放锁以确保一致性和隔离性,甚至可以使用“无锁”实现,例如MVCC。 There are rare occasions when explicit database locks must be requested, but this is an advanced use-case. 极少数情况下,必须请求显式数据库锁定,但这是高级用例。

Whilst agreeing with some of the sentiments of @pst's answer, I would say this depends slightly. 虽然同意@pst回答的某些观点,但我会说这在一定程度上取决于。

If the sequence of events is, and probably always will be, essentially "SQL oriented", then you may as well do the locking at the database level (and indeed, probably implicitly via the use of transactions). 如果事件的顺序基本上(并且将始终是“面向SQL的”),那么您也可以在数据库级别进行锁定(实际上,可能是通过使用事务来隐式锁定)。

However if there is, or you are planning to build in, significant data manipulation logic within your app tier (either generally or in the case of this specific operation), then locking at the app level may be more appropriate. 但是,如果您的应用程序层中有重要的数据操作逻辑 (或计划在其中进行构建)(通常是在这种特定操作的情况下),那么锁定应用程序级别可能更合适。 (In reality, you will probably still run your SQL in transactions so that you're actually locking at both levels.) (实际上,您可能仍会在事务中运行SQL,以便实际上在两个级别上都处于锁定状态。)

I don't think the issue of multiple VMs is necessarily a compelling issue on its own for relying on DB-level locking. 我认为,由于依赖于数据库级锁定,多个VM本身并不一定是一个引人注目的问题。 If you have multiple server apps accessing the database, you will in any case want to establish a well-defined protocol for which data is accessed concurrently under what circumstances. 如果您有多个服务器应用程序正在访问数据库,则无论如何都要建立一个定义明确的协议,在何种情况下可以同时访问数据。 And in a system of moderate complexity, you will in any case want to build in a system of running periodic sanity checks on the data. 在中等复杂度的系统中,无论如何您都希望构建一个对数据进行定期完整性检查的系统。 (Even if your server apps are perfectly behaved 100% of the time, will back end tech support never ever ever have to run some miscellaneous SQL on the database outside your app...?) (即使您的服务器应用程序在100%的时间内运行良好,后端技术支持也永远不会在应用程序外部的数据库上运行一些其他SQL ...?)

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

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