evolve: improve transaction related code to use the lock module
Refactored some of the places where we are using locking in evolve to use
the lock module and write less code.
This should also fix a couple of bug where tr could be None at release time.