Ticket #11156: 11156.diff
File 11156.diff, 7.0 KB (added by , 15 years ago) |
---|
-
django/db/models/query.py
364 364 params = dict([(k, v) for k, v in kwargs.items() if '__' not in k]) 365 365 params.update(defaults) 366 366 obj = self.model(**params) 367 sid = transaction.savepoint(using=self.db )367 sid = transaction.savepoint(using=self.db, only_if_needed=True) 368 368 obj.save(force_insert=True, using=self.db) 369 transaction.savepoint_commit(sid, using=self.db) 369 transaction.savepoint_commit(sid, using=self.db, 370 only_if_needed=True) 370 371 return obj, True 371 372 except IntegrityError, e: 372 transaction.savepoint_rollback(sid, using=self.db) 373 transaction.savepoint_rollback(sid, using=self.db, 374 only_if_needed=True) 373 375 try: 374 376 return self.get(**kwargs), False 375 377 except self.model.DoesNotExist: -
django/db/backends/postgresql/base.py
67 67 68 68 class DatabaseFeatures(BaseDatabaseFeatures): 69 69 uses_savepoints = True 70 needs_savepoint_on_exception = True 70 71 71 72 class DatabaseWrapper(BaseDatabaseWrapper): 72 73 operators = { -
django/db/backends/__init__.py
50 50 """ 51 51 pass 52 52 53 def _savepoint(self, sid ):53 def _savepoint(self, sid, only_if_needed=False): 54 54 if not self.features.uses_savepoints: 55 55 return 56 if only_if_needed and not self.features.needs_savepoint_on_exception: 57 return 56 58 self.cursor().execute(self.ops.savepoint_create_sql(sid)) 57 59 58 def _savepoint_rollback(self, sid ):60 def _savepoint_rollback(self, sid, only_if_needed=False): 59 61 if not self.features.uses_savepoints: 60 62 return 63 if only_if_needed and not self.features.needs_savepoint_on_exception: 64 return 61 65 self.cursor().execute(self.ops.savepoint_rollback_sql(sid)) 62 66 63 def _savepoint_commit(self, sid ):67 def _savepoint_commit(self, sid, only_if_needed=False): 64 68 if not self.features.uses_savepoints: 65 69 return 70 if only_if_needed and not self.features.needs_savepoint_on_exception: 71 return 66 72 self.cursor().execute(self.ops.savepoint_commit_sql(sid)) 67 73 68 74 def close(self): … … 92 98 can_return_id_from_insert = False 93 99 uses_autocommit = False 94 100 uses_savepoints = False 101 needs_savepoint_on_exception = False 95 102 # If True, don't use integer foreign keys referring to, e.g., positive 96 103 # integer primary keys. 97 104 related_fields_match_type = False -
django/db/backends/postgresql_psycopg2/base.py
30 30 class DatabaseFeatures(BaseDatabaseFeatures): 31 31 needs_datetime_string_cast = False 32 32 can_return_id_from_insert = False 33 uses_savepoints = True 33 34 34 35 class DatabaseOperations(PostgresqlDatabaseOperations): 35 36 def last_executed_query(self, cursor, sql, params): … … 148 149 self.connection.set_isolation_level(level) 149 150 finally: 150 151 self.isolation_level = level 151 self.features. uses_savepoints= bool(level)152 self.features.needs_savepoint_on_exception = bool(level) -
django/db/transaction.py
209 209 connection._rollback() 210 210 set_clean(using=using) 211 211 212 def savepoint(using=None ):212 def savepoint(using=None, only_if_needed=False): 213 213 """ 214 214 Creates a savepoint (if supported and required by the backend) inside the 215 215 current transaction. Returns an identifier for the savepoint that will be … … 226 226 savepoint_state[thread_ident][using] = [None] 227 227 tid = str(thread_ident).replace('-', '') 228 228 sid = "s%s_x%d" % (tid, len(savepoint_state[thread_ident][using])) 229 connection._savepoint(sid )229 connection._savepoint(sid, only_if_needed) 230 230 return sid 231 231 232 def savepoint_rollback(sid, using=None ):232 def savepoint_rollback(sid, using=None, only_if_needed=False): 233 233 """ 234 234 Rolls back the most recent savepoint (if one exists). Does nothing if 235 savepoints are not supported. 235 savepoints are not supported, or if only_if_needed is set and the backend 236 does not need to use savepoints to recover from exceptions. 236 237 """ 237 238 if using is None: 238 239 using = DEFAULT_DB_ALIAS 239 240 connection = connections[using] 240 241 thread_ident = thread.get_ident() 241 242 if thread_ident in savepoint_state and using in savepoint_state[thread_ident]: 242 connection._savepoint_rollback(sid )243 connection._savepoint_rollback(sid, only_if_needed) 243 244 244 def savepoint_commit(sid, using=None ):245 def savepoint_commit(sid, using=None, only_if_needed=False): 245 246 """ 246 247 Commits the most recent savepoint (if one exists). Does nothing if 247 savepoints are not supported. 248 savepoints are not supported, or if only_if_needed is set and the backend 249 does not need to use savepoints to recover from exceptions. 248 250 """ 249 251 if using is None: 250 252 using = DEFAULT_DB_ALIAS 251 253 connection = connections[using] 252 254 thread_ident = thread.get_ident() 253 255 if thread_ident in savepoint_state and using in savepoint_state[thread_ident]: 254 connection._savepoint_commit(sid )256 connection._savepoint_commit(sid, only_if_needed) 255 257 256 258 ############## 257 259 # DECORATORS # -
django/contrib/sessions/backends/db.py
58 58 session_data = self.encode(self._get_session(no_load=must_create)), 59 59 expire_date = self.get_expiry_date() 60 60 ) 61 sid = transaction.savepoint(using=self.using )61 sid = transaction.savepoint(using=self.using, only_if_needed=True) 62 62 try: 63 63 obj.save(force_insert=must_create) 64 64 except IntegrityError: 65 65 if must_create: 66 transaction.savepoint_rollback(sid, using=self.using) 66 transaction.savepoint_rollback(sid, using=self.using, 67 only_if_needed=True) 67 68 raise CreateError 68 69 raise 69 70