Changeset 3233
- Timestamp:
- 06/28/06 16:38:04 (2 years ago)
- Files:
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
django/branches/multiple-db-support/django/db/transaction.py
r3135 r3233 17 17 except ImportError: 18 18 import dummy_thread as thread 19 from django .db import connection19 from django import db 20 20 from django.conf import settings 21 21 … … 117 117 to be committed explicitly by the user. If you switch off transaction 118 118 management and there is a pending commit/rollback, the data will be 119 commited. 119 commited. Note that managed state applies across all connections. 120 120 """ 121 121 thread_ident = thread.get_ident() … … 124 124 top[-1] = flag 125 125 if not flag and is_dirty(): 126 connection._commit() 126 for cx in all_connections(): 127 cx._commit() 127 128 set_clean() 128 129 else: 129 130 raise TransactionManagementError("This code isn't under transaction management") 130 131 131 def commit_unless_managed( ):132 def commit_unless_managed(connections=None): 132 133 """ 133 134 Commits changes if the system is not in managed transaction mode. 134 135 """ 135 136 if not is_managed(): 136 connection._commit() 137 if connections is None: 138 connections = all_connections() 139 else: 140 connections = ensure_connections(connections) 141 for cx in connections: 142 cx._commit() 137 143 else: 138 144 set_dirty() 139 145 140 def rollback_unless_managed( ):146 def rollback_unless_managed(connections=None): 141 147 """ 142 148 Rolls back changes if the system is not in managed transaction mode. 143 149 """ 144 150 if not is_managed(): 145 connection._rollback() 151 if connections is None: 152 connections = all_connections() 153 for cx in connections: 154 cx._rollback() 146 155 else: 147 156 set_dirty() 148 157 149 def commit( ):158 def commit(connections=None): 150 159 """ 151 160 Does the commit itself and resets the dirty flag. 152 161 """ 153 connection._commit() 162 if connections is None: 163 connections = all_connections() 164 else: 165 connections = ensure_connections(connections) 166 for cx in connections: 167 cx._commit() 154 168 set_clean() 155 169 156 def rollback( ):170 def rollback(connections=None): 157 171 """ 158 172 This function does the rollback itself and resets the dirty flag. 159 173 """ 160 connection._rollback() 174 if connections is None: 175 connections = all_connections() 176 else: 177 connections = ensure_connections(connections) 178 for cx in connections: 179 cx._rollback() 161 180 set_clean() 162 181 … … 180 199 return _autocommit 181 200 182 def commit_on_success(func ):201 def commit_on_success(func, connections=None): 183 202 """ 184 203 This decorator activates commit on response. This way, if the view function … … 199 218 else: 200 219 if is_dirty(): 201 commit( )220 commit(connections) 202 221 return res 203 222 finally: … … 221 240 222 241 return _commit_manually 242 243 ########### 244 # HELPERS # 245 ########### 246 247 def all_connections(): 248 return [db.connection] + [ c.connection 249 for c in db.connections.values() ] 250 251 def ensure_connections(val): 252 connections = [] 253 if isinstance(val, basestring): 254 val = [val] 255 try: 256 iter(val) 257 except: 258 val = [val] 259 for cx in val: 260 if hasattr(cx, 'cursor'): 261 connections.append(cx) 262 elif hasattr(cx, 'connection'): 263 connections.append(cx.connection) 264 elif isinstance(cx, basestring): 265 connections.append(db.connections[cx].connection) 266 return connections 267
