Slight refactoring of isolation setting from r10029.

There was a bug in the way we were reading the DATABASE_OPTIONS setting
and a lot of essentially duplicated code. This is neater.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@10033 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Malcolm Tredinnick 2009-03-12 05:30:51 +00:00
parent 90a3c34b35
commit d4677d4bfb
1 changed files with 13 additions and 26 deletions

View File

@ -63,12 +63,9 @@ class DatabaseWrapper(BaseDatabaseWrapper):
super(DatabaseWrapper, self).__init__(*args, **kwargs) super(DatabaseWrapper, self).__init__(*args, **kwargs)
self.features = DatabaseFeatures() self.features = DatabaseFeatures()
if settings.DATABASE_OPTIONS.get('autocommit', False): autocommit = self.settings_dict["DATABASE_OPTIONS"].get('autocommit', False)
self.features.uses_autocommit = True self.features.uses_autocommit = autocommit
self._iso_level_0() self._set_isolation_level(int(not autocommit))
else:
self.features.uses_autocommit = False
self._iso_level_1()
self.ops = DatabaseOperations() self.ops = DatabaseOperations()
self.client = DatabaseClient(self) self.client = DatabaseClient(self)
self.creation = DatabaseCreation(self) self.creation = DatabaseCreation(self)
@ -116,7 +113,7 @@ class DatabaseWrapper(BaseDatabaseWrapper):
the same transaction is visible across all the queries. the same transaction is visible across all the queries.
""" """
if self.features.uses_autocommit and managed and not self.isolation_level: if self.features.uses_autocommit and managed and not self.isolation_level:
self._iso_level_1() self._set_isolation_level(1)
def _leave_transaction_management(self, managed): def _leave_transaction_management(self, managed):
""" """
@ -124,29 +121,19 @@ class DatabaseWrapper(BaseDatabaseWrapper):
leaving transaction management. leaving transaction management.
""" """
if self.features.uses_autocommit and not managed and self.isolation_level: if self.features.uses_autocommit and not managed and self.isolation_level:
self._iso_level_0() self._set_isolation_level(0)
def _iso_level_0(self): def _set_isolation_level(self, level):
""" """
Do all the related feature configurations for isolation level 0. This Do all the related feature configurations for changing isolation
doesn't touch the uses_autocommit feature, since that controls the levels. This doesn't touch the uses_autocommit feature, since that
movement *between* isolation levels. controls the movement *between* isolation levels.
""" """
assert level in (0, 1)
try: try:
if self.connection is not None: if self.connection is not None:
self.connection.set_isolation_level(0) self.connection.set_isolation_level(level)
finally: finally:
self.isolation_level = 0 self.isolation_level = level
self.features.uses_savepoints = False self.features.uses_savepoints = bool(level)
def _iso_level_1(self):
"""
The "isolation level 1" version of _iso_level_0().
"""
try:
if self.connection is not None:
self.connection.set_isolation_level(1)
finally:
self.isolation_level = 1
self.features.uses_savepoints = True