Merge "Ensure each greenthread uses it's own Session instance"

This commit is contained in:
Jenkins 2014-07-17 16:37:44 +00:00 committed by Gerrit Code Review
commit c024ea3848

View File

@ -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"
@ -78,7 +80,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)