Better use of context managers for database transactions