From 750095459b4f31ade70a65fc89e7a2084e4cbb50 Mon Sep 17 00:00:00 2001 From: Kiall Mac Innes Date: Thu, 17 Jul 2014 11:49:50 +0100 Subject: [PATCH] Ensure each greenthread uses it's own Session instance This uses a thread local store, allowing each greenthread to have it's own session stored correctly. Without this, each greenthread may end up using a single global session, which leads to bad things happening. Change-Id: I1ceb33d2bcb531d444ad05cf6cf74b6edd4e7d39 Closes-Bug: 1343211 --- designate/storage/impl_sqlalchemy/__init__.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/designate/storage/impl_sqlalchemy/__init__.py b/designate/storage/impl_sqlalchemy/__init__.py index e357abc3..57d7bb8c 100644 --- a/designate/storage/impl_sqlalchemy/__init__.py +++ b/designate/storage/impl_sqlalchemy/__init__.py @@ -14,6 +14,7 @@ # License for the specific language governing permissions and limitations # under the License. import time +import threading from oslo.config import cfg from oslo.db.sqlalchemy import utils as oslo_utils @@ -32,6 +33,7 @@ from designate.sqlalchemy.models import SoftDeleteMixin LOG = logging.getLogger(__name__) +LOCAL_STORE = threading.local() cfg.CONF.register_group(cfg.OptGroup( name='storage:sqlalchemy', title="Configuration for SQLAlchemy Storage" @@ -70,7 +72,19 @@ class SQLAlchemyStorage(base.Storage): super(SQLAlchemyStorage, self).__init__() self.engine = session.get_engine(self.name) - self.session = session.get_session(self.name) + + @property + def session(self): + # NOTE: This uses a thread local store, allowing each greenthread to + # have it's own session stored correctly. Without this, each + # greenthread may end up using a single global session, which + # leads to bad things happening. + global LOCAL_STORE + + if not hasattr(LOCAL_STORE, 'session'): + LOCAL_STORE.session = session.get_session(self.name) + + return LOCAL_STORE.session def begin(self): self.session.begin(subtransactions=True)