Gotchas

As you start to implement your own Session beans, there's bound to be a couple of aspects that will trip you up. The following quick checklist of such “gotchas” should keep you on the straight-and-narrow:

  • When you look up resources from JNDI, you should use a string of the form java:comp/env/XXX. However, in the deployment descriptor, only the XXX is needed; the java:comp/env prefix is implicit.

  • Perhaps obvious, but don't use ejb as a prefix for naming your business methods. Names of that format are reserved for the EJB architecture callback methods.

  • Don't implement the remote interface in your bean! If you do so, your bean could inadvertently return itself (Java keyword this) as a return type. If a client starts invoking methods on this reference, it will bypass all of the EJB container's transaction and security control that is managed within the EJBObject proxy to the bean. Instead, use the business interface idiom mentioned earlier today.

  • The EJBObject interface defines a getPrimaryKey() method; the EJBHome interface defines a remove(Object primaryKey) method. Attempting to call either of these for a Session bean will immediately throw a RemoteException, so don't do it. They are there only for Entity beans, discussed tomorrow.

  • You'll learn more about transactions on Day 8, but for now, remember that you should not perform work that updates a database in the ejbCreate or ejbRemove method, or indeed the other ejbXXX() lifecycle methods. This is because the transaction context is undefined. See section 7.5.7 of the EJB specification for more details.

  • Don't try to have a Session bean call itself through its own EJBObject; it won't work. This is prevented so that the bean developer does not need to worry about multiple threads. In other words, Session beans are non-reentrant. Of course, your bean can call methods on itself directly through its implicit this reference.

  • An ejbCreate() is required for stateless Session beans. It isn't in the javax.ejb.SessionBean interface because stateful Session beans won't necessarily have a no-arg create() method.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.41.229