views:

272

answers:

3

I have written a database application using a binary file as storage. it is accessed via powershell cmdlets.

You can put information into the database using the put- and you can read information using get-.

The problem is synchronisation. What is the best way to ensure that the cmdlets don't access the file at the same time?

The put- must have exclusive access ie no other writers or readers can access the file. The get- doesn't need exclusive access or readers can access the database at the same time.

Am I best using a file based locking mechanism or a .NET based synchronisation mechanism?

A: 

I think a mutex would work well here.

Will
A: 

I'm not aware of any .NET-based synchronization mechanisms that would be appropriate here (read on - I'm making an assumption that may not be true).

If you absolutely must have exclusive access for a "put," then either (a) file-based locking, or (b) database-based locking might be appropriate. For example, a "put" command might first check a special database field to see if it can gain exclusive access; if it can, it would update that field to show that exclusive access is already taken, preventing another simultaneous put from happening.

If I'm interpreting your question correctly, there's a possibility for this database to be accessed by multiple computers, meaning a mutex wouldn't really work since they're designed for inter-thread synchronization. If that's not the case and only a single computer would be accessing the file at any given time, then a mutex would work well.

Don Jones
A: 

I think the mutex method will have to do for now. Whilst PowerShell v2 does allow for easy remote exectution of scripts, I think that worrying about that at the moment would just get in the way of a quick implementation for PowerShell v1.

The database I'm using is just a plain old binary file so unfortunately I can't expect the database to do the synchronisation for me.

With the mutex, as lots of databases can be interacted with at the same time, do I need to use a named mutex, using the file path as the name, so that I can guarantee that the mutexes are unique for each database?

Jack Hughes
You'll need *some* kind of identifier, and the file path might be the best and easiest so that you don't have to maintain some kind of index.
Don Jones