【发布时间】:2011-11-09 23:12:04
【问题描述】:
我有一个 Java Web 服务器应用程序,它每 10 秒左右将对象更新到数据库中。
我注意到在更新时,即使没有应用任何更改,几乎每次数据库的大小都会增长几 KB。
我在这里使用了他们的示例: http://community.versant.com/documentation/reference/db4o-8.0/java/tutorial/docs/Structured.html#outline39
我尝试defrag 数据库,但这并没有减少到其原始大小,其中包含相同数量的对象。
谁能告诉我如何防止这种情况/我可能做错了什么?
编辑:
这是我更新或存储对象(在本例中为设备)的代码部分:
try {
LinkedList<Device> lDevices = Sense.getDevices();
if (lDevices.size() == 0) {
return;
}
db = Db4o.getDatabaseClient();
for (Device device : lDevices) {
List<Device> query = db.queryByExample(new Device(device.getDeviceId()));
if (query.size() == 1) {
//store changes
Device found = query.get(0);
found.setBatteryLevel(device.getBatteryLevel());
found.setLastknownLocation(device.getLastknownLocation());
found.setType(device.getType());
db.store(found);
} else {
// store new Device
db.store(device);
}
}
} catch (Exception e) {
} finally {
if (db instanceof ObjectContainer) {
db.close();
}
}
但现在我发现使用 Eclipse 插件 db4o 数据库浏览器,设备对象中的 Location 类已更新并再次存储在数据库中。
所以我一开始在数据库中有 2 个设备和 2 个位置,第一次更新后数据库中有 2 个设备和 4 个位置!!这让我发疯了!
有人知道为什么会这样吗?
【问题讨论】:
-
如果 device.getLastknownLocation() 返回的对象不是从数据库中检索到的,它将被存储(因为 db4o 认为它是一个新对象)。
-
好的,我找到的解决方案是需要先删除in-dept-objects,否则它们将被存储两次。感谢 Vagaus 和 Gamlor 的帮助!