Django Channels persistent DB connection

186 views
Skip to first unread message

BR

unread,
Oct 30, 2019, 12:17:54 PM10/30/19
to Django users
I moved from a local sqlite3 DB file to a shared remote (local LAN) MariaDB server and noticed a bit of a slow down. On top of the DB now being remote, I'm also using SSL to encrypt the DB information. Based on the description of database_sync_to_async it sounds like the connection is closed/cleaned up on each call.

Is there an option for a persistent DB connection in Channels?

BR

unread,
Oct 30, 2019, 12:21:12 PM10/30/19
to Django users
I should add: My interest would be to have a DB connection that lasts the lifetime of the parent consumer. Does it already do this, and am I imagining things?

Adam Hooper

unread,
Nov 15, 2019, 1:53:49 PM11/15/19
to Django users
On Wednesday, October 30, 2019 at 12:17:54 PM UTC-4, BR wrote:
I moved from a local sqlite3 DB file to a shared remote (local LAN) MariaDB server and noticed a bit of a slow down. On top of the DB now being remote, I'm also using SSL to encrypt the DB information. Based on the description of database_sync_to_async it sounds like the connection is closed/cleaned up on each call.

Is there an option for a persistent DB connection in Channels?

Use Django's CONN_MAX_AGE.

Enjoy life,
Adam 

o1bigtenor

unread,
Nov 15, 2019, 2:02:51 PM11/15/19
to django...@googlegroups.com
Read the docs and am left with a question.
If I want a connection to the database but want it for only the time
the customer (query source) is logged in - - - how do I specify that?
(So I don't want the connection to be timed - - - would rather the
connection was related to the online access and then closed when the
client (customer) logged off.)

TIA

Adam Hooper

unread,
Nov 15, 2019, 2:25:19 PM11/15/19
to Django users
On Friday, November 15, 2019 at 2:02:51 PM UTC-5, ajoeiam wrote:
If I want a connection to the database but want it for only the time
the customer (query source) is logged in - - - how do I specify that?
 (So I don't want the connection to be timed - - - would rather the
connection was related to the online access and then closed when the
client (customer) logged off.)

I don't think there's a way to do that.

If you're trying to use one DB connection per short-lived request, the way ordinary Django request handlers do when CONN_MAX_AGE is disabled, then simply bundle all your database queries into one synchronous function and decorate it with database_sync_to_async().

If you actually want to maintain one database connection per websocket, then don't use Channels. The whole point of Channels is to not consume a thread per websocket. A database connection is a thread (on the database server).

You won't find much help online if you're trying to tie up a database connection per Websocket. I found an exception: an academic project from some University of Alberta students. https://github.com/Kai-Bailey/WebSock. It's not Django.

Enjoy life,
Adam
Reply all
Reply to author
Forward
0 new messages