2
我們使用momoko並具有以下標準設置爲異步連接在龍捲風應用到DB:在非阻塞客戶端的交易?
class BaseHandler(tornado.web.RequestHandler):
@property
def db(self):
# Create a database connection when a request handler is called
# and store the connection in the application object.
if not hasattr(self.application, 'db'):
self.application.db = momoko.AsyncClient({
'host': 'localhost',
'database': 'momoko',
'user': 'frank',
'password': '',
'min_conn': 1,
'max_conn': 20,
'cleanup_timeout': 10
})
return self.application.db
有一天,我發現像這樣的代碼,將阻止應用程序:
fail = yield gen.Task(self.db.execute, 'BEGIN; SELECT * FROM non_existing_table; END;')
第一個想法,這是進來,是:
try:
fail = yield gen.Task(self.db.execute, 'BEGIN; SELECT * FROM non_existing_table; END;')
except:
reconnect()
經過一些挖掘主題後,我發現最好做一些事情摹這樣的:
try:
fail = yield gen.Task(self.db.execute, 'BEGIN; SELECT * FROM non_existing_table; END;')
except:
yield gen.Task(self.db.execute, 'ROLLBACK;')
最後,探索桃子的source code後,我發現,這是更好地使用阻塞客戶端進行交易。
所以BaseHandler轉化爲:
class BaseHandler(tornado.web.RequestHandler):
@property
def db(self):
# Create a database connection when a request handler is called
# and store the connection in the application object.
if not hasattr(self.application, 'db'):
self.application.db = momoko.AsyncClient({
'host': 'localhost',
'database': 'momoko',
'user': 'frank',
'password': '',
'min_conn': 1,
'max_conn': 20,
'cleanup_timeout': 10
})
return self.application.db
@property
def bdb(self):
# Create a database connection when a request handler is called
# and store the connection in the application object.
if not hasattr(self.application, 'bdb'):
self.application.bdb = momoko.BlockingClient({
'host': 'localhost',
'database': 'momoko',
'user': 'frank',
'password': '',
'min_conn': 1,
'max_conn': 20,
'cleanup_timeout': 10
})
return self.application.bdb
現在我的問題...有沒有在AsyncClient
使用任何交易安全的方式?或者AsyncClient
通常用於從數據庫中讀取數據,而不是在那裏寫入/更新數據?
感謝您的偉大的驅動程序。 :) –