How can I minimize number of trips to the Database using CMP?

if I call 5 different setters on a CMP 2.0 bean is that 5 round trips to the DB?  If so is there a way to batch the calls or get it so that it executes a single update rather than 5?  Or does the container (Jboss 4) manage this for me?  My Director who's a Softie and not familiar with J2EE was asking, because in a method call we called 5 setters for updating a users profile and he asked if it was 5 trips to the DB.  I couldn't give a concrete answer.  Any help of this would be great.
Who is Participating?
aozarovConnect With a Mentor Commented:
As I said if you had a transaction in place then calling the
setters will not trigger DB a operation (which will occurs only on commiting the transaction).
Personally, I prefer to have in the CMP itself setValueObject
or similar which gets the User POJO (in your case). That
method will invoke the rest of the CMP setters.
Doing that will enable you (if you want, though not recommended)
to have the transaction settings for the CMP itself.
I think the default transaction settings for JBoss is "Supports"
(at least for 3.2.6), so if you see transaction exceptions (as you suggested) then either you have transaction settings in the ejb-jar.xml for some of your beans or you programatically use the UserTransaction.
Though the J2EE spec does prevent such behaiver it is very unlikely for the application server to do that.
Normally DB interaction happens at the begining of the transaction and at the end (if commit). (That is definetly true for JBoss, and W.L)
Other times where you will find the application server apply the changes to the DB is before running a finder.
Jim CakalicConnect With a Mentor Senior Developer/ArchitectCommented:
Definitely sounds like you each EJB method call is occuring in a separate transaction. There are several things you might try.

First, you could perform transaction management directly within your client application. You would need to lookup the JTA UserTransaction object in your server and then call begin on the UserTransaction before any EJB method calls and commit on the object after.

Another common alternative is to use a Stateless Session EJB as a facade. Instead of calling the Entity EJB directly (which is considered bad practice), you'd make one method call to the Session EJB which would perform the updates on the Entity EJB. If the transaction demarcation mode of the method has been set to REQUIRED then the container will start and commit a transaction around the method call. That provides a single transactional context for all the update operations.

Another alternative to reducing the number of remote calls made to an EJB is to introduce a Value Object. The Value Object will have all the properties of the Entity EJB. When you want to get the Entity's properties you call a single get method and get back an object with all the properties. When you want to modify the Entity you call a single set or update method and pass a Value Object already set with the new values you want the entity to have. In both directions you are performing a bulk operation that takes only one method call and therefore a single transaction.

Jim Cakalic
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

phoenixsilverAuthor Commented:
I've seen both the Value object and the REQUIRED directive for transactions.  We actually use a stateless session facade (which is what I called the client) that interacts with the CMP.  Let's say we have a simple POJO object called User, I have a session fascade that handles the modifications of users called UserFacade.  I also have a CMP called UserEJB.  Now I believe that we have Transactions on or they are on by default with JBOSS because I've seen roll backs expections and such being thrown when we experienced FK issues with the DB.  How we've done it so far is we take any modifications from the User profile and populate a User POJO, call the session facade in the Struts Action class and pass the User POJO to the saveUser method of the POJO, in this method we get an CMP by the primary key which is the username and basically set all CMP values with the User POJO values.  

My ultimate question here is that my manager who comes from the .net world and isn't really familar with CMP would argue that using a transaction maybe costly or unneccassary so I wanted to know if there was a way to do with without transactions or value objects.   In .net they would be also limited to two options either Value Objects or Transactions, but the transactions would have to be manually invoked.  I thought maybe some how invoke all updates to occur at once.  Looks like there really are only two options.  I thought maybe there was a way.  
phoenixsilverAuthor Commented:
Not sure if I was clear on this, if we don't have transactions on updates get executed on every set am I correct?  I do believe we have transactions set to REQUIRED.
Right, if you don't have transaction in places each set will
cause an update (like setAutoCommit(true)). That is very bad
not only from performance reasons but more so from data integretiy reasons.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.