__init__.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import warnings
  2. from django.core import signals
  3. from django.db.utils import (DEFAULT_DB_ALIAS, DataError, OperationalError,
  4. IntegrityError, InternalError, ProgrammingError, NotSupportedError,
  5. DatabaseError, InterfaceError, Error, load_backend,
  6. ConnectionHandler, ConnectionRouter)
  7. from django.utils.deprecation import RemovedInDjango18Warning
  8. from django.utils.functional import cached_property
  9. __all__ = [
  10. 'backend', 'connection', 'connections', 'router', 'DatabaseError',
  11. 'IntegrityError', 'InternalError', 'ProgrammingError', 'DataError',
  12. 'NotSupportedError', 'Error', 'InterfaceError', 'OperationalError',
  13. 'DEFAULT_DB_ALIAS'
  14. ]
  15. connections = ConnectionHandler()
  16. router = ConnectionRouter()
  17. # `connection`, `DatabaseError` and `IntegrityError` are convenient aliases
  18. # for backend bits.
  19. # DatabaseWrapper.__init__() takes a dictionary, not a settings module, so
  20. # we manually create the dictionary from the settings, passing only the
  21. # settings that the database backends care about. Note that TIME_ZONE is used
  22. # by the PostgreSQL backends.
  23. # We load all these up for backwards compatibility, you should use
  24. # connections['default'] instead.
  25. class DefaultConnectionProxy(object):
  26. """
  27. Proxy for accessing the default DatabaseWrapper object's attributes. If you
  28. need to access the DatabaseWrapper object itself, use
  29. connections[DEFAULT_DB_ALIAS] instead.
  30. """
  31. def __getattr__(self, item):
  32. return getattr(connections[DEFAULT_DB_ALIAS], item)
  33. def __setattr__(self, name, value):
  34. return setattr(connections[DEFAULT_DB_ALIAS], name, value)
  35. def __delattr__(self, name):
  36. return delattr(connections[DEFAULT_DB_ALIAS], name)
  37. def __eq__(self, other):
  38. return connections[DEFAULT_DB_ALIAS] == other
  39. def __ne__(self, other):
  40. return connections[DEFAULT_DB_ALIAS] != other
  41. connection = DefaultConnectionProxy()
  42. class DefaultBackendProxy(object):
  43. """
  44. Temporary proxy class used during deprecation period of the `backend` module
  45. variable.
  46. """
  47. @cached_property
  48. def _backend(self):
  49. warnings.warn("Accessing django.db.backend is deprecated.",
  50. RemovedInDjango18Warning, stacklevel=2)
  51. return load_backend(connections[DEFAULT_DB_ALIAS].settings_dict['ENGINE'])
  52. def __getattr__(self, item):
  53. return getattr(self._backend, item)
  54. def __setattr__(self, name, value):
  55. return setattr(self._backend, name, value)
  56. def __delattr__(self, name):
  57. return delattr(self._backend, name)
  58. backend = DefaultBackendProxy()
  59. def close_connection(**kwargs):
  60. warnings.warn(
  61. "close_connection is superseded by close_old_connections.",
  62. RemovedInDjango18Warning, stacklevel=2)
  63. # Avoid circular imports
  64. from django.db import transaction
  65. for conn in connections:
  66. # If an error happens here the connection will be left in broken
  67. # state. Once a good db connection is again available, the
  68. # connection state will be cleaned up.
  69. transaction.abort(conn)
  70. connections[conn].close()
  71. # Register an event to reset saved queries when a Django request is started.
  72. def reset_queries(**kwargs):
  73. for conn in connections.all():
  74. conn.queries = []
  75. signals.request_started.connect(reset_queries)
  76. # Register an event to reset transaction state and close connections past
  77. # their lifetime. NB: abort() doesn't do anything outside of a transaction.
  78. def close_old_connections(**kwargs):
  79. for conn in connections.all():
  80. # Remove this when the legacy transaction management goes away.
  81. try:
  82. conn.abort()
  83. except DatabaseError:
  84. pass
  85. conn.close_if_unusable_or_obsolete()
  86. signals.request_started.connect(close_old_connections)
  87. signals.request_finished.connect(close_old_connections)