12.11.2014 Views

web server - Borland Technical Publications

web server - Borland Technical Publications

web server - Borland Technical Publications

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Managing transactions<br />

Managing transactions<br />

After the client creates a bean instance, it can use the getHandle() method to obtain a<br />

handle to this instance. Once it has the handle, it can write it to a serialized file. Later,<br />

the client program can read the serialized file, casting the object that it reads in to a<br />

Handle type. Then, it calls the getEJBObject() method on the handle to obtain the bean<br />

reference, casting the results of getEJBObject() to the correct type for the bean.<br />

To illustrate, the CartClient program might do the following to utilize a handle to the<br />

CartBean session bean:<br />

import java.io;<br />

import javax.ejb.Handle;<br />

...<br />

Cart cart;<br />

...<br />

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

// call getHandle on the cart object to get its handle<br />

cartHandle = cart.getHandle();<br />

// write the handle to serialized file<br />

FileOutputStream f = new FileOutputStream ("carthandle.ser");<br />

ObjectOutputStream o = new ObjectOutputStream(f);<br />

o.writeObject(myHandle);<br />

o.flush();<br />

o.close();<br />

...<br />

// read handle from file at later time<br />

FileInputStream fi = new FileInputStream ("carthandle.ser");<br />

ObjectInputStream oi = new ObjectInputStream(fi);<br />

//read the object from the file and cast it to a Handle<br />

cartHandle = (Handle)oi.readObject();<br />

oi.close();<br />

...<br />

// Use the handle to reference the bean instance<br />

try {<br />

Object ref = context.lookup("cart");<br />

Cart cart1 = (Cart) javax.rmi.PortableRemoteObject.narrow(ref, Cart.class);<br />

...<br />

} catch (RemoteException e) {<br />

...<br />

}<br />

...<br />

When finished with the session bean handle, the client can remove it with the<br />

javax.ejb.EJBHome.remove(Handle handle) method.<br />

A client program can manage its own transactions rather than letting the enterprise<br />

bean (or container) manage the transaction. A client that manages its own transaction<br />

does so in exactly the same manner as a session bean than manages its own<br />

transaction.<br />

When a client manages its own transactions, it is responsible for delimiting the<br />

transaction boundaries. That is, it must explicitly start the transaction and end (commit<br />

or roll back) the transaction.<br />

A client uses the javax.transaction.UserTransaction interface to manage its own<br />

transactions. It must first obtain a reference to the UserTransaction interface, using<br />

JNDI to do so. Once it has the UserTransaction context, the client uses the<br />

UserTransaction.begin() method to start the transaction, followed later by the<br />

UserTransaction.commit() method to commit and end the transaction (or<br />

90 BES Developer’s Guide

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

Saved successfully!

Ooh no, something went wrong!