From 8e5be21524a6d3fed1a6031e8f931e3877087269 Mon Sep 17 00:00:00 2001 From: wangxiyuan Date: Thu, 2 Jun 2016 09:23:00 +0800 Subject: [PATCH] Avoid multi-threading problem with sql backend Now zaqar's sql backend always use only one connection to db. It will lead an multi-thread problem: pymysql.err.InternalError) Packet sequence number wrong So we should use session in sqlalchemy to avoid this happen. Change-Id: I84e2f9899e141aed151f7c06fc4b1f603481886e Closes-bug: #1588117 --- zaqar/storage/sqlalchemy/driver.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/zaqar/storage/sqlalchemy/driver.py b/zaqar/storage/sqlalchemy/driver.py index 41dd83b59..942f05dc0 100644 --- a/zaqar/storage/sqlalchemy/driver.py +++ b/zaqar/storage/sqlalchemy/driver.py @@ -14,6 +14,8 @@ # the License. import sqlalchemy as sa +from sqlalchemy.orm import scoped_session +from sqlalchemy.orm import sessionmaker from zaqar.common import decorators from zaqar import storage @@ -63,9 +65,13 @@ class ControlDriver(storage.ControlDriverBase): # TODO(cpp-cabrera): expose connect/close as a context manager # that acquires the connection to the DB for the desired scope and # closes it once the operations are completed + # TODO(wangxiyuan): we should migrate to oslo.db asap. @decorators.lazy_property(write=False) def connection(self): - return self.engine.connect() + # use scoped_session to avoid multi-threading problem. + session = scoped_session(sessionmaker(bind=self.engine, + autocommit=True)) + return session() def run(self, *args, **kwargs): return self.connection.execute(*args, **kwargs)