Hi,


I catch a thread deadlock while running H2 database in embedded mode with my 
app.

H2 1.4.196, connection string: jdbc:h2:file:mydir/mydb;MAX_MEMORY_ROWS=50000



Found one Java-level deadlock:
=============================
"Thread-102":
  waiting to lock monitor 0x00007f10580058e8 (object 0x00000005c9415e30, a 
org.h2.engine.Database),
  which is held by "Thread-99"
"Thread-99":
  waiting to lock monitor 0x00007f0fec0087e8 (object 0x00000005c95fd490, a 
org.h2.engine.Session),
  which is held by "pool-thread-1"
"pool-thread-1":
  waiting to lock monitor 0x00007f10580058e8 (object 0x00000005c9415e30, a 
org.h2.engine.Database),
  which is held by "Thread-99"

Java stack information for the threads listed above:
===================================================
"Thread-102":
   at org.h2.command.Command.executeUpdate(Command.java:253)
   - waiting to lock <0x00000005c9415e30> (a org.h2.engine.Database)
   at 
org.h2.jdbc.JdbcPreparedStatement.executeUpdateInternal(JdbcPreparedStatement.java:164)
   - locked <0x00000005ca95fca8> (a org.h2.engine.Session)
   at 
org.h2.jdbc.JdbcPreparedStatement.executeBatch(JdbcPreparedStatement.java:1215)
   ...

"Thread-99":
   at org.h2.command.ddl.Analyze.analyzeTable(Analyze.java:145)
   - waiting to lock <0x00000005c95fd490> (a org.h2.engine.Session)
   at org.h2.engine.Session.commit(Session.java:671)
   at org.h2.command.dml.TransactionCommand.update(TransactionCommand.java:46)
   at org.h2.command.CommandContainer.update(CommandContainer.java:101)
   at org.h2.command.Command.executeUpdate(Command.java:260)
   - locked <0x00000005c9415e30> (a org.h2.engine.Database)
   at org.h2.jdbc.JdbcConnection.commit(JdbcConnection.java:479)
   - locked <0x00000005dddbe080> (a 
org.h2.jdbcx.JdbcXAConnection$PooledJdbcConnection)
   ...

"pool-thread-1":
   at org.h2.result.ResultTempTable.dropTable(ResultTempTable.java:234)
   - waiting to lock <0x00000005c9415e30> (a org.h2.engine.Database)
   - locked <0x00000005c95fd490> (a org.h2.engine.Session)
   - locked <0x00000005ca914f30> (a org.h2.engine.Session)
   at org.h2.result.ResultTempTable.close(ResultTempTable.java:192)
   - locked <0x00000005d566e4a8> (a org.h2.result.ResultTempTable)
   at org.h2.result.LocalResult.close(LocalResult.java:429)
   at org.h2.jdbc.JdbcResultSet.nextRow(JdbcResultSet.java:3274)
   at org.h2.jdbc.JdbcResultSet.next(JdbcResultSet.java:131)
   ...


Found 1 deadlock.


The problem is that Thread-99 and pool-thread-1 locks the Database 
<0x00000005c9415e30> 

and the SystemSession <0x00000005c95fd490> objects in a different orders.

Thread-99 is making Connection.commit() after a simple INSERT,

pool-thread-1 is finishing to read a large resultset.



In ResultTempTable.java I've found a potential problem: probably the 
synchronization cascade must be reordered.


// the transaction must be committed immediately
// TODO this synchronization cascade is very ugly
synchronized (session) {
    synchronized (sysSession) {
        synchronized (database) {
            sysSession.commit(false);
        }
    }
}


Best regards,

Antón

-- 
You received this message because you are subscribed to the Google Groups "H2 
Database" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/h2-database.
For more options, visit https://groups.google.com/d/optout.

Reply via email to