Skip to content

Commit

Permalink
Add database locking
Browse files Browse the repository at this point in the history
This should prevent two processes from using the same database at the
same time!
  • Loading branch information
mneumann committed Mar 12, 2012
1 parent 88a8624 commit 6fdebe3
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions lib/MMDB/DBMS.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ def initialize(dirname, readonly, schemas)
# XXX: Validate Schema
#if File.exist?(File.join(@dirname, "schema"))

@lock = File.open(File.join(@dirname, "lock"), File::RDWR|File::CREAT, 0644)

raise "Database locked" if not @lock.flock(File::LOCK_EX|File::LOCK_NB)

@commit_log = CommitLog.new(File.join(@dirname, "commit"))

# parse the commit record
Expand Down Expand Up @@ -101,6 +105,9 @@ def snapshot
def close
@dbs.each_value {|db| db.close}
@dbs = {}
@lock.flock(File::LOCK_UN)
@lock.close
@lock = nil
end

def get_db(dbid)
Expand Down

0 comments on commit 6fdebe3

Please sign in to comment.