Db4o的级联(Activation-Concept)概念

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

场景:在地球上任意找一个人然后让他找出他认识的任意五个人,这五个人成为初始条件……递归。

结果:通过这样的查询方式会把地球上的每个人都找出来。

假设:任何数据库执行这样的操作恐怕没有不崩溃的。

现实: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的官方教程是一个“家谱”的例子

without-activation.png

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

with-activation_614x464.png

设置级联“级数”的场景

转载于:https://www.cnblogs.com/ponyblog/archive/2012/01/16/2323510.html

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值