Add lock around write operations in graph

The graph uses a persistent database connection so
a lock is required because our current sqlite3 driver
does not implement retry logic when the ErrBusy is received.
Merge pull request #2551 from dotcloud/concurrent-db-access

Add lock around write operations in graph