Tip

Improve your overall EJB performance with the use of course grained accessor methods


A common mistake that I often come across in EJB programming is an overuseof fine-
A common mistake that I often come across in EJB programming is an overuse
of fine-grained accessor methods resulting in an unnecessary resource drain.

In example 1, to create/edit a PersonBean we would be making a separate
request for each of its set methods causing unnecessary amounts of network
traffic,processor utilization, and database resources. Retrieval of the data 
would also require a separate request for each attribute.

Example 1.
public class PersonBean 
	implements SessionBean {

	//member variables
	public long userid;
	public String username;
	public String firstname;
	public String lastname;

	//accessor methods
	

    Requires Free Membership to View

public void setUserId(long id) { this.userid = id; } public void setUsername (String username) { this.username = username; } public void setFirstname(String firstname) { this.firstname = firstname; } public void setLastname (String lastname) { this.lastname = lastname; } public long getUserId() { return this.userid; } public String getUsername() { return this.username; } public String getFirstname() { return this.firstname; } public String getLastname() { return this.lastname; } } Note - In the following example an accessor method that takes all four attributes as a signature would be adequate but what if PersonBean contained 50 attributes? Things could get sloppy and become a nightmare to debug and maintain. I only used 4 attributes for this example in an attempt to maintain simplicity. A more favorable method that significantly reduces the network consumption is to use a data structure object containing all of the necessary variables (example 2). We can then instantiate this structure object and pass this to a new setPersonStruct(PersonStruct) method in PersonBean (example 3). This will reduce the number of requests to our bean from 4 to 1. We will also need a getPersonStruct() for data retrieval. Example 2. import java.io.Serializable; public class PersonStruct implements Serializable{ //member variables public long userid; public String username; public String firstname; public String lastname; //constructor public PersonStruct(long userid, String username, String firstname, String lastname){ this.userid = userid; this.username = username; this.firstname = firstname; this.lastname = lastname; } } Example 3. Create an accessor method for the PersonBean that accepts a PersonStruct as an argument. We also want an accessor method that returns a instantiated PersonStruct. public class PersonBean implements SessionBean { //member variables public long userid; public String username; public String firstname; public String lastname; //new set method taking a PersonStruct as an argument public void setPersonStruct(PersonStruct person){ this.userid = person.userid; this.username = person.username; this.firstname = person.firstname; this.lastname = person.lastname; } //new get method returning an instantiated PersonStruct public PersonStruct getPersonStruct(){ return new PersonStruct(userid, username, firstname, lastname); } //accessor methods public void setUserId(long id) { this.userid = id; } public void setUsername(String username) { this.username = username; } public void setFirstname(String firstname) { this.firstname = firstname; } public void setLastname(String lastname) { this.lastname = lastname; } public long getUserId() { return this.userid; } public String getUsername() { return this.username; } public String getFirstname() { return this.firstname; } public String getLastname() { return this.lastname; } } Note - It's still a good idea to leave our individual get and set methods intact. If you only wanted to change or retrieve a single attribute it would make more sense to use the corresponding method for that attribute rather than creating a new structure object. Ryan Tremaine rtrema@bellatlantic.net

This was first published in August 2001

There are Comments. Add yours.

 
TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

Disclaimer: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.