diff options
| author | Aymeric Augustin <aymeric.augustin@m4x.org> | 2013-03-03 15:43:24 +0100 |
|---|---|---|
| committer | Aymeric Augustin <aymeric.augustin@m4x.org> | 2013-03-11 14:48:54 +0100 |
| commit | 1617557ae30001cef8a863687d4bcdc28151cd50 (patch) | |
| tree | 1f6f6e7820a20adb92c496c02e830f4be148432f | |
| parent | 7b4815b455fc99a2661492f91f7242cfb09a7017 (diff) | |
Added BaseDatabaseWrapper.ensure_connection.
This API is useful because autocommit cannot be managed without an open
connection.
| -rw-r--r-- | django/db/backends/__init__.py | 34 | ||||
| -rw-r--r-- | django/db/backends/postgresql_psycopg2/base.py | 4 |
2 files changed, 24 insertions, 14 deletions
diff --git a/django/db/backends/__init__.py b/django/db/backends/__init__.py index d372384fc5..b7f4628e9a 100644 --- a/django/db/backends/__init__.py +++ b/django/db/backends/__init__.py @@ -86,20 +86,33 @@ class BaseDatabaseWrapper(object): """Creates a cursor. Assumes that a connection is established.""" raise NotImplementedError + ##### Backend-specific methods for creating connections ##### + + def connect(self): + """Connects to the database. Assumes that the connection is closed.""" + # Reset parameters defining when to close the connection + max_age = self.settings_dict['CONN_MAX_AGE'] + self.close_at = None if max_age is None else time.time() + max_age + self.errors_occurred = False + # Establish the connection + conn_params = self.get_connection_params() + self.connection = self.get_new_connection(conn_params) + self.init_connection_state() + connection_created.send(sender=self.__class__, connection=self) + + def ensure_connection(self): + """ + Guarantees that a connection to the database is established. + """ + if self.connection is None: + with self.wrap_database_errors(): + self.connect() + ##### Backend-specific wrappers for PEP-249 connection methods ##### def _cursor(self): + self.ensure_connection() with self.wrap_database_errors(): - if self.connection is None: - # Reset parameters defining when to close the connection - max_age = self.settings_dict['CONN_MAX_AGE'] - self.close_at = None if max_age is None else time.time() + max_age - self.errors_occurred = False - # Establish the connection - conn_params = self.get_connection_params() - self.connection = self.get_new_connection(conn_params) - self.init_connection_state() - connection_created.send(sender=self.__class__, connection=self) return self.create_cursor() def _commit(self): @@ -285,6 +298,7 @@ class BaseDatabaseWrapper(object): """ Enable or disable autocommit. """ + self.ensure_connection() self._set_autocommit(autocommit) self.autocommit = autocommit diff --git a/django/db/backends/postgresql_psycopg2/base.py b/django/db/backends/postgresql_psycopg2/base.py index cc1d1ae567..384b38cc58 100644 --- a/django/db/backends/postgresql_psycopg2/base.py +++ b/django/db/backends/postgresql_psycopg2/base.py @@ -169,8 +169,6 @@ class DatabaseWrapper(BaseDatabaseWrapper): Switch the isolation level when needing transaction support, so that the same transaction is visible across all the queries. """ - if self.connection is None: # Force creating a connection. - self.cursor().close() if managed and self.autocommit: self.set_autocommit(False) @@ -179,8 +177,6 @@ class DatabaseWrapper(BaseDatabaseWrapper): If the normal operating mode is "autocommit", switch back to that when leaving transaction management. """ - if self.connection is None: # Force creating a connection. - self.cursor().close() if not managed and not self.autocommit: self.rollback() # Must terminate transaction first. self.set_autocommit(True) |
