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

Posted on 2005-04-02
Medium Priority
Last Modified: 2013-11-24
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.
Question by:phoenixsilver
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
LVL 15

Expert Comment

ID: 13689451
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.
LVL 19

Assisted Solution

by:Jim Cakalic
Jim Cakalic earned 75 total points
ID: 13689795
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

Author Comment

ID: 13690363
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.  
Congratulations! You’re Certified – Now What?

Starting a new career can be overwhelming. Becoming certified in your field of expertise is a great start, but where do you go from here?  Here are some tips to help you on your career journey.


Author Comment

ID: 13690374
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.
LVL 15

Accepted Solution

aozarov earned 300 total points
ID: 13690387
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.
LVL 15

Expert Comment

ID: 13690419
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.

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
In this post we will learn different types of Android Layout and some basics of an Android App.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses
Course of the Month15 days, 10 hours left to enroll

741 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question