[英]Django connection pooling and time fields
Has anyone got connection pooling working with Django, SQLAlchemy, and MySQL?有没有人使用 Django、SQLAlchemy 和 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.我使用了本教程( http://node.to/wordpress/2008/09/30/another-database-connection-pool-solution-for-django-mysql/ )效果很好但我遇到的问题是我带回了一个时间字段,它正在转换为 timedelta,因为没有使用 Django 特定的转换。
Conversion code from django/db/backends/mysql/base.py来自 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')
In Django trunk, edit django/db/ init .py and comment out the line:在 Django 主干中,编辑 django/db/ init .py 并注释掉该行:
signals.request_finished.connect(close_connection)信号.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;另一个必要的更改是在 django/middleware/transaction.py; remove the two transaction.is_dirty() tests and always call commit() or rollback().
删除两个 transaction.is_dirty() 测试并始终调用 commit() 或 rollback()。 Otherwise, it won't commit a transaction if it only read from the database, which will leave locks open that should be closed.
否则,如果它只从数据库中读取,它将不会提交事务,这将使应该关闭的锁处于打开状态。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.