【发布时间】:2013-03-22 10:45:42
【问题描述】:
我有一个会话范围的 bean,它读取视图的内容并存储 SelectItem 对象的哈希图。这些然后用于填充 XPage 上的组合框值。
代码运行良好,除非我尝试在最内层循环中回收 ViewEntry 对象。如果该行被注释掉,我的 SelectItems 会很好地返回。如果该行未注释,则我的组合框为空。所以我想知道的是:
- 为什么会这样?
- 如何解决此问题并像优秀的开发人员一样正确回收对象?
- 如果我不能明确回收对象,对内存等有什么潜在影响?
代码(突出显示问题行且当前已注释掉):
public class comboBox extends HashMap<String, List<SelectItem>> implements Serializable {
private static final long serialVersionUID = 1L;
public comboBox() {
this.init();
}
private void init() {
Database database = null;
View vwData = null;
ViewNavigator navData = null;
try {
PrimeUser thisUser = (PrimeUser) resolveVariable(FacesContext.getCurrentInstance(), "PrimeUser");
String lang = thisUser.getLang();
database = getCurrentDatabase();
vwData = database.getView("DataLookup");
navData = vwData.createViewNavFromCategory(lang);
ViewEntry keyCat = navData.getFirst();
ViewEntry nextCat = null;
while (keyCat != null) {
String thisKey = (String) keyCat.getColumnValues().get(1);
List<SelectItem> options = new ArrayList<SelectItem>();
options.add(new SelectItem("Please select..."));
ViewEntry dataEntry = navData.getChild(keyCat);
ViewEntry nextChild = null;
while (dataEntry != null) {
nextChild = navData.getNextSibling();
Object optValue = dataEntry.getColumnValues().get(2);
String optLabel = (String) dataEntry.getColumnValues().get(3);
options.add(new SelectItem(optValue, optLabel));
// dataEntry.recycle(); //<---- PROBLEM HERE
dataEntry = nextChild;
}
this.put(thisKey, options);
nextCat = navData.getNextCategory();
keyCat.recycle();
keyCat = nextCat;
}
} catch (NotesException e) {
e.printStackTrace();
} finally {
try {
navData.recycle();
vwData.recycle();
database.recycle();
} catch (NotesException e) {
}
}
}
public List<SelectItem> get(String key) {
return this.get(key);
}
}
已解决:
解决方案非常简单(感谢 Sven Hasselbach 提醒我再次查看服务器日志输出)。更改很简单 - 换行:
nextCat = navData.getNextCategory();
为此:
nextCat = getNextSibling(keyCat);
虽然“getNextCategory”是ViewNavigator的一个方法,没有任何参数;它只获取当前条目之后的下一个类别。如果当前条目已被回收 - 就像上面的情况一样 - 那么显然当前条目为 null 并且该方法会引发异常。
“getNextSibling”有一个可选的 ViewEntry 参数,如果指定,它将覆盖指向当前 ViewEntry 的指针,否则它将使用。
我责怪星期五。
【问题讨论】:
-
Domino 对象不能被序列化。因此,无法保证将代码放入 sessionScope 中会按预期工作。
-
没有一个 Domino 对象被保存为 bean 的属性,所以据我所知,序列化不是问题。但是该 bean 已经在 viewScope 中进行了测试,结果相同。