J2EEOnline J2EEOnline

EJB Architecture  «Prev  Next»
Lesson 10Transactions and remote objects
Objective Describe how transactions are applied to remote objects.

J2EE Transactions and Remote Objects

Transactions can be applied to remote objects.However, we have already discussed the benefits of separating business logic from plumbing, so the remote object must be kept in the dark about the transaction.
The transaction will be managed by the container. Consider the Slide Show below in which we transfer the $100 from checking to savings in a remote object called Teller.
Note that, in this case, the remote object does not have to know any of the details concerning the transaction. All it does is make individual requests to the database.

  1. In container: a message received from client for the transfer () method: "Begin Transaction" executed and the transfer() method called.
  2. In transfer() a request is made to deduct $100 from checking.
  3. Inside database, $100 is deducted from checking account temporarily
  4. In transfer() a request is made to credit $100 to savings.
  5. In database, $100 is temporarily credited to savings account.
  6. In container, assuming all went well, "commit transaction" is executed by the container
  7. In the database, database commits credits and debits permanently

Transactions Remote Objects
Remote objects are networked object implementations that can be called by another JVM. They implement a remote interface and thus expose methods that can be invoked by remote clients.

Remote Object Implementation

The physical locations of remote objects and the clients that invoke them are not important. For example, it is possible for a client running in the same address space as a remote object to invoke a method on that object. It is also possible for a client across the Internet to do the same thing. To the remote object, both invocations appear to be the same. To make your object a remote object available to be invoked on by remote hosts, your remote class must perform one of the following steps:
Extend the class javax.rmi.PortableRemoteObject. PortableRemoteObject is a base class from which you can derive your remote objects. When your remote object is constructed, it automatically calls the PortableRemote Object's constructor, which makes the object available to be called remotely. Do not extend javax.rmi.PortableRemoteObject. Perhaps your remote object class needs to inherit implementation from another custom class. In this case, because Java does not allow for multiple implementation inheritance, you cannot extend PortableRemoteObject. If you do this, you must manually export your object so that it is available to be invoked on by remote hosts. To export your object, call javax.rmi.PortableRemoteObject.exportObject().
In the next lesson, you will be introduced to the deployment of a component in an Object Monitor.