Code

Opened 8 years ago

Closed 8 years ago

Last modified 7 years ago

#1237 closed enhancement (duplicate)

[patch]thread safety connection pool for all backend

Reported by: junzhang.jn@… Owned by: adrian
Component: Database layer (models, ORM) Version:
Severity: normal Keywords: connection pool db
Cc: Triage Stage: Unreviewed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

Django uses the same connection for all threads. It breaks any database when using transaction or in multithread web server.
this patch is implemented to avoid it.

like ticket [463] but for all.

  • use AutoReleaseCursor return connection object to pool when cursor deleted.
  • use connections dict protect threaded active connection.
  • db backend must implement a get_check_sql function to provide a sql which can check a connection's health.

ado_mssql:

def get_check_sql():
    return "select top 1 1 from sysfiles"

oracle:

def get_check_sql():
    return "select 1 from dual";

other....

sorry for my very very poor english.

Attachments (7)

__init__.py.diff (3.4 KB) - added by junzhang.jn@… 8 years ago.
pool.py (5.6 KB) - added by junzhang.jn@… 8 years ago.
__init__.py.2.diff (2.7 KB) - added by junzhang.jn@… 8 years ago.
delete AutoReleaseCursor , it breaks transaction in multithread when 'threadsafety > 1'.
modpython.py.diff (1.0 KB) - added by junzhang.jn@… 8 years ago.
wsgi.py.diff (906 bytes) - added by junzhang.jn@… 8 years ago.
django.db.__init__.py.diff (2.9 KB) - added by junzhang.jn@… 8 years ago.
pool.py.2 (6.0 KB) - added by junzhang.jn@… 8 years ago.

Download all attachments as: .zip

Change History (10)

Changed 8 years ago by junzhang.jn@…

Changed 8 years ago by junzhang.jn@…

Changed 8 years ago by junzhang.jn@…

delete AutoReleaseCursor , it breaks transaction in multithread when 'threadsafety > 1'.

Changed 8 years ago by junzhang.jn@…

Changed 8 years ago by junzhang.jn@…

comment:1 Changed 8 years ago by junzhang.jn@…

update the connection pool in magic-removal branch. only the next two diffs needed.

pool.py.2:
  • fix check_thread close not thread safety connetion. now thread safety < 2 connection will not run check thread. so you must use zhe connection in the web server which has thread pool.
  • fix cannot stop the check_thread in manage.py shell. use atexit.register function for this problem.
django.db.__init__.py.diff:
  • add new setting USE_DBPOOL.

and now ,you must implement get_check_sql function in django.db.bachend.xxxx.base.

sqlite3:

def get_check_sql():
    return "SELECT name FROM sqlite_master WHERE type='table' limit 1"

ado_mssql:

def get_check_sql():
    return "select top 1 1 from sysfiles"

postgresql:

def get_check_sql():
    return "SELECT c.relname FROM pg_catalog.pg_class limit 1"

Changed 8 years ago by junzhang.jn@…

Changed 8 years ago by junzhang.jn@…

comment:2 Changed 8 years ago by eugene@…

  • Resolution set to duplicate
  • Status changed from new to closed

Superseded by #1442

comment:3 Changed 7 years ago by anonymous

  • milestone Version 1.0 deleted

Milestone Version 1.0 deleted

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.