Questions tagged [pessimistic-locking]

Pessimistic locking is a strategy that will lock a database record, that is to be updated, for exclusive use until the update is complete.

Pessimistic locking is a strategy that will lock a database record, that is to be updated, for exclusive use until the update is complete. The database record is locked immediately when the lock is requested and it is then guaranteed that the database record will be updated. Unlike optimistic locking, great care must be taken in the application design to avoid deadlocks.

178 questions
2
votes
0 answers

Why HSQLDB doesn't lock row when use SELECT ... FOR UPDATE with MVCC concurrency control model

I am trying to synchronize threads in my SpringBoot application through pessimistic locks in the database. For the test environment, I use HSQLDB. But for some reason, when calling the SELECT ... FOR UPDATE query, my all threads receive the result…
2
votes
1 answer

Why PostgreSQL throws concurrent update error while SELECT FOR UPDATE SKIP LOCKED?

My Java application interacts with PostgreSQL via MyBatis. From multiple threads it executes this request select * from v_packet_unread limit 1000 for update skip locked and sometimes gets ERROR: could not serialize access due to…
diziaq
  • 6,881
  • 16
  • 54
  • 96
2
votes
1 answer

Optimistic concurrency - Isn't there a problem?

I've read the article. The article describes the next solution to situations when many users can write to the same DB. You as a user need to: Retrieve the row and the last modified dateTime of the row. Make the calculations you want, but don't…
2
votes
0 answers

How to guard/lock entites in reactive pipelines between each subscription

How do I lock an entity in a Reactive context between each subscription? I have an endpoint to cancel an order. In the flow, I call some other services to cancel promotion and so on, and at last, I will mark Order in cancelled status and persist…
2
votes
0 answers

Updlock, Holdlock, Rowlock usage? Pessimistic Locking in SQL Server

I have some question about Pessimistic Locking in SQL Server? Here are my classes and test scenario; Entity class: @Data @Entity(name = "mapping") @Table( uniqueConstraints = @UniqueConstraint( name =…
Sefas
  • 89
  • 5
2
votes
0 answers

What is JPA pessimistic explicit @Lock usecase (and just @Transactional is not an option)?

I have some knowledge on JPA and DB locks (both optimistic and pessimistic) and transactions, how they works and what guarantees. A TransactionRequiredException is thrown if there is no active transaction when lock is called because explicit…
J.J. Beam
  • 2,612
  • 2
  • 26
  • 55
2
votes
1 answer

Using pessimistic lock in transaction can not prevent record from being updated?

I'm using the Spring Boot JPA, with TransactionTemplate and pessimistic lock, I'm trying to prevent the record from being updated by other thread during the transaction. Here is the code: transactionService.executeTransaction(() -> { …
Man Shen
  • 401
  • 2
  • 16
2
votes
2 answers

catching ConstraintViolationException and handling unque constraints

I'm really upset with Hibernate! I have a database table (mysql) that holds parent-child relationships that allow me to build a tree of categories. I have multiple threads which can try to get and if not there create the category path (with several…
Andy Nuss
  • 745
  • 1
  • 7
  • 20
2
votes
3 answers

"Next number" scenarios using Entity Framework

I have been playing with Entity Framework and so far I like using it a lot. But everything I've done so far assumes optimistic locking which works well for me in most cases. However, I have the following scenario: A SQL Server table with only one…
2
votes
1 answer

mysql transaction pessimistic lock corrupted

I am using hibernate, and innodb and mysql. I have a pessimistic lock (on a specific table row) obtained at the beginning of a very long hibernate transaction. On occasion, when I run the program that starts this transaction and am running in…
Andy Nuss
  • 745
  • 1
  • 7
  • 20
2
votes
1 answer

Pessimistic locking in grails 3+ lock()

def check(){ println"start first" Domain1 domain1=Domain1.get(1); domain1.lock(); println "locking started" sleep(20*60) println "save first" domain1.name="hari ram" …
2
votes
0 answers

Why can I get properties of doctrine locked entity but can't set this entity to another

I've got a question. I would like to understand why I can get properties from a pessimistic write locked entity doing this: $string = $entity->getTitle(); but I can't make it: $otherEntity->setEntity($entity); It works with the pessimistic read…
Jul6art
  • 219
  • 3
  • 14
2
votes
0 answers

Row-Level Update Lock using System.Transactions

I have a MSSQL procedure with the following code in it: SELECT Id, Role, JurisdictionType, JurisdictionKey FROM dbo.SecurityAssignment WITH(UPDLOCK, ROWLOCK) WHERE Id = @UserIdentity I'm trying to move that same behavior into a component that…
2
votes
0 answers

When is PessimisticLockException Thrown when using PessimisticLocking

According to the docs, if a lock is attempted and the object is already a locked, the transaction will not be rolled back and a LockTimeoutException is thrown. In what cases will a PessimisticLockException be thrown when using an EntityManager?
DD.
  • 21,498
  • 52
  • 157
  • 246
2
votes
1 answer

How to prevent the race condition in this simple example?

The code below will fetch a new address from an external API if the user doesn't yet have one: def create_address if current_user.address.blank? data = AddressAPI.create_address current_user.update!(address: data['address']) end …