Issue #659 resolved

recipe for having reliable db connections per thread

Anonymous created an issue


This is a recipe proposal (couldn't find how to login to the Wiki).

I had problems using cherrypy.thread_data.db.cursor(): it is unreliable when running some heavy SQL queries. There were plenty of malloc errors (double free), dropped connections etc, even when running with 2 CherryPy threads against local database.

May I suggest following code:

import cherrypy import MySQLdb from DBUtils.PersistentDB import PersistentDB

class HomePage: @cherrypy.expose def index(self): sql = "select max(whatever) from bigtable;" #long running query c = persDb.connection().cursor() c.execute(sql) res = c.fetchone() c.close() return ' '.join( ['<html>', repr(res), '</html>'] )

root = HomePage() cherrypy.tree.mount(root)

if name == 'main':
persDb=PersistentDB(MySQLdb,10000,host='localhost', db='db', passwd='****')

cherrypy.config.update({'server.thread_pool': 10})

Cheers, Alex

Reported by Alexandru Toth