Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.Sign up
Unable to get retry working with unique constraint failure. #313
I haven't been able to figure out how to get the retry parameter to work correctly with database errors. I can reproduce the test code with ZeroDivisionError exceptions, but not with database errors (such as a unique constraint violation).
#!/usr/bin/env python3 from pony import orm db = orm.Database() class Foo(db.Entity): unique = orm.Required(int, unique=True) @orm.db_session(retry=3) def retry_unique(): print("RETRY_UNIQUE") Foo(unique=1) @orm.db_session(retry=3, retry_exceptions=[ZeroDivisionError]) def retry_zero_div(): print("RETRY_ZERO_DIV") 1/0 def main(): db.bind(provider='postgres', user='postgres', password='password', host='127.0.0.1', database='test_retry') db.generate_mapping(create_tables=True) try: # This will run 4 times. retry_zero_div() except: pass try: # This will succeed (if db is empty). retry_unique() # This will only run once. <- Bug? retry_unique() except: pass if __name__ == '__main__': main()
Running this code on a clean database produces the following output:
I (correctly) see
When I trace
At this point, I'm not entirely sure if I'm doing something wrong or there is really a bug. Any advice would be appreciated.
Thanks for reporting! It was a bug.
Previous code for retry handling assumed that exception is raised inside a function. In your example, the exception was raised outside of the function, when