tzaeschke / zoodb

ZooDB Object Database
Apache License 2.0
57 stars 9 forks source link

Mutli-threaded access causes lock exception #41

Closed tzaeschke closed 10 years ago

tzaeschke commented 10 years ago

Using different threads to open and commit databases causes the following Exception:

java.lang.IllegalMonitorStateException: null
    at java.util.concurrent.locks.ReentrantReadWriteLock$Sync.tryRelease(ReentrantReadWriteLock.java:374) ~[na:1.7.0_65]
    at java.util.concurrent.locks.AbstractQueuedSynchronizer.release(AbstractQueuedSynchronizer.java:1260) ~[na:1.7.0_65]
    at java.util.concurrent.locks.ReentrantReadWriteLock$WriteLock.unlock(ReentrantReadWriteLock.java:1131) ~[na:1.7.0_65]
    at org.zoodb.internal.server.DiskAccessOneFile.beginCommit(DiskAccessOneFile.java:505) ~[zoodb.jar:na]
    at org.zoodb.internal.model1p.Node1P.beginCommit(Node1P.java:286) ~[zoodb.jar:na]
    at org.zoodb.internal.Session.processOptimisticVerification(Session.java:192) ~[zoodb.jar:na]
    at org.zoodb.internal.Session.commit(Session.java:128) ~[zoodb.jar:na]
    at org.zoodb.jdo.impl.TransactionImpl.commit(TransactionImpl.java:82) ~[zoodb.jar:na]
tzaeschke commented 10 years ago

This happens because the current thread-locking in the database uses Java Lock, which are based on ThreadLocal storage and require the same thread to lock and unlock a Lock. Solution: Replace it with a read-write semaphore which is Thread insensitive.

tzaeschke commented 10 years ago

Note that this error occurs even if the Threads are not used concurrently. Concurrent threads are not supported at the moment.