Quick revision
The short answer
In PostgreSQL, lock_timeout limits how long a statement waits to acquire any lock, statement_timeout limits a statement's total run time, and idle_in_transaction_session_timeout ends sessions that sit idle with a transaction open. In MySQL, innodb_lock_wait_timeout (50 seconds by default) limits InnoDB row lock waits, and by default only the waiting statement is rolled back, not the transaction. In a Spring Boot service with HikariCP we set them per connection with connection-init-sql, per database role, or in the JDBC URL, use the jakarta.persistence.lock.timeout hint for pessimistic locks, and keep every value below the HTTP timeout.
What to remember
In this answer, we saw that PostgreSQL's lock_timeout bounds lock waits, statement_timeout bounds a statement's total time, and idle_in_transaction_session_timeout ends sessions idling in open transactions, while MySQL's innodb_lock_wait_timeout bounds row lock waits and rolls back only the statement unless innodb_rollback_on_timeout is on. From Spring Boot we set them through HikariCP's connection-init-sql, role settings or JDBC URL options, use the jakarta.persistence.lock.timeout hint per query, prefer SET LOCAL for per-transaction overrides, and choose values below the HTTP timeout.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
In PostgreSQL, lock_timeout limits how long a statement waits to acquire any lock, statement_timeout limits a statement's total run time, and idle_in_transaction_session_timeout ends sessions that sit idle with a transaction open. In MySQL, innodb_lock_wait_timeout (50 seconds by default) limits InnoDB row lock waits, and by default only the waiting statement is rolled back, not the transaction. In a Spring Boot service with HikariCP we set them per connection with connection-init-sql, per database role, or in the JDBC URL, use the jakarta.persistence.lock.timeout hint for pessimistic locks, and keep every value below the HTTP timeout.
Why this question is asked
Timeouts are the cheapest protection against lock problems, and most services run with none: a single long transaction can then make requests wait for minutes while the client gave up long ago. Interviewers ask this to see whether we know what each setting measures, which one would have saved us in a given incident, and how to set them correctly behind a connection pool. In this answer, we will go through the three PostgreSQL timeouts, the MySQL lock wait timeout and its rollback rule, the ways to set them from Spring Boot and HikariCP, the JPA lock timeout hint, and how to choose the values.
1The Three PostgreSQL Timeouts
Each timeout watches a different kind of waiting. lock_timeout limits how long one statement may wait to acquire a lock, any lock: a row lock held by another transaction, or a table lock, such as the one an ALTER TABLE needs. If the wait exceeds it, the statement fails with SQLState 55P03 (lock_not_available) and the transaction is aborted. It does not limit the statement's work once it has its locks.
statement_timeout limits the total time of one statement, including waiting for locks, so it catches slow queries as well as long waits; it fails with SQLState 57014 (query_canceled). idle_in_transaction_session_timeout is different again: it ends the whole session when it has been idle inside an open transaction for longer than the limit. That catches the application bug where code begins a transaction, then waits on something else, holding locks and blocking vacuum while doing nothing. PostgreSQL 17 also added transaction_timeout, which limits a whole transaction's duration.
| Setting | Limits | On expiry |
|---|---|---|
| lock_timeout (PostgreSQL) | wait to acquire one lock | statement fails, 55P03 |
| statement_timeout (PostgreSQL) | total time of one statement | statement fails, 57014 |
| idle_in_transaction_session_timeout | idle time inside an open transaction | session terminated |
| innodb_lock_wait_timeout (MySQL) | wait for an InnoDB row lock | error 1205, statement rolled back |
2MySQL's innodb_lock_wait_timeout
In MySQL, innodb_lock_wait_timeout limits how long a statement waits for an InnoDB row lock, 50 seconds by default, which is far longer than most web requests. When it expires, the statement fails with error 1205, Lock wait timeout exceeded. The surprising part is what is rolled back: by default only that statement, not the whole transaction. The transaction stays open, with its earlier changes and locks, and the application must decide to roll back or retry. If innodb_rollback_on_timeout is enabled, a server-level setting, the whole transaction is rolled back instead.
Note that it only covers row locks. Waits for metadata locks, such as an ALTER TABLE waiting behind an open transaction, are governed by lock_wait_timeout, whose default is a year. Both can be set per session.
Because of that rule, Spring's handling matters: when the exception propagates out of a @Transactional method, Spring rolls back the transaction, so the partial transaction does not commit. Code that catches the exception inside the transaction and carries on would commit the earlier statements without the failed one.
3Setting Them From Spring Boot and HikariCP
There are three common places to set session defaults, and they differ in scope. HikariCP's connection-init-sql runs once on every new physical connection, so all borrowers of that connection get the setting. A database role setting, ALTER ROLE app SET lock_timeout = '2s', applies to every session that logs in as that role, whatever the application. And the JDBC URL can carry startup options: PostgreSQL's driver accepts options=-c lock_timeout=2s, and MySQL Connector/J accepts sessionVariables.
The role approach has the advantage that it also covers migrations, scripts and other tools that log in as the same role, and it survives configuration mistakes in a single service. Whichever we choose, we should verify it with SHOW lock_timeout from a pooled connection, because a typo in a URL option is silently ignored in some setups.
4Per-Query Lock Timeouts in JPA
For a specific pessimistic lock, JPA has a standard hint, jakarta.persistence.lock.timeout, in milliseconds. In Spring Data JPA we put it on the repository method next to @Lock. A value of 0 usually means do not wait at all (NOWAIT); other values are translated by the provider in a database-specific way, and some databases do not support a wait time in the lock clause, so check what your Hibernate version sends, and fall back to SET LOCAL lock_timeout inside the transaction where needed.
Spring's @Transactional(timeout = 5) is another layer: Spring and Hibernate apply the remaining transaction time as a JDBC query timeout on each statement, so the driver cancels a statement that would run past the transaction's deadline.
5Choosing the Values
The rule that ties the layers together is that the database should give up before the client does. If the load balancer and client time out after 10 seconds, a statement that waits 50 seconds for a lock keeps working on a request nobody is waiting for, holding a connection and maybe locks. So a typical online service might use lock_timeout of 1 to 3 seconds, statement_timeout of 5 to 10 seconds, all below the 10-second HTTP limit, and idle_in_transaction_session_timeout of a minute.
Batch jobs and migrations need different values, which they should set explicitly for their own sessions or transactions, for example a migration with lock_timeout = '3s' that retries, so an ALTER TABLE waiting behind a long transaction does not block every query queued behind it. Finally, timeouts produce errors, so the application should map them to clear responses, such as 503 with a retry hint, and count them in metrics.
6Follow-up questions
Why can SET LOCAL be safer than SET with a pool?
SET changes the setting for the rest of the session, and with a pool the session outlives our request: the next request that borrows the connection inherits our changed value, which is a hard bug to trace. SET LOCAL changes it only until the current transaction ends, then the session default returns, so a per-transaction override cannot leak to other borrowers. Behind PgBouncer in transaction mode, SET LOCAL is the only form that behaves predictably at all.
Is statement_timeout enough without lock_timeout?
It bounds everything, but it cannot tell waiting from working. With only statement_timeout = 10s, a statement may spend all 10 seconds waiting for a lock. A short lock_timeout fails lock waits quickly, which is usually the right reaction, while still letting a legitimately heavy query use its full statement budget.
Does HikariCP's connectionTimeout relate to these?
It is a different wait: how long a thread waits to borrow a connection from the pool, 30 seconds by default. It also should be shorter than the HTTP timeout, because pool exhaustion usually comes from the same long transactions that lock timeouts are meant to cut short.
7Conclusion
In this answer, we saw that PostgreSQL's lock_timeout bounds lock waits, statement_timeout bounds a statement's total time, and idle_in_transaction_session_timeout ends sessions idling in open transactions, while MySQL's innodb_lock_wait_timeout bounds row lock waits and rolls back only the statement unless innodb_rollback_on_timeout is on. From Spring Boot we set them through HikariCP's connection-init-sql, role settings or JDBC URL options, use the jakarta.persistence.lock.timeout hint per query, prefer SET LOCAL for per-transaction overrides, and choose values below the HTTP timeout.
Open with
In PostgreSQL, lock_timeout is how long a single statement may wait to get any lock, row or table, before failing with 55P03. statement_timeout caps the statement's total run time, waiting included. idle_in_transaction_session_timeout kills a session that sits idle with a transaction open, which catches the code that opens a transaction and then waits on something else while holding locks.
Then, if they want more
In MySQL, innodb_lock_wait_timeout limits row lock waits in InnoDB, fifty seconds by default, and the catch is that on timeout only the statement is rolled back, not the transaction, unless innodb_rollback_on_timeout is on, so the application has to roll back.
In Spring Boot with Hikari I'd set them as session defaults, either with spring.datasource.hikari.connection-init-sql running SET lock_timeout, or as role settings with ALTER ROLE, which also covers scripts, or through JDBC URL options like PostgreSQL's options parameter or Connector/J's sessionVariables. For individual pessimistic locks I'd use the jakarta.persistence.lock.timeout hint and check how Hibernate applies it.
For per-transaction overrides I'd use SET LOCAL so nothing leaks to the next borrower of the pooled connection. And I'd keep all of them below the HTTP timeout so the database gives up before the client does.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- PostgreSQL 17
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- PostgreSQL documentation, MySQL reference manual
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Explain lock_timeout, statement_timeout and idle_in_transaction_session_timeout in...": https://scrivio-interview-library.netlify.app/databases/database-locking/a318fd9448/