2
我開始2個龍捲風實例,例如。 tornado-8000和tornado-8001,使用nginx上行。多龍捲風實例之間的Sqlalchemy查詢不一致
當我在一個實例中插入數據時,對另一個實例的查詢無法找到此數據。
的SQLAlchemy的查詢如下:
topics = self.db.query(Topic).order_by(Topic.updated_at.desc()).limit(20)
而我的SQLAlchemy被inited如下:
class Application(tornado.web.Application):
def __init__(self):
# setup app
from urls import handlers,ui_modules
settings = dict(
debug = options.debug,
static_path = os.path.join(root_dir, "static"),
xsrf_cookies = True,
cookie_secret = "nzjxcjasduuqwheazmu293nsadhaslzkci9023nsadnua9sdads/Vo=",
ui_modules = ui_modules,
)
tornado.web.Application.__init__(self, handlers, **settings)
self.db = scoped_session(sessionmaker(bind=engine,autocommit=False,autoflush=False))
BaseHandler:
class BaseHandler(web.RequestHandler):
@property
def db(self):
return self.application.db
我插入:
topic = Topic()
topic.title = u'abcdefg'
self.db.add(topic)
self.db.commit()
錯誤在哪裏?
在查詢「Topic」之前,嘗試「commit()」或「rollback()」另一個實例。如果這樣做,那就是導致你的承諾對象「隱形」的事務隔離。 – van
@van很感謝你,這是我的錯。我應該重寫tornado.web.RequestHandler中的on_finish方法並在其中執行session.remove()。因爲會話沒有被刪除,所以它是孤立的。感謝您的建議。 – goofansu
太棒了。請在解決方案中添加答案並接受。 – van