2 Replies Latest reply on Sep 28, 2009 2:02 AM by bartatamas

    Entity bean deadlock

    bartatamas

      Hi!

      I have two entity beans for example: A, B
      There is a relation between these beans, and I have to be able to access B from A and A from B.

      A: getTitle() {
      return name + getB().getName();
      }

      B: getTitle() {
      return name + getA().getName();
      }

      When a thread calls A.getTitle() and an other thread calls B.getTitle(), there can be a deadlock: thread1 locks A; thread2 locks B; thread1 waits for B; thread2 waits for A -> deadlock

      What can I do to avoid these deadlock? Read-only method declaration isn't enough. I have to call the beans from each other. The real situation is much more difficult, it is only a simplified case.

      How can I obtain that a get method doesn't lock the bean itself?
      Thanks, Tamas

        • 1. Re: Entity bean deadlock

          Maybe you could use

          A: getTitle() {
          return name + getSync().getName(getB());
          }

          B: getTitle() {
          return name + getSync().getName(getA());
          }

          Where Sync is a Singleton with

          Sync:
          syncronised getName(Bean b){
          return b.getName();
          }
          This way the threads run without interference until on of them calls the getName() of Sync. At this point, one of them is prone to win the race, and the syncronisation within Sync avoids the deadlock.

          The penalty for the indirect call to Sync should not be too bad if the three beans are in the same VM.

          Didn't try it, though ;-)

          Cheers ,
          Georg

          • 2. Re: Entity bean deadlock
            bartatamas

            I think it has the same problem.

            thread1 is in A.getTitle so locks A.
            thread2 is in B.getTitle so locks B.

            thread1 gets B and calls Sync.getName(B), and waits for B there (in b.getName() because thread2 locks B)

            thread2 gets A and waits for the Singleton to call getName(A), but thread1 will not return