summaryrefslogtreecommitdiff
path: root/odoo/sql_db.py
diff options
context:
space:
mode:
Diffstat (limited to 'odoo/sql_db.py')
-rw-r--r--odoo/sql_db.py59
1 files changed, 24 insertions, 35 deletions
diff --git a/odoo/sql_db.py b/odoo/sql_db.py
index c7990b025ef..1d1f74748a0 100644
--- a/odoo/sql_db.py
+++ b/odoo/sql_db.py
@@ -22,6 +22,8 @@ from psycopg2.extensions import ISOLATION_LEVEL_AUTOCOMMIT, ISOLATION_LEVEL_READ
from psycopg2.pool import PoolError
from werkzeug import urls
+from odoo.api import Environment
+
psycopg2.extensions.register_type(psycopg2.extensions.UNICODE)
_logger = logging.getLogger(__name__)
@@ -58,23 +60,19 @@ import threading
from inspect import currentframe
-SENTINEL = object()
-
-
-def get_env(frame, back=0):
- """ Retrieve an environment from the given stack frame. """
- for i in range(back):
- frame = frame.f_back
- while frame:
- env = frame.f_locals.get('env', SENTINEL)
- if env is not SENTINEL:
- return env
- env = getattr(frame.f_locals.get('self'), 'env', SENTINEL)
- if env is not SENTINEL:
- return env
- frame = frame.f_back
- return None
+def flush_env(cr):
+ """ Retrieve and flush an environment corresponding to the given cursor """
+ for env in list(Environment.envs):
+ if env.cr is cr:
+ env['base'].flush()
+ break
+def clear_env(cr):
+ """ Retrieve and clear an environment corresponding to the given cursor """
+ for env in list(Environment.envs):
+ if env.cr is cr:
+ env.clear()
+ break
import re
re_from = re.compile('.* from "?([a-zA-Z_0-9]+)"? .*$')
@@ -386,9 +384,7 @@ class Cursor(object):
def commit(self):
""" Perform an SQL `COMMIT`
"""
- env = get_env(currentframe(), 2)
- if env is not None:
- env['base'].flush()
+ flush_env(self)
result = self._cnx.commit()
for func in self._pop_event_handlers()['commit']:
func()
@@ -398,9 +394,7 @@ class Cursor(object):
def rollback(self):
""" Perform an SQL `ROLLBACK`
"""
- env = get_env(currentframe(), 2)
- if env is not None:
- env.clear()
+ clear_env(self)
result = self._cnx.rollback()
for func in self._pop_event_handlers()['rollback']:
func()
@@ -428,17 +422,16 @@ class Cursor(object):
def savepoint(self, flush=True):
"""context manager entering in a new savepoint"""
name = uuid.uuid1().hex
- env = get_env(currentframe(), 2) if flush else None
- if env is not None:
- env['base'].flush()
+ if flush:
+ flush_env(self)
self.execute('SAVEPOINT "%s"' % name)
try:
yield
- if env is not None:
- env['base'].flush()
+ if flush:
+ flush_env(self)
except Exception:
- if env is not None:
- env.clear()
+ if flush:
+ clear_env(self)
self.execute('ROLLBACK TO SAVEPOINT "%s"' % name)
raise
else:
@@ -495,15 +488,11 @@ class TestCursor(object):
_logger.debug("TestCursor.autocommit(%r) does nothing", on)
def commit(self):
- env = get_env(currentframe(), 1)
- if env is not None:
- env['base'].flush()
+ flush_env(self)
self._cursor.execute('SAVEPOINT "%s"' % self._savepoint)
def rollback(self):
- env = get_env(currentframe(), 1)
- if env is not None:
- env.clear()
+ clear_env(self)
self._cursor.execute('ROLLBACK TO SAVEPOINT "%s"' % self._savepoint)
def __enter__(self):