2

在 Db4o 中是否可以将新对象加载到持久性 IObjectContainer 中?

我有一个桌面应用程序,它在启动时会打开一个连接(IObjectContainer)。如果我查询所有对象:

var objects = from DummyClass foo in session
              select foo

它完美地选择了所有对象。但是,如果另一个客户端在此之后添加了新类,则相同的查询仍然选择相同的对象,而没有新的对象。

我还知道:

session.Ext().Refresh(obj, int.MaxValue);

但我什至没有激活对新对象的引用,所以在那里。如何刷新新对象

请注意:我不想每次需要一些数据时都打开/关闭会话,我想利用 OODB(透明激活、加载后的对象持久性等)

谢谢

更新(代码示例以便更好地理解)

// store one class to fill database with some data
using (var mainSession = SessionFactory.CreateNewConnection())
{
    mainSession.Store(new DummyClass());
    mainSession.Commit();
}

using (var mainSession = SessionFactory.CreateNewConnection())
{
    // returns one object
    var objects = from DummyClass foo in session
                 select foo;
    using (var secondSession = SessionFactory.CreateNewConnection())
    {
        secondSession.Store(new DummyClass());
        secondSession.Commit();
    }

    // this loop reload objects known for mainSession (which is not new object)
    foreach (var obj in objects2)
    {
        mainSession.Ext().Refresh(obj, int.MaxValue);
    }

    // new DummyClass is commited but still not visible (Read-Commited isolation)
    // returns one object
    var objects2 = from DummyClass foo in session
                  select foo;
}

using (var mainSession = SessionFactory.CreateNewConnection())
{
    // returns two objects
    var objects = from DummyClass foo in session
                 select foo;
}

我需要类似的东西:

// refresh all objects of DummyClass
session.Ext().Refresh(typeof(DummyClass), int.MaxValue);
4

2 回答 2

1

您的客户在存储数据后是否调用了 commit() 方法?否则,其他客户端将无法使用新数据。

于 2011-11-18T21:17:59.817 回答
1

您可以使用已提交事件:

using Db4objects.Db4o;
using Db4objects.Db4o.Events;
using Db4objects.Db4o.IO;
using Db4objects.Db4o.Ext;

namespace PushedUpdates
{
    class Program
    {
        static void Main()
        {
            var config = Db4oEmbedded.NewConfiguration();
            config.File.Storage = new MemoryStorage();
            var container = Db4oEmbedded.OpenFile(config, "IN-MEMORY");

            var client = container.Ext().OpenSession();

            var clientEvents = EventRegistryFactory.ForObjectContainer(client);
            clientEvents.Committed += (s, a) =>
                                        {
                                            foreach(IObjectInfo added in a.Added)
                                            {
                                                System.Console.WriteLine(added.GetObject());
                                            }
                                        };

            container.Store(new Item { Value = 1 } );
            container.Commit();
            container.Store(new Item { Value = 2 });
            container.Commit();
            container.Store(new Item { Value = 3 });
            container.Commit();

            client.Close();
            container.Close();
        }
    }

    class Item
    {
        public int Value { get; set; }

        public override string ToString()
        {
            return "" + Value;
        }
    }
}
于 2011-11-18T22:49:28.883 回答