From traditional Notes development we learned that retrieving Domino objects like databases and views in script was not effective, and should be avoided in loops.
In XPages we cannot serialize Domino objects and often we retrieve the same object many times. We have an example where we are retrieving project data, based on project number stored in project related documents. The bean is scoped to applicationScope
and results are cached.
public class Projects{
private TreeMap<String, Project> projectList;
public Projects() {
}
public Project getProjectInfo(String projNum) {
Project project = null;
if (projectList==null) {
projectList = new TreeMap<String,Project>();
}
if (projectList.containsKey(projNum)) {
project = projectList.get(projNum);
} else {
try {
Database projDb = DominoAccess.getDatabase("projects");
View v = projDb.getView("(projLookup)");
ViewEntry ve = v.getEntryByKey(projNum);
if (ve != null) {
project = new Project(ve);
projectList.put(projNum, project);
}
} catch (Exception e) {
}
}
return project;
}
}
When this is first used in for example a repeat, the database and view objects are created for each document. Is this best practice or are there better ways of solving this?
I know we can put all projects in the Map at first use, but also not sure if this is best practice regarding memory?