场景:在地球上任意找一个人然后让他找出他认识的任意五个人,这五个人成为初始条件……递归。
结果:通过这样的查询方式会把地球上的每个人都找出来。
假设:任何数据库执行这样的操作恐怕没有不崩溃的。
现实:Db4o数据库的默认查询方式就是这样的!查询一个对象的时候如果这个对象和其他对象有关联那么被关联的对象也一并查询出来。
Db4o通过 引入Activation-Concept理念 来解决上面的问题。解决问题的思路:限定级联的“级”数。
关键字:“级”,depth
Db4o已经使用了默认查询级联“级数”是 4 级——不包括自身在内。
改变级联“级数”的方法
private static void increaseActivationDepth() {
// #example: Increase the activation depth to 10
EmbeddedConfiguration configuration = Db4oEmbedded.newConfiguration();
configuration.common().activationDepth(10);
ObjectContainer container = Db4oEmbedded.openFile(configuration,DATABASE_FILE);
// #end example
try {
Person joelle = queryForJoelle(container);
Person julia = joelle.getMother().getMother().getMother().getMother().getMother();
boolean isActivated = container.ext().isActive(julia);
System.out.println("Is activated? "+ isActivated);
} finally {
container.close();
}
}
private static EmbeddedConfiguration moreActivationOptions() {
// #example: More activation options
EmbeddedConfiguration configuration = Db4oEmbedded.newConfiguration();
// At least activate persons to a depth of 10
configuration.common().objectClass(Person.class).minimumActivationDepth(10);
// Or maybe we just want to activate all referenced objects
configuration.common().objectClass(Person.class).cascadeOnActivate(true);
// #end example
return configuration;
}
db4o向数据库中存储未发现有“级数”限制
db4o级联更新要明确指定是否可级联更新,默认不能级联更新。
//修改数据库配置对每个操作的级联更新级数设置为2
EmbeddedConfiguration configuration = Db4oEmbedded.newConfiguration();
configuration.common().updateDepth(2);
//对“类”设置级联:
EmbeddedConfiguration configuration = Db4oEmbedded.newConfiguration();
// Update all referenced objects for the Driver class
configuration.common().objectClass(Driver.class).cascadeOnUpdate(true);
看看Db4o官方教程上是怎么说的 Db4o Reference book (java) Activation
Db4o的官方教程是一个“家谱”的例子

以上是不设置级联“级数”的场景

设置级联“级数”的场景
本文探讨了Db4o数据库在级联查询时可能遇到的性能问题,特别是当查询深度无限时,可能导致数据库崩溃。文章介绍了Db4o如何通过引入Activation-Concept理念和限定级联查询的级数来解决这一问题,并提供了调整查询深度和级联更新级数的代码示例。

被折叠的 条评论
为什么被折叠?



