2011-11-18 107 views
2

Db4o可以將新對象加載到持久IObjectContainer中嗎?Db4o - 刷新永久會話中的所有新對象

我有一個桌面應用程序,它在啓動時打開一個連接(IObjectContainer)。如果我查詢所有對象:

var objects = from DummyClass foo in session 
       select foo 

它完美地選擇所有對象。但是,如果另一個客戶端在此之後添加新的類,則相同的查詢仍將選擇相同的對象,而不會有新的類。

我也知道:

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

,但我沒有新的對象,因此,即使有未激活的引用。 如何刷新新對象

只要注意:我不想每次我需要一些數據時間打開/關閉會話,我想利用面向對象數據庫的(透明激活,對象持久性,因爲裝等)

謝謝

UPDATE(爲了更好地理解代碼示例)

// 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); 

回答

1

您可以使用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; 
     } 
    } 
} 
+0

+1的暗示,但我擔心的表現。針對所有客戶端的每次提交都會引發此事件 –

1

存儲數據後,客戶端是否調用了commit()方法?否則,新數據將不可用於其他客戶端。