Re: Writable lock nested in read-only lock
Svata Dedic <[email protected]> Mon, 30 Sep 2002 10:44:12 +0200
| Newsgroups | gmane.comp.java.netbeans.modules.mdr.devel |
|---|---|
| Message-ID | <[email protected]> |
Brian,
Brian Smith wrote:
> Hi,
>
> I am writing some code like this:
>
> repository.beginTrans(false);
> try {
> // [1] read only part
> boolean ok = false;
>
> if (I need to write to the repository then) {
> repository.beginTrans(true);
> try {
> // [2] read-write part
> ok = true;
> } finally {
> repository.endTrans(!ok);
> }
> }
> // [3] read only part
> } finally {
> repository.endTrans();
> }
>
> As you can see, I have a write transaction nested inside of a read
> transaction. The reason I am doing this is because I want to lock the
> repository against _writes_ for all three parts, [1], [2], [3], but I
> want to lock the repository against _reads_ for as little time as
> possible [2]. However, MDR responds with an error: Writable lock nested
> in read-only lock.
>
>
> Would it be possible to change MDR's behavior so that writable locks
> could be nested inside read-only locks? Is this an unsound thing to do?
> It seems sound to me (the inner call to beginTrans() would block until
> all _other_ readers ended their transactions, just like any other call
> to beginTrans(true)). Is there a better way to do what I want?
>
You risk that when your thread will attempt to promote its read lock to
a write one it will block (since there will be several other readers),
possibly holding other resources locked (those it locked from [1]
forward). If there's another thread, which also got the read lock and
waits on that resource or will wait to the previous one's output, you
get a deadlock.
-Svata
--
Svatopluk Dedic <mailto:[email protected]>
NetBeans, Java/Repository <http://java.netbeans.org>