Created
May 5, 2016 07:17
-
-
Save garylgh/6f40b10ccef09fb8678b3417d8eec047 to your computer and use it in GitHub Desktop.
Python事务装饰器
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- 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