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

Ryan Tremaine shows users how to avoid overusing fine-grained accessor methods in EJB programming.


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
	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 last published in August 2001

Dig Deeper on Enterprise Application Integration (EAI)

PRO+

Content

Find more PRO+ content and other member only offers, here.

Start the conversation

Send me notifications when other members comment.

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

Please create a username to comment.

-ADS BY GOOGLE

SearchSoftwareQuality

SearchCloudApplications

SearchAWS

TheServerSide

SearchWinDevelopment

Close