Domanda

is it possible in Db4o to load new objects into persistent IObjectContainer?

I have a desktop application which opens one connection (IObjectContainer) when started. if I query all objects with:

var objects = from DummyClass foo in session
              select foo

it selects all objects perfectly. However, if another client adds new classes after this, the same query still selects the same objects, without new ones.

I also know about:

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

but I don't have even not activated references to new objects so there. How to refresh new objects?

Just note: I don't want to open/close session every time I need some data, I want to take advantage of OODB (Transparent activation, object persistance since loaded etc.)

Thank you

UPDATE (code example for better understanding)

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

I need something like:

// refresh all objects of DummyClass
session.Ext().Refresh(typeof(DummyClass), int.MaxValue);
È stato utile?

Soluzione

You may use Commited events:

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;
        }
    }
}

Altri suggerimenti

Did your client call the commit() method after storing the data? Otherwise the new data will not be available for other clients.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top