Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/retry delay #688

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# PonyORM release 0.7.17 (2023-06-15)

* Introduce retry_delay into retry db_session

# PonyORM release 0.7.16 (2022-01-28)

## Bugfixes
Expand Down
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ Pony is an advanced object-relational mapper. The most interesting feature of Po

Here is an example query in Pony:

select(p for p in Product if p.name.startswith('A') and p.cost <= 1000)
```python
select(p for p in Product if p.name.startswith('A') and p.cost <= 1000)
```

Pony translates queries to SQL using a specific database dialect. Currently Pony works with SQLite, MySQL, PostgreSQL and Oracle databases.

Expand Down Expand Up @@ -62,4 +64,4 @@ Meet the PonyORM team, chat with the community members, and get your questions a
Join our newsletter at [ponyorm.org](https://ponyorm.org).
Reach us on [Twitter](https://twitter.com/ponyorm).

Copyright (c) 2013-2022 Pony ORM. All rights reserved. info (at) ponyorm.org
Copyright (c) 2013-2023 Pony ORM. All rights reserved. info (at) ponyorm.org
2 changes: 1 addition & 1 deletion pony/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import os, sys
from os.path import dirname

__version__ = '0.7.16'
__version__ = '0.7.17'

def detect_mode():
try: import google.appengine
Expand Down
12 changes: 9 additions & 3 deletions pony/orm/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import builtins, json, re, sys, types, datetime, logging, itertools, warnings, inspect, ast
from operator import attrgetter, itemgetter
from itertools import chain, starmap, repeat
from time import time
from time import time, sleep
from decimal import Decimal
from random import shuffle, randint, random
from threading import Lock, RLock, current_thread, _MainThread
Expand Down Expand Up @@ -407,23 +407,28 @@ def rollback():
select_re = re.compile(r'\s*select\b', re.IGNORECASE)

class DBSessionContextManager(object):
__slots__ = 'retry', 'retry_exceptions', 'allowed_exceptions', \
__slots__ = 'retry', 'retry_delay', 'retry_exceptions', 'allowed_exceptions', \
'immediate', 'ddl', 'serializable', 'strict', 'optimistic', \
'sql_debug', 'show_values'
def __init__(db_session, retry=0, immediate=False, ddl=False, serializable=False, strict=False, optimistic=True,
def __init__(db_session, retry:int=0, retry_delay:int=0, immediate:bool=False, ddl:bool=False, serializable:bool=False, strict:bool=False, optimistic:bool=True,
retry_exceptions=(TransactionError,), allowed_exceptions=(), sql_debug=None, show_values=None):
if retry != 0:
if type(retry) is not int: throw(TypeError,
"'retry' parameter of db_session must be of integer type. Got: %s" % type(retry))
if type(retry_delay) is not int: throw(TypeError,
"'retry_delay' parameter of db_session must be of integer type. Got: %s" % type(retry))
if retry < 0: throw(TypeError,
"'retry' parameter of db_session must not be negative. Got: %d" % retry)
if retry_delay < 0: throw(TypeError,
"'retry_delay' parameter of db_session must not be negative. Got: %d" % retry)
if ddl: throw(TypeError, "'ddl' and 'retry' parameters of db_session cannot be used together")
if not callable(allowed_exceptions) and not callable(retry_exceptions):
for e in allowed_exceptions:
if e in retry_exceptions: throw(TypeError,
'The same exception %s cannot be specified in both '
'allowed and retry exception lists simultaneously' % e.__name__)
db_session.retry = retry
db_session.retry_delay = retry_delay
db_session.ddl = ddl
db_session.serializable = serializable
db_session.immediate = immediate or ddl or serializable or not optimistic
Expand Down Expand Up @@ -533,6 +538,7 @@ def new_func(func, *args, **kwargs):
if not do_retry:
raise
rollback()
sleep(db_session.retry_delay)
finally:
db_session.__exit__(exc_type, exc, tb)
reraise(exc_type, exc, tb)
Expand Down