Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django connection pooling and time fields

Has anyone got connection pooling working with Django, SQLAlchemy, and MySQL?

I used this tutorial (http://node.to/wordpress/2008/09/30/another-database-connection-pool-solution-for-django-mysql/) which worked great but the issue I'm having is whenever I bring back a time field it is being converted to a timedelta since the Django-specific conversions are not being used.

Conversion code from django/db/backends/mysql/base.py

django_conversions = conversions.copy()
django_conversions.update({
FIELD_TYPE.TIME: util.typecast_time,
FIELD_TYPE.DECIMAL: util.typecast_decimal,
FIELD_TYPE.NEWDECIMAL: util.typecast_decimal,

})

Connection code from article:

if settings.DATABASE_HOST.startswith('/'):
            self.connection = Database.connect(port=kwargs['port'], 
                                               unix_socket=kwargs['unix_socket'], 
                                               user=kwargs['user'], 
                                               db=kwargs['db'], 
                                               passwd=kwargs['passwd'], 
                                               use_unicode=kwargs['use_unicode'], 
                                               charset='utf8')
        else:
            self.connection = Database.connect(host=kwargs['host'], 
                                               port=kwargs['port'], 
                                               user=kwargs['user'], 
                                               db=kwargs['db'], 
                                               passwd=kwargs['passwd'], 
                                               use_unicode=kwargs['use_unicode'], 
                                               charset='utf8')
like image 332
Colin Humber Avatar asked Mar 20 '09 20:03

Colin Humber


1 Answers

In Django trunk, edit django/db/init.py and comment out the line:

signals.request_finished.connect(close_connection)

This signal handler causes it to disconnect from the database after every request. I don't know what all of the side-effects of doing this will be, but it doesn't make any sense to start a new connection after every request; it destroys performance, as you've noticed.

Another necessary change is in django/middleware/transaction.py; remove the two transaction.is_dirty() tests and always call commit() or rollback(). Otherwise, it won't commit a transaction if it only read from the database, which will leave locks open that should be closed.

like image 64
Vivek S Avatar answered Oct 06 '22 01:10

Vivek S