As far as i know and hell, experienced it isn’t possible to share django db connection for threads.
I have searched around and found two solutions for this problem:
- Using direct
SQLconnection by using relevant DBMS driver directly. - Use
sqlalchmenyand its session poll thing.
Isn’t there really any other way? isn’t it possible to ask django to create new connection for thread (sounds something simple and useful to me!)
Thanks.
The thread-safety issue with the DB has nothing to do with the connection; it’s when two threads try to alter the same piece of data simulataneously, or even one trying to write while the other tries to read. In order to prevent this, you simply need to lock the table when one of your threads needs to do something with it. A quick and easy way with Django 1.4+ is to use
select_for_update, which will lock the table until you do anupdateoperation.