2013-03-02 06:46:09 +08:00
|
|
|
import warnings
|
|
|
|
|
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
|
Refactored database exceptions wrapping.
Squashed commit of the following:
commit 2181d833ed1a2e422494738dcef311164c4e097e
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Wed Feb 27 14:28:39 2013 +0100
Fixed #15901 -- Wrapped all PEP-249 exceptions.
commit 5476a5d93c19aa2f928c497d39ce6e33f52694e2
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Tue Feb 26 17:26:52 2013 +0100
Added PEP 3134 exception chaining.
Thanks Jacob Kaplan-Moss for the suggestion.
commit 9365fad0a650328002fb424457d675a273c95802
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Tue Feb 26 17:13:49 2013 +0100
Improved API for wrapping database errors.
Thanks Alex Gaynor for the proposal.
commit 1b463b765f2826f73a8d9266795cd5da4f8d5e9e
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Tue Feb 26 15:00:39 2013 +0100
Removed redundant exception wrapping.
This is now taken care of by the cursor wrapper.
commit 524bc7345a724bf526bdd2dd1bcf5ede67d6bb5c
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Tue Feb 26 14:55:10 2013 +0100
Wrapped database exceptions in the base backend.
This covers the most common PEP-249 APIs:
- Connection APIs: close(), commit(), rollback(), cursor()
- Cursor APIs: callproc(), close(), execute(), executemany(),
fetchone(), fetchmany(), fetchall(), nextset().
Fixed #19920.
commit a66746bb5f0839f35543222787fce3b6a0d0a3ea
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Tue Feb 26 14:53:34 2013 +0100
Added a wrap_database_exception context manager and decorator.
It re-throws backend-specific exceptions using Django's common wrappers.
2013-02-26 21:53:34 +08:00
|
|
|
from django.db.utils import (DEFAULT_DB_ALIAS,
|
|
|
|
DataError, OperationalError, IntegrityError, InternalError,
|
|
|
|
ProgrammingError, NotSupportedError, DatabaseError,
|
|
|
|
InterfaceError, Error,
|
|
|
|
load_backend, ConnectionHandler, ConnectionRouter)
|
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)
|
|
|
|
|
2013-03-01 00:05:25 +08:00
|
|
|
def __delattr__(self, name):
|
|
|
|
return delattr(connections[DEFAULT_DB_ALIAS], name)
|
|
|
|
|
2011-12-16 21:40:19 +08:00
|
|
|
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
|
|
|
|
2008-08-06 23:32:46 +08:00
|
|
|
def close_connection(**kwargs):
|
2013-02-18 18:37:26 +08:00
|
|
|
warnings.warn(
|
|
|
|
"close_connection is superseded by close_old_connections.",
|
|
|
|
PendingDeprecationWarning, stacklevel=2)
|
2013-02-06 05:52:29 +08:00
|
|
|
# Avoid circular imports
|
|
|
|
from django.db import transaction
|
|
|
|
for conn in connections:
|
2013-02-13 05:11:22 +08:00
|
|
|
# If an error happens here the connection will be left in broken
|
|
|
|
# state. Once a good db connection is again available, the
|
|
|
|
# connection state will be cleaned up.
|
|
|
|
transaction.abort(conn)
|
|
|
|
connections[conn].close()
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2013-02-18 18:37:26 +08:00
|
|
|
# Register an event to reset saved 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
|
|
|
|
2013-02-18 18:37:26 +08:00
|
|
|
# Register an event to reset transaction state and close connections past
|
|
|
|
# their lifetime. NB: abort() doesn't do anything outside of a transaction.
|
|
|
|
def close_old_connections(**kwargs):
|
|
|
|
for conn in connections.all():
|
|
|
|
try:
|
|
|
|
conn.abort()
|
|
|
|
except DatabaseError:
|
|
|
|
pass
|
|
|
|
conn.close_if_unusable_or_obsolete()
|
|
|
|
signals.request_started.connect(close_old_connections)
|
|
|
|
signals.request_finished.connect(close_old_connections)
|
|
|
|
|
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)
|