Locks and transactions for dummies

Matt Mackall mpm at selenic.com
Mon Jul 20 20:24:16 CDT 2009

On Mon, 2009-07-20 at 20:27 -0400, Greg Ward wrote:
> Hi all --
> I'm plugging away at adding a new Mercurial backend to cvs2svn,
> creating a real cvs2hg in the process.  Since this is my first attempt
> to write to a repo, I have all sorts of beginner questions about locks
> and transactions.  Reading the code is somewhat helpful, but not
> helpful enough.  ("How?" is not the same as "Why?".)  I've searched
> the wiki and am not turning up much.
> So: have I missed something?  Here are my main questions right now, to
> give you an idea of what's stumping me.
> 1) How do you commit a transaction?  I've copied the "finally: del tr"
> pattern from localrepository.addchangegroup(), so every transaction is
> successfully aborted.  Great.  Now what if there was no error and I
> want to commit it?


> 2) How many transactions should I have?  I could do one giant
> transaction for the whole cvs->hg conversion, or one per changeset.
> Which is better and why?

It's faster to have just one, but if the transaction is aborted, you're
rolled all the way back to the start.

> 3) Presumably I need to do locking: don't want anyone committing to my
> conversion target while the conversion is running.  But how many
> locks, i.e. one for the whole conversion or one per changeset?  And
> what's the difference between repo.lock() and repo.wlock(), anyways?

lock covers the repository, wlock covers the working directory state.
Generally you only need to take these locks when writing.

You probably want to hold the lock across the whole operation - you
don't really want people adding commits between csets.

http://selenic.com : development and support for Mercurial and Linux

More information about the Mercurial-devel mailing list