I'm using the Peewee ORM in my Python WSGI web app. I've been having issues with the MySQL Server "going away", so I switched to the PooledMySQLDatabase. I will still having issues with the server going away after a few hours..
According to the Docs, for best performance I must manage the connection(s) myself, by calling .connect() and .close(). I was not doing this at all.
I've added 2 decorators that get called before and after every request, but is this correct?
@pre_save()
def pre_save_handler(sender,instance,created):
db.connect()
logger.debug('Attempting to save %s' % instance)
@post_save()
def post_save_handler(sender, instance, created):
db.close()
logger.debug('%s was just saved' % instance)
Are there any pitfalls doing it this way? Should I move the .connect() and .close() into my application?
Most WSGI frameworks provide some sort of hook for running code before and after a request. I suggest using those hooks to connect/close.
Check out these docs: http://docs.peewee-orm.com/en/latest/peewee/database.html#adding-request-hooks