0 Replies Latest reply on Apr 5, 2005 12:24 PM by spoonman464

    When is it necessary to set reentrant to true?

    spoonman464

      It appears difficult to find a conceptual explanation of when the reentrant node in an entity bean should be set to true. I realize that deep under the covers it has to do with concurrency and allowing multiple threads to access the same instance of an entity bean, blah, blah, blah, but I am a business application developer so details like that don't mean much to me.

      Instead, I deal with things on a higher level. My current situation and objectives seem to indicate that I should set reentrant to true but I want someone else's opinion before I do it.

      The situation is that I have two CMP entity beans called Author and Book. They are in a CMR relationship of many-to-one with the assumption that a book is written by only one Author and a single Author may write many books.

      To cut down on the number of method calls on the entity beans, I'm using what are often called "Data Transfer Objects" or "Transfer Objects" or "Value Objects". Basically they are plain old JavaBeans which contain instance variables that map (mostly) to the schemas for the types of EJBs they represent. I have one called AuthorTO and another called BookTO. When the session bean that I'm using as a façade between the remote client and the entity beans needs a book record, for example, it calls the getData() method on the entity bean's component interface. That method call returns a BookTO object which the session bean passes to the remote client.

      The instance variables in the BookTO include many things about the book record like the title, a synopsis, and the number of pages in the book. It also includes the name of the author. The Book EJB gets the name of the auther through the CMR relationship. see the code here:

      //abstract accessor methods for relationship fields
       public abstract AuthorLocal getAuthor();
       public abstract void setAuthor(AuthorLocal a);
      
      // methods for cmr objects
      public String getAuthorFullname(){
       AuthorLocal author = this.getAuthor();
       String str = author.getFirstname() + " " + author.getLastname();
       return str;
      }
      


      After getting the Author's fullname, it sticks that value into the BookTO. The idea is so that when the remote client gets a transfer object for a book, he gets all of the relevant information about the book AND the author's name. This approach seems sensible and reasonable and I'm assuming that it is a correct approach.

      Everything is fine up to this point. Here's the rub.

      Getting the BookTO when starting with an Author reference
      My session bean working as a façade also gets AuthorTO objects. When it has a reference to an Author's component interface, it needs to be able to get all of the information about all of the books the author has written. So, in the Author Bean code, I have a method that gets an ArrayList containing BookTOs for the books the author has written. See this code here:
      // abstract accessor methods for relationship fields
       public abstract Collection getBooks();
       public abstract void setBooks(Collection c);
      
      // methods using cmr objects
       public ArrayList getBooksData(){
       ArrayList al = new ArrayList();
       Collection c = this.getBooks();
       Iterator it = c.iterator();
       while (it.hasNext()){
       BookLocal book = (BookLocal) it.next();
       al.add(book.getData());
       }
       return al;
      }
      


      When this code runs, the al.add(book.getData()); line causes the Book Bean code to run the getFirstname() and getLastname() methods of the same Author bean the session bean is already using. At that moment, I get the error about reentering the same thread.

      So, maybe I'm way off base and should be doing things differently OR maybe my Author bean should be Reentrant. Since I want to be able to take full advantage of the CMR relationship, I think my approach is reasonable and sensible, but, what do I know? I don't write EJBs, I write business applications.

      Have I found a legitimate use of a reentrant entity bean? In other words, is reentrant=true a good idea or possibly even recommended when entity beans are related?

      Spoon