Skip to content

Commit

Permalink
Fixed #1442 -- Fixed multithreading problem with various database bac…
Browse files Browse the repository at this point in the history
…kends. Thanks, Eugene Lazutkin

git-svn-id: http://code.djangoproject.com/svn/django/trunk@2579 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information
adrianholovaty committed Mar 28, 2006
1 parent 3ff5b99 commit 67cbb5c
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 5 deletions.
9 changes: 8 additions & 1 deletion django/core/db/backends/ado_mssql.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -44,7 +44,14 @@ def variantToPython(variant, adType):
return res return res
Database.convertVariantToPython = variantToPython Database.convertVariantToPython = variantToPython


class DatabaseWrapper: try:
# Only exists in python 2.4+
from threading import local
except ImportError:
# Import copy of _thread_local.py from python 2.4
from django.utils._threading_local import local

class DatabaseWrapper(local):
def __init__(self): def __init__(self):
self.connection = None self.connection = None
self.queries = [] self.queries = []
Expand Down
21 changes: 19 additions & 2 deletions django/core/db/backends/mysql.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -47,14 +47,31 @@ def __getattr__(self, attr):
else: else:
return getattr(self.cursor, attr) return getattr(self.cursor, attr)


class DatabaseWrapper: try:
# Only exists in python 2.4+
from threading import local
except ImportError:
# Import copy of _thread_local.py from python 2.4
from django.utils._threading_local import local

class DatabaseWrapper(local):
def __init__(self): def __init__(self):
self.connection = None self.connection = None
self.queries = [] self.queries = []


def _valid_connection(self):
if self.connection is not None:
try:
self.connection.ping()
return True
except DatabaseError:
self.connection.close()
self.connection = None
return False

def cursor(self): def cursor(self):
from django.conf.settings import DATABASE_USER, DATABASE_NAME, DATABASE_HOST, DATABASE_PORT, DATABASE_PASSWORD, DEBUG from django.conf.settings import DATABASE_USER, DATABASE_NAME, DATABASE_HOST, DATABASE_PORT, DATABASE_PASSWORD, DEBUG
if self.connection is None: if not self._valid_connection():
kwargs = { kwargs = {
'user': DATABASE_USER, 'user': DATABASE_USER,
'db': DATABASE_NAME, 'db': DATABASE_NAME,
Expand Down
9 changes: 8 additions & 1 deletion django/core/db/backends/postgresql.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -9,7 +9,14 @@


DatabaseError = Database.DatabaseError DatabaseError = Database.DatabaseError


class DatabaseWrapper: try:
# Only exists in python 2.4+
from threading import local
except ImportError:
# Import copy of _thread_local.py from python 2.4
from django.utils._threading_local import local

class DatabaseWrapper(local):
def __init__(self): def __init__(self):
self.connection = None self.connection = None
self.queries = [] self.queries = []
Expand Down
9 changes: 8 additions & 1 deletion django/core/db/backends/sqlite3.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -24,7 +24,14 @@ def utf8(s):
return s return s
return [utf8(r) for r in row] return [utf8(r) for r in row]


class DatabaseWrapper: try:
# Only exists in python 2.4+
from threading import local
except ImportError:
# Import copy of _thread_local.py from python 2.4
from django.utils._threading_local import local

class DatabaseWrapper(local):
def __init__(self): def __init__(self):
self.connection = None self.connection = None
self.queries = [] self.queries = []
Expand Down

0 comments on commit 67cbb5c

Please sign in to comment.