2006-05-02 09:31:56 +08:00
|
|
|
from django.conf import settings
|
|
|
|
from django.core import signals
|
2007-09-16 03:25:20 +08:00
|
|
|
from django.core.exceptions import ImproperlyConfigured
|
2011-07-13 17:35:51 +08:00
|
|
|
from django.db.utils import (ConnectionHandler, ConnectionRouter,
|
|
|
|
load_backend, DEFAULT_DB_ALIAS, DatabaseError, IntegrityError)
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2010-01-22 22:30:06 +08:00
|
|
|
__all__ = ('backend', 'connection', 'connections', 'router', 'DatabaseError',
|
2009-12-22 23:18:51 +08:00
|
|
|
'IntegrityError', 'DEFAULT_DB_ALIAS')
|
2006-05-02 09:31:56 +08:00
|
|
|
|
|
|
|
|
2012-10-29 06:02:41 +08:00
|
|
|
if settings.DATABASES and DEFAULT_DB_ALIAS not in settings.DATABASES:
|
2010-10-07 01:30:37 +08:00
|
|
|
raise ImproperlyConfigured("You must define a '%s' database" % DEFAULT_DB_ALIAS)
|
2009-12-22 23:18:51 +08:00
|
|
|
|
|
|
|
connections = ConnectionHandler(settings.DATABASES)
|
2009-03-13 12:41:45 +08:00
|
|
|
|
2010-01-22 22:30:06 +08:00
|
|
|
router = ConnectionRouter(settings.DATABASE_ROUTERS)
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2009-03-11 11:39:34 +08:00
|
|
|
# `connection`, `DatabaseError` and `IntegrityError` are convenient aliases
|
|
|
|
# for backend bits.
|
|
|
|
|
|
|
|
# DatabaseWrapper.__init__() takes a dictionary, not a settings module, so
|
|
|
|
# we manually create the dictionary from the settings, passing only the
|
|
|
|
# settings that the database backends care about. Note that TIME_ZONE is used
|
|
|
|
# by the PostgreSQL backends.
|
2011-12-16 21:40:19 +08:00
|
|
|
# We load all these up for backwards compatibility, you should use
|
2009-12-22 23:18:51 +08:00
|
|
|
# connections['default'] instead.
|
2011-12-16 21:40:19 +08:00
|
|
|
class DefaultConnectionProxy(object):
|
|
|
|
"""
|
|
|
|
Proxy for accessing the default DatabaseWrapper object's attributes. If you
|
|
|
|
need to access the DatabaseWrapper object itself, use
|
|
|
|
connections[DEFAULT_DB_ALIAS] instead.
|
|
|
|
"""
|
|
|
|
def __getattr__(self, item):
|
|
|
|
return getattr(connections[DEFAULT_DB_ALIAS], item)
|
|
|
|
|
|
|
|
def __setattr__(self, name, value):
|
|
|
|
return setattr(connections[DEFAULT_DB_ALIAS], name, value)
|
|
|
|
|
|
|
|
connection = DefaultConnectionProxy()
|
2009-12-22 23:18:51 +08:00
|
|
|
backend = load_backend(connection.settings_dict['ENGINE'])
|
2006-05-02 09:31:56 +08:00
|
|
|
|
|
|
|
# Register an event that closes the database connection
|
|
|
|
# when a Django request is finished.
|
2008-08-06 23:32:46 +08:00
|
|
|
def close_connection(**kwargs):
|
2009-12-28 13:11:57 +08:00
|
|
|
for conn in connections.all():
|
|
|
|
conn.close()
|
2008-08-06 23:32:46 +08:00
|
|
|
signals.request_finished.connect(close_connection)
|
2006-05-02 09:31:56 +08:00
|
|
|
|
|
|
|
# Register an event that resets connection.queries
|
|
|
|
# when a Django request is started.
|
2008-08-06 23:32:46 +08:00
|
|
|
def reset_queries(**kwargs):
|
2009-12-28 13:11:57 +08:00
|
|
|
for conn in connections.all():
|
|
|
|
conn.queries = []
|
2008-08-06 23:32:46 +08:00
|
|
|
signals.request_started.connect(reset_queries)
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2009-12-22 23:18:51 +08:00
|
|
|
# Register an event that rolls back the connections
|
2006-05-02 09:31:56 +08:00
|
|
|
# when a Django request has an exception.
|
2008-08-06 23:32:46 +08:00
|
|
|
def _rollback_on_exception(**kwargs):
|
2006-05-02 09:31:56 +08:00
|
|
|
from django.db import transaction
|
2009-12-22 23:18:51 +08:00
|
|
|
for conn in connections:
|
|
|
|
try:
|
|
|
|
transaction.rollback_unless_managed(using=conn)
|
|
|
|
except DatabaseError:
|
|
|
|
pass
|
2008-08-06 23:32:46 +08:00
|
|
|
signals.got_request_exception.connect(_rollback_on_exception)
|