在 Db4o 中是否可以将新对象加载到持久化 IObjectContainer 中?
我有一个桌面应用程序,它在启动时打开一个连接 (IObjectContainer)。如果我查询所有对象:
var objects = from DummyClass foo in session
select foo
它完美地选择了所有对象。但是,如果另一个客户端在此之后添加新类,则相同的查询仍会选择相同的对象,而不会选择新的对象。
我还知道:
session.Ext().Refresh(obj, int.MaxValue);
但我什至没有激活对新对象的引用。 如何刷新新对象?
请注意:我不想每次需要一些数据时都打开/关闭 session ,我想利用 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);
最佳答案
您可以使用Commited 事件:
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;
}
}
}
关于c# - Db4o - 刷新持久 session 中的所有新对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8189177/