Threadsafe database transactions