简体   繁体   English

从db4o到db4o复制的复制不完全

[英]db4o to db4o replication not quite replicating

I am having trouble getting replication to function completely between two db4o databases. 我无法使复制在两个db4o数据库之间完全起作用。 I have followed many tutorials and my code seems to be on par with them (well obviously not though). 我遵循了许多教程,并且我的代码似乎与它们相当(显然不是)。 The output suggests that the ReplicationSession is detecting changes but it isn't replicating the changes in the other database. 输出表明ReplicationSession正在检测更改,但未在其他数据库中复制更改。

private ReflectiveDatabase()
{
    openDb();
    providerA = new Db4oEmbeddedReplicationProvider(hostContainer);
    providerB = new Db4oEmbeddedReplicationProvider(clientContainer);

    //Start a new ReplicationSession with event for replacing newest object on conflict.
    replication = Replication.begin(providerA, providerB,
    new ReplicationEventListener() {
        @Override
        public  void onReplicate(ReplicationEvent replicationEvent) {
            if (replicationEvent.isConflict()) {
                ObjectState stateDesktop = replicationEvent.stateInProviderA();
                ObjectState stateMobile = replicationEvent.stateInProviderB();

                if (stateDesktop.modificationDate() >= stateMobile.modificationDate()) {
                    replicationEvent.overrideWith(stateDesktop);
                } else {
                    replicationEvent.overrideWith(stateMobile);
                }
            }
        }
    });   
}

public EmbeddedConfiguration configure()
{
    EmbeddedConfiguration configuration = Db4oEmbedded.newConfiguration();
    configuration.file().generateUUIDs(ConfigScope.GLOBALLY);
    configuration.file().generateCommitTimestamps(true);
    return configuration;
}

public void openDb()
{
    // try to connect to the host
    if(hostContainer != null) hostContainer.close();

    try
    {
        hostContainer = Db4oEmbedded.openFile(configure(), "local1.db4o");
    }
    catch (com.db4o.ext.Db4oIOException e)
    {
        ...
    }

    // try to connect to the client
    if(clientContainer != null) 
    {
        clientContainer.close();
    }

    try
    {
        clientContainer = Db4oEmbedded.openFile(configure(), "local2.db4o");
    }
    catch (com.db4o.ext.Db4oIOException e)
    {
        ...
    }
}

And here is the actual syncing that I have been running from a timer every 8s 这是我每8秒从一个计时器运行一次的实际同步

public void syncDatabases()
{        
    // First get the changes of the two replication-partners
    ObjectSet<Object> changesOnHost = replication.providerA().objectsChangedSinceLastReplication();
    ObjectSet<Object> changesOnClient = replication.providerB().objectsChangedSinceLastReplication();
    System.out.println("Changes on Server: " + changesOnHost.size());
    System.out.println("Changes on Client: " + changesOnClient.size());
    // then iterate over both change-sets and replicate it
    for (Object changedObjectOnClient : changesOnClient)
    {
        replication.replicate(changedObjectOnClient);
    }
    for (Object changedObjectOnHost : changesOnHost)
    {
        replication.replicate(changedObjectOnHost);
    }
    replication.commit();
}

public void writeToClient(Object object)
{
    clientContainer.store(object);
    clientContainer.commit();
}

Works just fine for new objects created and written to a database. 对于创建并写入数据库的新对象而言,它工作得很好。

If I write a changed object from one of these databases, such as a field change, the sync method when run will pick up that there is a changed object and it is in fact the correct one and its field has been changed. 如果我从这些数据库之一写入更改的对象(例如字段更改),则运行时sync方法将发现存在更改的对象,而实际上是正确的对象及其字段已更改。 However, I am not seeing that object being replicated in the other database. 但是,我没有看到该对象在另一个数据库中被复制。 Its field is not the same as the changed object's field. 它的字段与更改的对象的字段不同。

Do I simply have a misconception about db4o's replication abilities? 我是否只是对db4o的复制能力有误解? This is a bit out of my league as a 2nd year but if anyone can see where I am going wrong I would greatly appreciate it. 这已经是我第二年的比赛了,但是如果有人能看到我要去哪里错了,我将不胜感激。

I managed to get the version 8 to replicate correctly. 我设法使版本8能够正确复制。 I was not following the tutorial exactly and made assumptions (Although without clarification it is still an assumption). 我没有完全按照本教程进行操作并做出了假设(尽管没有澄清,但这仍然是一个假设)。

It seems that instantiating the ReplicationSession and the ReplicationProviders at the time of syncing is critical. 似乎在同步时实例化ReplicationSession和ReplicationProviders是至关重要的。 I changed my code so that at SyncDatabases() these are created and not kept (And I guess garbage collection takes care of them). 我更改了代码,以便在SyncDatabases()上创建并保留它们(我想垃圾回收会解决它们)。

public void syncDatabases()
{        
    // First get the changes of the two replication-partners
    Db4oEmbeddedReplicationProvider providerA = new Db4oEmbeddedReplicationProvider(hostContainer);
    Db4oEmbeddedReplicationProvider providerB = new Db4oEmbeddedReplicationProvider(clientContainer);
    ReplicationSession replication = Replication.begin(providerA, providerB);

    ObjectSet<Object> changesOnHost = replication.providerA().objectsChangedSinceLastReplication();
    ObjectSet<Object> changesOnClient = replication.providerB().objectsChangedSinceLastReplication();

    // then iterate over both change-sets and replicate it
    for (Object changedObjectOnClient : changesOnClient)
    {
        replication.replicate(changedObjectOnClient);
    }
    for (Object changedObjectOnHost : changesOnHost)
    {
        replication.replicate(changedObjectOnHost);
    }
    replication.commit();

    replication.replicateDeletions(Object.class);
}

The constructor just runs openDb() to get the objectContainers initialised. 构造函数只运行openDb()即可初始化objectContainers。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM