Timeout error trying to lock table in h2

前端 未结 8 1473
无人共我
无人共我 2020-12-02 16:39

I get the following error under a certain scenario

When a different thread is populating a lot of users via the bulk upload operation and I was trying to view the li

相关标签:
8条回答
  • 2020-12-02 17:16

    I'd like to suggest that if you are getting this error, then perhaps you should not be using a transaction on your bulk database operation. Consider instead doing a transaction on each individual update: does it make sense to think of an entire bulk import as a transaction? Probably not. If it does, then yes, MVCC=true or a bigger lock timeout is a reasonable solution.

    However, I think for most cases, you are seeing this error because you are trying to perform a very long transaction - in other words you are not aware that you are performing a really long transaction. This was certainly the case for myself and I simply took more care on how I was writing records (either using no transactions or using smaller transactions) and the lock timeout issue was resolved.

    0 讨论(0)
  • 2020-12-02 17:19

    Yes, you can change the lock timeout. The default is relatively low: 1 second (1000 ms).

    In many cases the problem is that another connection has locked the table, and using multi-version concurrency also solves the problem (append ;MVCC=true to the database URL).

    EDIT: MVCC=true param is no longer supported, because since h2 1.4.200 it's always true for a MVStore engine, which is a default engine.

    0 讨论(0)
  • 2020-12-02 17:21

    I faced quite the same problem and using the parameter "MVCC=true", it solved it. You can find more explanations about this parameter in the H2 documentation here : http://www.h2database.com/html/advanced.html#mvcc

    0 讨论(0)
  • 2020-12-02 17:26

    Working with DBUnit, H2 and Hibernate - same error, MVCC=true helped, but I would still get the error for any tests following deletion of data. What fixed these cases was wrapping the actual deletion code inside a transaction:

    Transaction tx = session.beginTransaction();
    ...delete stuff
    tx.commit(); 
    
    0 讨论(0)
  • 2020-12-02 17:30

    From a 2020 user, see reference

    Basically, the reference says:

    Sets the lock timeout (in milliseconds) for the current session. The default value for this setting is 1000 (one second).

    This command does not commit a transaction, and rollback does not affect it. This setting can be appended to the database URL: jdbc:h2:./test;LOCK_TIMEOUT=10000

    0 讨论(0)
  • 2020-12-02 17:31

    I got this issue with the PlayFramework

    JPAQueryException occured : Error while executing query from models.Page where name = ?: Timeout trying to lock table "PAGE"

    It ended being an infinite loop of sorts because I had a

    @Before

    without an unless which caused the function to repeatedly call itself

    @Before(unless="getUser")

    0 讨论(0)
提交回复
热议问题