Creation and Removal of EJBs

   

Creating instances of enterprise beans is much different that creating regular Java objects where all the objects reside in the same JVM. The container steps in and performs many system services when a bean is instantiated . In fact, the container might not need to even create a new bean, but rather pull an existing one from a bean pool or possibly from another user if resources are limited. To create a new bean, or really to obtain a local or remote reference to a new bean, you must go through the home interface. After a client has located an enterprise bean's home interface, the client can get a reference to an instance of the enterprise bean by using one of the create methods on the home interface. For example, assuming that the home interface for the OrderFulfillmentProcessorBean has already been located, the following code fragment shows how you can create a remote reference:

 // Other code here to lookup the home interface  OrderFulfillmentProcessor remoteProcessor = null;  remoteProcessor = orderFulfillmentProcessorHome.create(); 

The create method on the OrderFulfillmentProcessorHome returns a remote interface reference to the new enterprise bean instance. Remember that the enterprise bean that the remote reference points to might have come from an object pool. It's entirely up to the container whether it creates a new object or pulls one from somewhere else. There's no requirement that the container create a new instance of the bean when a client calls one of the create methods, just as long as the client gets a valid reference. In fact, some containers might not even prepare a bean for the client when a create method is called. In some vendor's products, an instance of the bean might not even be prepared for the client until the client makes the first remote method call.

In the previous example, the details of how to locate a home interface for an enterprise bean were not shown. For information on how to obtain a home interface for an enterprise bean, see "Locating EJB Objects," p. 97 .

To remove instances of your enterprise beans, you should use one of the several remove methods available through the home or component interfaces. Which version of the remove method you use depends on which type of enterprise bean you are using and also whether you have a reference to the home or remote component interface. Both the entity bean and session bean interfaces support a no-argument remove method from the component interface. You can also call one of several remove methods on the home interface for your enterprise bean. You can pass the handle in the remove method for either the session bean or the entity bean. Also for the entity bean, you can pass in the primary key for the bean that you want to remove. For both types of enterprise beans, the remove method might not actually remove the object and free the memory for the object because the enterprise bean might just be put back into the bean pool. Whether or not the removed bean is truly removed or just put back into the pool is entirely up to the container implementation.

Caution

A javax.ejb.RemoveException will be thrown if you call the remove method that takes a primary key for a session bean. The reason that it's even there is because both the entity bean and session bean home interfaces share javax.ejb.EJBHome as the interface for their homes , and this method is defined there.




Special Edition Using Enterprise JavaBeans 2.0
Special Edition Using Enterprise JavaBeans 2.0
ISBN: 0789725673
EAN: 2147483647
Year: 2000
Pages: 223

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net