Login

Multi-DB Reconnecting Persistent Postgres Connection

Author:
cronosa
Posted:
June 28, 2013
Language:
Python
Version:
1.5
Tags:
web-socket persistent multi-db connection socket database multiple-databases twisted
Score:
0 (after 0 ratings)

This is a modification of http://djangosnippets.org/snippets/1707/ that handles the database going down or PG Bouncer killing the connection. This also works in things like Twisted to make sure the connection is alive before doing a real query. Thanks @mike_tk for the original post!

EDIT: Updated the wrapper to handle multi-db. Before it was using the first connection it made, now it creates an attribute name for the connection based on the name of the database.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# Custom DB backend postgresql_psycopg2 based
# implements persistent database connection using thread local storage
from threading import local

from django.db.backends.postgresql_psycopg2.base import DatabaseError, \
    DatabaseWrapper as BaseDatabaseWrapper, IntegrityError
from psycopg2 import OperationalError

threadlocal = local()


class DatabaseWrapper(BaseDatabaseWrapper):
    def is_alive(self, connection):
        try:  # Check if connection is alive
            connection.cursor().execute('SELECT 1')
        except (DatabaseError, OperationalError):
            return False
        else:
            return True

    @property
    def local_key(self):
        return '{}_{}'.format(self.settings_dict.get('NAME', 'db'), 'connection')

    @property
    def local_connection(self):
        return getattr(threadlocal, self.local_key, None)

    @local_connection.setter
    def local_connection(self, connection):
        setattr(threadlocal, self.local_key, connection)

    def _cursor(self, *args, **kwargs):
        if self.local_connection is not None and self.connection is None:
            if self.is_alive(self.local_connection):
                self.connection = self.local_connection
            else:
                self.local_connection = None
        elif self.connection is not None:
            if self.is_alive(self.connection):
                self.local_connection = self.connection
            else:
                self.connection.close()
                self.connection = None
                self.local_connection = None
        cursor = super(DatabaseWrapper, self)._cursor(*args, **kwargs)
        if self.local_connection is None and self.connection is not None:
            self.local_connection = self.connection
        return cursor

    def close(self):
        if self.connection is not None:
            self.connection.commit()
            self.connection = None

More like this

  1. Save a model using an arbitrary db connection by weswinham 5 years, 11 months ago
  2. Make runfcgi fail when database connection is open before fork by mpasternacki 4 years, 4 months ago
  3. Manager for multiple database connections by rix 5 years, 12 months ago
  4. Persistent connection to PostgreSQL database by mike_tk 5 years, 11 months ago
  5. OracleAuthBackend by nosrednakram 5 years, 11 months ago

Comments

Please login first before commenting.