Java Data Repository performance

It’s a common scenario that a web site rarely updates the background database but frequently
access the database to fetch requested data. Consider the web site that provides the results of Secondary School Certificate exam of a country, based the population of that country the total number of students who attended the exam may be 2 million, 3 million or more. In India it may be more than 10 million. When the result is just published just think about the number of hits per second on that web site. A student just provides a Student Registration number and gets the result from the site. So should that
web application search the database every time there is a request for result in that site? To me it’s a
‘NO’. In the age of WiMax and 3G it is expected the website to appear in a twinkling of an eye. So if you
can provide sufficient RAM to that web server why don’t you use data repository. “Data
Repository”? Yes. Data Repository: It’s an implementation of virtual storage of a portion of a database or entire database.

The concept is:
Database (MySQL, SQL Server etc), Load the data in to RAM and refresh periodically Æ Response
of the data query. For each subsequent database query, after the initial loading, the response is generated from the data loaded into RAM. So, there is no need to knock the database for every query. This is a quite
faster approach. But remember if the database rarely has insert, update and delete operation this
approach is very much efficient. If the there is a change in the database the change will be reflected in the database only after the data repository is refreshed.

So in a web server how would you implement that? Here is an example to implement Data
Repository using Java and STRUTS framework. For simplicity I have omitted some minor parts such as
import statement, portion to fetch data from Database in the reload() method. I have also omitted
the details of the DataDTO class.


Implementing the DataRepository:

/*********************** Start of************************/ 
public class DataRepository 
	// 60 Seconds, you can change it s u like   
  public static final long REFRESH_INTERVAL = 60 * 1000; 
  static DataRepository repository = null; 
  private long lastRelodingTime; 
  //Its a map to get Result using Registration number as KEY 
  HashMap regMap; 
  private DataRepository()  { 
  public static DataRepository getInstance()  { 
    if (repository == null) 
    return repository; 
  private synchronized static void createDataRepository()  { 
    if (repository == null) 
      repository = new DataRepository(); 
  private void reload()  { 
   // DataDTO is the clas to hold Registration Number,  
   // Name and CGPA of any student 
   // For simplicity here I havent added it. 
      ResultSet rs = null; 
      rs = ResultSet after database query 
    // DataDTO provides StudentName, Student Registration Number 
    // and student CGPA. You can add additional member variable 
    // or method for more details*/ 
    DataDTO dto = new DataDTO();  
    regMap.put(dto, new Long(rs.getLong("registration")));  
  public synchronized DataDTO getResult(long registrationNumber)  { 
    return (DataDTO)this.regMap.get(new Long(registrationNumber)); 
  private void checkForReload()  { 
    long now = System.currentTimeMillis(); 
    if (now - lastRelodingTime > REFRESH_INTERVAL) { 
      lastRelodingTime = now; 
  public synchronized void forceReload()  { 
    lastRelodingTime = System.currentTimeMillis(); 
}/*********************** End of************************/ 

Look at the implementation, you can’t directly create object of the DataRepository Class. The
DataRepository class is an implementation of a singleton. So, there will be only one instance of the
DataRepository Class.
You should get the instance of the Class this way:
DataRepository instance = DataRepository.getInstance();

The logic here is, you create a single instance of the class. Load the data from database to Java data
structure inside the reload() method. The data loaded from the database is refreshed when a new
request is made and the refresh time interval expires. Now the object of the DataRepository Class is
loaded into the memory along with the data loaded from Database.
Suppose, a student with registration number = 101 is looking for his result. You can provide the
result without searching it again.
DataDTO studentInfo ;
long regNumber = 101;
studentInfo=(DataDTO) DataRepository.getInstance().getResult(regNumber);
String studentName = studentInfo.getName();
float studentCGPA = studentInfo.getCGPA();

If you are using STRUTS framework you can have Form Class that extends ActionForm. Say, you Form
class name is DataForm.
DataForm form = new DataForm();
Finally: Forward this DataForm to your Action class.
You can use this architecture in any desktop, web or other server type application based on your


Add comment

Security code

This website uses cookies to ensure you get the best experience on our website. Learn More.

Got It!