Home > SOA Tips > > Improve your overall EJB performance with the use of course grained accessor methods
SOA Tips:
EMAIL THIS
 TIPS & NEWSLETTERS TOPICS 


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


Ryan Tremaine
08.27.2001
Rating: -3.67- (out of 5)


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   



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

Rate this Tip
To rate tips, you must be a member of SearchSOA.com.
Register now to start rating these tips. Log in if you are already a member.




Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   



RELATED CONTENT
Java
Denmark is first to adopt Universal Business Language
XML-based financial standard gets new support
SOAP test tool adds WS-Security, MIME support
Survey: IT spending back from the dead
Is Eclipse in Sun's future?
WASP Server adds to XML Schema support
IBM, Microsoft on opposite sides of standards fence
WS orchestration 'power struggle' under way, IDC says
Internet millionaire gives integration a whirl
Novell targets Web services security

RELATED RESOURCES
2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
Search Bitpipe.com for the latest white papers and business webcasts
Whatis.com, the online computer dictionary

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.



SOA Trends and Strategy - SOA Education, SOA Development, SOA Implementations
About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
SEARCH 
TechTarget provides technology professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective purchase decisions and managing their organizations' technology projects - with its network of technology-specific websites, events and online magazines.

TechTarget Corporate Web Site  |  Media Kits  |  Site Map




All Rights Reserved, Copyright 2001 - 2009, TechTarget | Read our Privacy Policy
  TechTarget - The IT Media ROI Experts