Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Added BaseDatabaseWrapper.ensure_connection.

This API is useful because autocommit cannot be managed without an open
connection.
  • Loading branch information...
commit 1617557ae30001cef8a863687d4bcdc28151cd50 1 parent 7b4815b
Aymeric Augustin authored March 03, 2013
34  django/db/backends/__init__.py
@@ -86,20 +86,33 @@ def create_cursor(self):
86 86
         """Creates a cursor. Assumes that a connection is established."""
87 87
         raise NotImplementedError
88 88
 
  89
+    ##### Backend-specific methods for creating connections #####
  90
+
  91
+    def connect(self):
  92
+        """Connects to the database. Assumes that the connection is closed."""
  93
+        # Reset parameters defining when to close the connection
  94
+        max_age = self.settings_dict['CONN_MAX_AGE']
  95
+        self.close_at = None if max_age is None else time.time() + max_age
  96
+        self.errors_occurred = False
  97
+        # Establish the connection
  98
+        conn_params = self.get_connection_params()
  99
+        self.connection = self.get_new_connection(conn_params)
  100
+        self.init_connection_state()
  101
+        connection_created.send(sender=self.__class__, connection=self)
  102
+
  103
+    def ensure_connection(self):
  104
+        """
  105
+        Guarantees that a connection to the database is established.
  106
+        """
  107
+        if self.connection is None:
  108
+            with self.wrap_database_errors():
  109
+                self.connect()
  110
+
89 111
     ##### Backend-specific wrappers for PEP-249 connection methods #####
90 112
 
91 113
     def _cursor(self):
  114
+        self.ensure_connection()
92 115
         with self.wrap_database_errors():
93  
-            if self.connection is None:
94  
-                # Reset parameters defining when to close the connection
95  
-                max_age = self.settings_dict['CONN_MAX_AGE']
96  
-                self.close_at = None if max_age is None else time.time() + max_age
97  
-                self.errors_occurred = False
98  
-                # Establish the connection
99  
-                conn_params = self.get_connection_params()
100  
-                self.connection = self.get_new_connection(conn_params)
101  
-                self.init_connection_state()
102  
-                connection_created.send(sender=self.__class__, connection=self)
103 116
             return self.create_cursor()
104 117
 
105 118
     def _commit(self):
@@ -285,6 +298,7 @@ def set_autocommit(self, autocommit=True):
285 298
         """
286 299
         Enable or disable autocommit.
287 300
         """
  301
+        self.ensure_connection()
288 302
         self._set_autocommit(autocommit)
289 303
         self.autocommit = autocommit
290 304
 
4  django/db/backends/postgresql_psycopg2/base.py
@@ -169,8 +169,6 @@ def _enter_transaction_management(self, managed):
169 169
         Switch the isolation level when needing transaction support, so that
170 170
         the same transaction is visible across all the queries.
171 171
         """
172  
-        if self.connection is None:             # Force creating a connection.
173  
-            self.cursor().close()
174 172
         if managed and self.autocommit:
175 173
             self.set_autocommit(False)
176 174
 
@@ -179,8 +177,6 @@ def _leave_transaction_management(self, managed):
179 177
         If the normal operating mode is "autocommit", switch back to that when
180 178
         leaving transaction management.
181 179
         """
182  
-        if self.connection is None:             # Force creating a connection.
183  
-            self.cursor().close()
184 180
         if not managed and not self.autocommit:
185 181
             self.rollback()                     # Must terminate transaction first.
186 182
             self.set_autocommit(True)

0 notes on commit 1617557

Please sign in to comment.
Something went wrong with that request. Please try again.