12.11.2014 Views

web server - Borland Technical Publications

web server - Borland Technical Publications

web server - Borland Technical Publications

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Client view of an enterprise bean<br />

calls the enterprise bean methods in the same way that it invokes any method, such as<br />

its own method summarize().<br />

...<br />

Cart cart;<br />

{<br />

...<br />

// obtain a reference to the bean's remote interface<br />

cart = home.create(cardHolderName, creditCardNumber, expirationDate);<br />

}<br />

// create a new book object<br />

Book knuthBook = new Book("The Art of Computer Programming", 49.95f);<br />

// add the new book item to the cart<br />

cart.addItem(knuthBook);<br />

...<br />

// list the items currently in the cart<br />

summarize(cart);<br />

cart.removeItem(knuthBook);<br />

...<br />

Removing bean instances<br />

The remove() method operates differently for session beans than for entity beans.<br />

Because a session object exists for one client and is not persistent, a client of a<br />

session bean should call the remove() method when finished with a session object.<br />

There are two remove() methods available to the client: the client can remove the<br />

session object with the javax.ejb.EJBObject.remove() method, or the client can remove<br />

the session handle with the javax.ejb.EJBHome.remove(Handle handle) method. Go to<br />

“Using a bean's handle” on page 89 for more information on handles.<br />

While it is not required that a client remove a session object, it is considered to be good<br />

programming practice. If a client does not remove a stateful session bean object, the<br />

container eventually removes the object after a certain time, specified by a timeout<br />

value. The timeout value is a deployment property. However, a client can also keep a<br />

handle to the session for future reference.<br />

Clients of entity beans do not have to deal with this problem as entity beans are only<br />

associated with a client for the duration of a transaction and the container is in charge<br />

of their life cycles, including their activation and passivation. A client of an entity bean<br />

calls the bean's remove() method only when the entity object is to be deleted from the<br />

underlying database.<br />

Using a bean's handle<br />

A handle is an another way to reference an enterprise bean. A handle is a serializable<br />

reference to a bean. You can obtain a handle from the bean's remote interface. Once<br />

you have the handle, you can write it to a file (or other persistent storage). Later, you<br />

can retrieve the handle from storage and use it to reestablish a reference to the<br />

enterprise bean.<br />

However, you can only use the remote interface handle to recreate the reference to the<br />

bean; you cannot use it to recreate the bean itself. If another process has removed the<br />

bean, or the system crashed or shutdown and removed the bean instance, then an<br />

exception is thrown when the client application tries to use the handle to reestablish its<br />

reference to the bean.<br />

When you are not sure that the bean instance will still be in existence, rather than using<br />

a handle to the remote interface, you can store the bean's home handle and recreate<br />

the bean object later by invoking the bean's create or find methods.<br />

Chapter 11: Writing enterprise bean clients 89

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!