Wednesday, February 16, 2005

Using both local and remote interfaces of ejb

I believe the decision of using remote or local interfaces should be determined at the architecture/design phase and use one or the other but not both.

Recently I was asked to provide both implementation and I lost my battle of choosing one or the other. It's a long story why I lost my battle. Well, here is what I did to dynamically determine which interface to use without code or configuration changes. The service consumer can pass in a parameter requesting local/remote to service locator.

First define an interface (let's call it paymentService) where you define all business methods. Make sure all the methods throw Exception. Both remote and Local interface should be extending this interface. The remote interface should override all the methods and throw RemoteException (as required by EJB spec). I really do not like this idea of forcing all the methods to throw Exception. But I could not think of a better way of doing this. The problem is the remote interface methods must throw RemoteException and the local interface must not throw RemoteException.

Now the the create() method fromt the home interface should return the "PaymentService" and not the PaymentEJBServiceLocal or PaymentEJBService (remote), that way the caller can invoke the business methods without knowing whether the service is remote or local. Though it is not required, I would have an explicit catch block for RemoteExceptions in the client code.

Now the ejb should be deployed as two ejbs (local & remote) with two distinct jndi names and the lookup method can be made conditional to return remote or local.

With this solution a client can use the local interface while another client is using remote interface. This is useful when you have both remote and local clients ant they invoke same service.

Monday, February 14, 2005

Transaction Management with EJBS

When managing persistence with Entity beans, you have to set the transaction properties for both the entity beans and the calling EJBs appropriately.

This is what I did for the current project I am working on:
From the facade I set the transaction levels as Required and supports, thinking that I really do not want to unnecessarily tie up the container resources for the methods that I do not care for transaction.
For example, I have no reason to put itransaction around methods of a session bean when the methods are just reading data. Now these session beans directly or indirectly calls entity beans and I had the entity beans transaction as supports and load frequency to transaction, as I wanted the entity beans to load for each transaction.

The side effect of this setup was that every time any getter was called on the entity bean that was not part in a transaction, it reloaded the whole entity bean. So, if a Business Object called five getters on an Entity bean, the Entity Bean loaded five times. Though I saved some resources (and probably some processing time) by not making the methods of the Session Bean transactional, the reloading of ejb was taking way way too much time and resource compared to what it would have taken if the Session Bean had called these methods on the Entity Bean in a transaction.

I put all Session Bean methods in transaction and it improved performance many folds. I set all the Entity Bean methods transaction property to Mandatory (would throw Exception if not called with in a transaction) to ensure the callers are in a transaction and the Entity Bean is loaded only once per transaction.