Skip to content

Instantly share code, notes, and snippets.

@garylgh
Created May 5, 2016 07:17
Show Gist options
  • Save garylgh/6f40b10ccef09fb8678b3417d8eec047 to your computer and use it in GitHub Desktop.
Save garylgh/6f40b10ccef09fb8678b3417d8eec047 to your computer and use it in GitHub Desktop.
Python事务装饰器
# -*- coding: utf-8 -*-
def transactional():
def transaction(func):
def __decorator(obj,*args,**kwargs):
try:
func(obj,*args,**kwargs)
print 'do commit'
obj.session.commit()
except Exception,e:
obj.session.rollback()
finally:
obj.session.close()
return __decorator
return transaction
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment