tags:

views:

34

answers:

1

How can I know when a db4o started from code has finished?

Doc mentions it does the backup in a separate thread, but doesn't mention anything about when it finishes. The method doesn't receives any callback.

+2  A: 

As far as I know this is not possible =(. When you try to run a back-up while one is allready in progress, you get an BackupInProgressException. This way you know that there's allready a backup in progress.

However that isn't a real notification and not usfull in many situations.

However there's a complex work-around. You can provide your own storage-implementation to the backup-process:

IStorage myStorage = ...; container.Ext().Backup(myStorage, "backup.db4o.bak");

This way you can implement a wrapper-storage which notifies you. You build a decorator which implements the IStorage-interface. The IBin-instances which are returned then notify you when closing. When the backup calls the close-methon on the IBin-instance you know that it is done.

Here's a draft how it's done. (Not testet or anything). The StorageDecorator is a base-class for IStorage-decorators, which is included in db4o.

class NotificationStorageDecorator : StorageDecorator
{
    public NotificationStorageDecorator(IStorage storage) : base(storage)
    {

    }

    public NotificationStorageDecorator() : this(new FileStorage()){}

    protected override IBin Decorate(BinConfiguration config, IBin bin)
    {
        return new MyNotificationBin(bin);    
    }

    class MyNotificationBin : BinDecorator
    {
        public MyNotificationBin(IBin bin) : base(bin)
        {
        }

        public override void Close()
        {
            base.Close();
            // Implement here your notification-mechanism
            // For example with events etc
            Console.Out.WriteLine("Backup-done");
        }
    }
}

// then use it

            var notifactionStorage = new NotificationStorageDecorator();
            container.Ext().Backup(notifactionStorage,"backup.db4o.back");
Gamlor
+1 @Gamlor I find this to be a very reasonable work around. If no one else comes with something, I'll definitely try this one.
eglasius