#35702: Clarify mysqlclient does not provide connection pooling --------------------------------------+------------------------------------ Reporter: Hisham Mahmood | Owner: (none) Type: Cleanup/optimization | Status: new Component: Documentation | Version: dev Severity: Normal | Resolution: Keywords: | Triage Stage: Accepted Has patch: 0 | Needs documentation: 0 Needs tests: 0 | Patch needs improvement: 0 Easy pickings: 0 | UI/UX: 0 --------------------------------------+------------------------------------ Changes (by Simon Charette):
* stage: Unreviewed => Accepted Comment: The note about `mysqlclient` being thread safe and implementing connection pooling was inherited in #23446 (7f089ac2e3e6a7f4b2b41085a37d35e074fad805). The thread safety and connection pooling claims were added for `MySQLDB` [https://github.com/django/django/pull/1906/files originally] but they are certainly not true for either `pymysql` or `mysqlclient`. As pointed out in [https://mysqlclient.readthedocs.io/user_guide.html their docs] > The MySQL protocol can not handle multiple threads using the same connection at once. Some earlier versions of MySQLdb utilized locking to achieve a threadsafety of 2. While this is not terribly hard to accomplish using the standard Cursor class (which uses mysql_store_result()), it is complicated by SSCursor (which uses mysql_use_result(); with the latter you must ensure all the rows have been read before another query can be executed. It is further complicated by the addition of transactions, since transactions start when a cursor executes a query, but end when COMMIT or ROLLBACK is executed by the Connection object. Two threads simply cannot share a connection while a transaction is in progress, in addition to not being able to share it during query execution. This excessively complicated the code to the point where it just isn’t worth it. > > The general upshot of this is: Don’t share connections between threads. It’s really not worth your effort or mine, and in the end, will probably hurt performance, since the MySQL server runs a separate thread for each connection. You can certainly do things like cache connections in a pool, and give those connections to one thread at a time. **If you let two threads use a connection simultaneously, the MySQL client library will probably upchuck and die. You have been warned**. I would remove the thread safety claims as well. -- Ticket URL: <https://code.djangoproject.com/ticket/35702#comment:1> Django <https://code.djangoproject.com/> The Web framework for perfectionists with deadlines. -- You received this message because you are subscribed to the Google Groups "Django updates" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-updates+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/django-updates/010701917b2ccc96-140e7832-479a-47af-b0db-473dd52dd446-000000%40eu-central-1.amazonses.com.