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.

0 Comments:

Post a Comment

<< Home