From 67cbb5c248706bc101a6f3b9d65643423b5df1f8 Mon Sep 17 00:00:00 2001 From: Adrian Holovaty Date: Tue, 28 Mar 2006 17:39:53 +0000 Subject: [PATCH] Fixed #1442 -- Fixed multithreading problem with various database backends. Thanks, Eugene Lazutkin git-svn-id: http://code.djangoproject.com/svn/django/trunk@2579 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/core/db/backends/ado_mssql.py | 9 ++++++++- django/core/db/backends/mysql.py | 21 +++++++++++++++++++-- django/core/db/backends/postgresql.py | 9 ++++++++- django/core/db/backends/sqlite3.py | 9 ++++++++- 4 files changed, 43 insertions(+), 5 deletions(-) diff --git a/django/core/db/backends/ado_mssql.py b/django/core/db/backends/ado_mssql.py index f13cc8b9e8..4afe0cef70 100644 --- a/django/core/db/backends/ado_mssql.py +++ b/django/core/db/backends/ado_mssql.py @@ -44,7 +44,14 @@ def variantToPython(variant, adType): return res 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): self.connection = None self.queries = [] diff --git a/django/core/db/backends/mysql.py b/django/core/db/backends/mysql.py index 1a8ce10e81..51b8e85d30 100644 --- a/django/core/db/backends/mysql.py +++ b/django/core/db/backends/mysql.py @@ -47,14 +47,31 @@ class MysqlDebugWrapper: else: 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): self.connection = None 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): 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 = { 'user': DATABASE_USER, 'db': DATABASE_NAME, diff --git a/django/core/db/backends/postgresql.py b/django/core/db/backends/postgresql.py index dbc0cd0677..0bc799c247 100644 --- a/django/core/db/backends/postgresql.py +++ b/django/core/db/backends/postgresql.py @@ -9,7 +9,14 @@ import psycopg as Database 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): self.connection = None self.queries = [] diff --git a/django/core/db/backends/sqlite3.py b/django/core/db/backends/sqlite3.py index dc477da13d..1b2ae8cd84 100644 --- a/django/core/db/backends/sqlite3.py +++ b/django/core/db/backends/sqlite3.py @@ -24,7 +24,14 @@ def utf8rowFactory(cursor, row): return s 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): self.connection = None self.queries = []