Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set lock timeout in postgres - Hibernate

I'm trying to set a Lock for the row I'm working on until the next commit:

entityManager.createQuery("SELECT value from Table where id=:id")
            .setParameter("id", "123")
            .setLockMode(LockModeType.PESSIMISTIC_WRITE)
            .setHint("javax.persistence.lock.timeout", 10000)
            .getSingleResult();

What I thought should happen is that if two threads will try to write to the db at the same time, one thread will reach the update operation before the other, the second thread should wait 10 seconds and then throw PessimisticLockException.

But instead the thread hangs until the other thread finishes, regardless of the timeout set.

Look at this example :

database.createTransaction(transaction -> {
    // Execute the first request to the db, and lock the table
    requestAndLock(transaction);

    // open another transaction, and execute the second request in
    // a different transaction
    database.createTransaction(secondTransaction -> {
        requestAndLock(secondTransaction);
    });

    transaction.commit();
});

I expected that in the second request the transaction will wait until the timeout set and then throw the PessimisticLockException, but instead it deadlocks forever.

Hibernate generates my request to the db this way :

SELECT value from Table where id=123 FOR UPDATE

In this answer I saw that Postgres allows only SELECT FOR UPDATE NO WAIT that sets the timeout to 0, but it isn't possible to set a timeout in that way.

Is there any other way that I can use with Hibernate / JPA? Maybe this way is somehow recommended?

like image 864
Daniel Taub Avatar asked Aug 14 '18 16:08

Daniel Taub


People also ask

How to set lock timeout in Hibernate?

createQuery("SELECT value from Table where id=:id") . setParameter("id", "123") . setLockMode(LockModeType.

What is lock timeout in Postgres?

lock_timeout is a configuration parameter determining the period of time which should elapse before an attempt to acquire a lock is abandoned. lock_timeout was added in PostgreSQL 9.3.

How to set lock_ timeout in postgres?

There's no query-level way to set lock_timeout , but you can and should just: SET LOCAL lock_timeout = '1s'; after you BEGIN a transaction.

What is hibernate Postgres?

PostgreSQL is an object-relational database management system. It's one of the most popular databases used in the world. Hibernate is a framework for mapping object-oriented domain model to a relational database. Hibernate handles persistent database accesses with high-level object handling functions.


4 Answers

Hibernate supports a bunch of query hints. The one you're using sets the timeout for the query, not for the pessimistic lock. The query and the lock are independent of each other, and you need to use the hint shown below.

But before you do that, please be aware, that Hibernate doesn't handle the timeout itself. It only sends it to the database and it depends on the database, if and how it applies it.

To set a timeout for the pessimistic lock, you need to use the javax.persistence.lock.timeout hint instead. Here's an example:

entityManager.createQuery("SELECT value from Table where id=:id")
        .setParameter("id", "123")
        .setLockMode(LockModeType.PESSIMISTIC_WRITE)
        .setHint("javax.persistence.lock.timeout", 10000)
        .getSingleResult();
like image 77
Thorben Janssen Avatar answered Oct 16 '22 15:10

Thorben Janssen


There is the lock_timeout parameter that does exactly what you want.

You can set it in postgresql.conf or with ALTER ROLE or ALTER DATABASE per user or per database.

like image 45
Laurenz Albe Avatar answered Oct 16 '22 14:10

Laurenz Albe


I think you could try

SET LOCAL lock_timeout = '10s';
SELECT ....;

I doubt Hibernate supports this out-of-box. You could try find a way to extend it, not sure if it worth it. Because I guess using locks on a postges database (which is mvcc) is not the smartest option.

You could also do NO WAIT and delay-retry several times from your code.

like image 3
kan Avatar answered Oct 16 '22 14:10

kan


The hint for lock timeout for PostgresSQL doesn't work on PostreSQL 9.6 (.setHint("javax.persistence.lock.timeout", 10000)

The only solution I found is uncommenting lock_timeout property in postgresql.conf:

lock_timeout = 10000            # in milliseconds, 0 is disabled
like image 2
LubošCZ Avatar answered Oct 16 '22 14:10

LubošCZ