aspose file tools*
The moose likes EJB and other Java EE Technologies and the fly likes EJB (CMP) performance ... why so slow? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "EJB (CMP) performance ... why so slow?" Watch "EJB (CMP) performance ... why so slow?" New topic
Author

EJB (CMP) performance ... why so slow?

Stephen Lee
Ranch Hand

Joined: Dec 11, 2001
Posts: 74
Though I have written many EJB code, I am not familiar with the tuning of
the EJB. I am wondering what I can do to improve the performace of my code.
I have used struts and session facade in my code. My client calls the
stateless session bean which lookups the entity bean for obtaining the data.
200 records takes about 8 seconds, I think this is much slower than using
JDBC directly.
I have tried to include part of my code in the following:
In session bean:
public Vector getParts(String projID) {
private PartsDataHome partsDataHome = lookup(...) ; // lookup the entity
bean PartsData
try {
Vector parts = new Vector();
Enumeration items = null;
items = Collections.enumeration(partsDataHome.findByProjID(projID));
while (items.hasMoreElements()) {
PartsData partsData = (PartsData)items.nextElement();
PartsVO partsVO = new PartsVO(partsData.getPartsID);
partsVO.setPartsName(partsData.getPartsName());
partsVO.set...(..);
.......... // there are total about 15 setXXX() method
parts.add(partsInfo);
}
return parts;
}
catch (Exception e) {
throw new EJBException(e);
}
}
... while PartsData is the entity bean
Is there any problem about my code? or I should set something? Is there any suggestion? Thank you very much!
PS: the DB is MySQL while the driver is mm.mysql-2.0.13
Stephen
Byron Estes
Ranch Hand

Joined: Feb 21, 2002
Posts: 313
EJB (especially EntityBeans) is not a silver bullet technology. It's not appropriate for all situations. What you're doing is probably a better candidate for a stateless session bean and straight JDBC.
Regards,


Byron Estes<br />Sun Certified Enterprise Architect<br />Senior Consulant<br />Blackwell Consulting Services<br />Chicago, IL<br /><a href="http://www.bcsinc.com" target="_blank" rel="nofollow">www.bcsinc.com</a>
Dave Landers
Ranch Hand

Joined: Jul 24, 2002
Posts: 401
Some things to consider...
First, you are calling some 15 methods on the same entity. This does lots of network traffic (serialized data pass-by-value), and lots of database traffic (depending on container optimizations and transaction settings, might call ejbLoad/ejbStore around each method call). A good pattern for this is to create a Value object that contains all the attributes of your entity - do all the set methods on this object and make one entity call (to a setByValue mehtod).
Using a Local interface for the Entity may help as it uses pass-by-reference and does not serialize arguments and return values on each method call.
Another possible problem is transactions. Not sure what your transaction settings are on these beans, but you might have problems here.
There are several ways that transactions could affect you, but most likely is if the session bean is not transactional (i.e. transaction-attribute set to NotRequired or Supports), then each call to the 15 entity set methods is potentially in a separate transaction (or non transactional). Transaction creation can be expensive, and batching up several calls in one transaction gives the container and the database an opportunity to optimize things like ejbLoad/ejbStore calls, etc.
So try setting transaction-attribute to Required on the Session and Entity methods - see if that helps. Also make sure you have the transaction-attribute set on the findXXX method on the Home.
Another thing to watch is the size of the collection returned from the findXXX methods - if tey return too many entities (like, hundreds or thousands), you might be swamping the app server's entity pool. If that's the case, I'd think about how to fix this. One way is to add a home method to just do a query and return only primary keys (vs a finder that returns references to beans that the container has to load). You can then step thru the returned beans and call findByPrimaryKey. Doesn't sound too different except that you can control how many you load at one time (i.e. how many beans in each transaction).
Of course, I must say that all the "transaction" comments above have been aimed at helping your performance - don't forget that trasactions are there to protect your data integrety. So don't do anything in the name of performance that would compromise your data.
I have gone on too long.... Hope it helps.
Cheers, and keep experimenting.
-- Dave
Karthik Guru
Ranch Hand

Joined: Mar 06, 2001
Posts: 1209
i observed that using ejbSelects i can return a column from the database.
But the problem with ejbSelect is that it can't be defined in the home interface but on the bean?
So even thoug am able to access it "indirectly "through the Local interfaces, it does'nt quite fit in there!
say i have a Person and i want all the PersonID (primary keys) and not the Person objects, i s'd'nt be writing a ejbSelect in the person bean whereas it has nothing to do with a particular Person bean.
Can finders (defined on Home interfaces) return a collection of data that maps to a single column in the database??
I c'dnt use them that way though (remeber havign got some errors while doign a ejbc with weblogic6.1)
thanks!
Originally posted by Dave Landers:
Some things to consider...
First, you are calling some 15 methods on the same entity. This does lots of network traffic (serialized data pass-by-value), and lots of database traffic (depending on container optimizations and transaction settings, might call ejbLoad/ejbStore around each method call). A good pattern for this is to create a Value object that contains all the attributes of your entity - do all the set methods on this object and make one entity call (to a setByValue mehtod).
Using a Local interface for the Entity may help as it uses pass-by-reference and does not serialize arguments and return values on each method call.
Another possible problem is transactions. Not sure what your transaction settings are on these beans, but you might have problems here.
There are several ways that transactions could affect you, but most likely is if the session bean is not transactional (i.e. transaction-attribute set to NotRequired or Supports), then each call to the 15 entity set methods is potentially in a separate transaction (or non transactional). Transaction creation can be expensive, and batching up several calls in one transaction gives the container and the database an opportunity to optimize things like ejbLoad/ejbStore calls, etc.
So try setting transaction-attribute to Required on the Session and Entity methods - see if that helps. Also make sure you have the transaction-attribute set on the findXXX method on the Home.
Another thing to watch is the size of the collection returned from the findXXX methods - if tey return too many entities (like, hundreds or thousands), you might be swamping the app server's entity pool. If that's the case, I'd think about how to fix this. One way is to add a home method to just do a query and return only primary keys (vs a finder that returns references to beans that the container has to load). You can then step thru the returned beans and call findByPrimaryKey. Doesn't sound too different except that you can control how many you load at one time (i.e. how many beans in each transaction).
Of course, I must say that all the "transaction" comments above have been aimed at helping your performance - don't forget that trasactions are there to protect your data integrety. So don't do anything in the name of performance that would compromise your data.
I have gone on too long.... Hope it helps.
Cheers, and keep experimenting.
-- Dave
Satish Gopalakrishnan
Ranch Hand

Joined: Feb 13, 2001
Posts: 52
This is a decent article on Common Pitfalls while writing Entity EJB's . worth a glance.
http://dev2dev.bea.com/articlesnews/discussion/thread.jsp?forum=1&thread=196
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: EJB (CMP) performance ... why so slow?
 
Similar Threads
BeanNotReetrantException
BeanNotReentrantException
How to implement EntityBean as Value Object?
Please help me with the calling entity bean create method
EJB lookup and Jboss SEAM