简体   繁体   English

Spring @Transactional 和 Spring @Lock 注释之间有什么关系?

[英]What is the relation between Spring @Transactional and Spring @Lock annotation?

I found that the @Transactional is used to ensure transaction on repository method or on a service method.我发现@Transactional用于确保存储库方法或服务方法上的事务。
@Lock is used on repository method to ensure locking of entity to provide isolation. @Lock用于存储库方法以确保锁定实体以提供隔离。

Some questions are raised in my mind:我的脑海中提出了一些问题:

  1. What are major difference/relations in these two annotations?这两个注释的主要区别/关系是什么?
  2. When to use @Transactional and when to use @Lock ?何时使用@Transactional以及何时使用@Lock
  3. Is @Lock useful in distributed database system to provide data concurrency and consistency? @Lock在分布式数据库系统中是否有用以提供数据并发和一致性?

Transactional: Whenever you put @Transactional annotation, it enables transactional behavior which qualifies ACID properties事务性:每当您放置 @Transactional 注释时,它都会启用符合 ACID 属性的事务性行为

ACID: ACID ( Atomicity , Consistency , Isolation , Durability ) is a set of properties of database transactions intended to guarantee the validity even in the event of errors. ACID: ACID(原子性一致性隔离性、持久性)是数据库事务的一组属性,旨在保证即使发生错误时的有效性。

Atomic Guarantees that all operations in a transaction are treated as a single “unit”, which either succeeds completely or fails completely.原子保证事务中的所有操作都被视为一个“单元”,它要么完全成功,要么完全失败。

Consistent Ensures that a transaction can only bring the database from one valid state to another by preventing data corruption.一致通过防止数据损坏,确保事务只能将数据库从一个有效的 state 带到另一个。

Isolation Determines how and when changes made by one transaction become visible to the other.隔离确定一个事务所做的更改如何以及何时对另一个事务可见。 Serializable and Snapshot Isolation are the top 2 isolation levels from a strictness standpoint.从严格性的角度来看,Serializable 和 Snapshot Isolation 是前 2 个隔离级别。

Durable Ensures that the results of the transaction are permanently stored in the system.持久确保交易结果永久存储在系统中。 The modifications must persist even in case of power loss or system failures.即使在断电或系统故障的情况下,修改也必须持续存在。


Lock: It should not be confused with transactional,@Lock enables locking behavior during a transaction Lock:不应与事务性混淆,@Lock 在事务期间启用锁定行为

JPA has two main lock types defined. JPA 定义了两种主要的锁类型。

  1. Pessimistic Locking悲观锁定
  2. Optimistic Locking乐观锁定

If you want to know more about Pessimistic and Obtimistic locking you can explore the internet, below is explanation from Baeldung ,如果您想了解更多关于悲观和乐观锁定的信息,您可以浏览互联网,以下是Baeldung的解释,

Pessimistic Locking When we are using Pessimistic Locking in a transaction and access an entity, it will be locked immediately.悲观锁定当我们在事务中使用悲观锁定并访问实体时,它将立即被锁定。 The transaction releases the lock either by committing or rolling back the transaction.事务通过提交或回滚事务来释放锁。

Optimistic Locking In Optimistic Locking, the transaction doesn't lock the entity immediately.乐观锁定在乐观锁定中,事务不会立即锁定实体。 Instead, the transaction commonly saves the entity's state with a version number assigned to it.相反,事务通常保存实体的 state 并为其分配版本号。

When we try to update the entity's state in a different transaction, the transaction compares the saved version number with the existing version number during an update.当我们尝试在不同的事务中更新实体的 state 时,事务会在更新期间将保存的版本号与现有的版本号进行比较。

At this point, if the version number differs, it means that the entity can't be modified.此时,如果版本号不同,则表示无法修改实体。 If there is an active transaction then that transaction will be rolled back and the underlying JPA implementation will throw an OptimisticLockException.如果存在活动事务,则该事务将回滚,并且底层 JPA 实现将抛出 OptimisticLockException。

Apart from the version number approach, we can use other approaches such as timestamps, hash value computation, or serialized checksum, depending on which approach is the most suitable for our current development context.除了版本号方法,我们还可以使用其他方法,例如时间戳、hash 值计算或序列化校验和,这取决于哪种方法最适合我们当前的开发环境。

There are also other lock types available in spring spring 中还提供其他锁类型

  • NONE: No lock.无:无锁。
  • OPTIMISTIC: Optimistic lock. OPTIMATIC:乐观锁。
  • OPTIMISTIC_FORCE_INCREMENT: Optimistic lock, with version update. OPTIMISTIC_FORCE_INCREMENT:乐观锁,有版本更新。
  • PESSIMISTIC_FORCE_INCREMENT: Pessimistic write lock, with version update PESSIMISTIC_FORCE_INCREMENT:悲观写锁,有版本更新
  • PESSIMISTIC_READ: Pessimistic read lock. PESSIMISTIC_READ:悲观读锁。
  • PESSIMISTIC_WRITE: Pessimistic write lock. PESSIMISTIC_WRITE:悲观写锁。
  • READ: Synonymous with OPTIMISTIC.阅读:与乐观同义。
  • WRITE: Synonymous with OPTIMISTIC_FORCE_INCREMENT. WRITE:与 OPTIMISTIC_FORCE_INCREMENT 同义。

Now answer to your questions现在回答你的问题

  1. What are the major differences/relations in these two annotations?这两个注释的主要区别/关系是什么?

You will understand after reading above看完上面你就明白了

  1. When to use @Transactional and when to use @Lock?何时使用@Transactional,何时使用@Lock?

If you want transactional behavior then add @transactional and if your usecase requires locking and as per use case use appropriate locking如果您想要事务行为,请添加 @transactional 并且如果您的用例需要锁定并根据用例使用适当的锁定

  1. Is @Lock useful in the distributed database system to provide data concurrency and consistency? @Lock 在分布式数据库系统中是否有用,可以提供数据的并发性和一致性?

The two main tools we use to cope with concurrency are database transactions and distributed locks.我们用来处理并发的两个主要工具是数据库事务和分布式锁。 These two are not interchangeable.这两者不可互换。 You can't use a transaction when you need a lock.需要锁时不能使用事务。 You can't use a lock when you need a transaction.需要事务时不能使用锁。 source 资源

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

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