比如考虑用户的注册的情况,先根据用户名判断数据库里有没有同用户名称的,如果没有的话创建用户。
请问各位,这种情形需不需要加数据库事务?并限定事务的级别。我考察了几个开源的代码发现都没有加数据库事务。下面是 reddit 的代码:
def register(name, password, registration_ip):
try:
a = Account._by_name(name)
raise AccountExists
except NotFound:
a = Account(name = name, password = bcrypt_password(password))
# new accounts keep the profanity filter settings until opting out
a.pref_no_profanity = True
a.registration_ip = registration_ip
a._commit()
return a