Part of lp.services.database.transaction_policy View In Hierarchy
Context manager for read-only transaction policy. Use this to define regions of code that explicitly allow or disallow changes to the database: # We want to be sure that inspect_data does not inadvertently # make any changes in the database, but we can't run it on the # slave store because it doesn't tolerate replication lag. with DatabaseTransactionPolicy(read_only=True): inspect_data() The simplest way to use this is as a special transaction: * You must commit/abort before entering the policy. * Exiting the policy through an exception aborts its changes. * Before completing a read-write policy region, you must commit or abort. You can also have multiple transactions inside one policy, however; the policy still applies after a commit or abort. Policies can be nested--a nested policy overrides the one it's nested in. After the nested policy has exited, the previous policy applies again: # This code needs to control the database changes it makes very # carefully. Most of it is just gathering data, with one quick # database update at the end. with DatabaseTransactionPolicy(read_only=True): data = gather_data() more_data = figure_stuff_out(data) # End the ongoing transaction so we can go into our update. transaction.commit() # This is the only part where we update the database! with DatabaseTransactionPolicy(read_only=False): update_model(data, more_data) transaction.commit() # We've got a bit more work to do here, but it doesn't # affect the database. write_logs(data) notify_user(more_data)
|Method||__init__||Create a policy.|
|Method||__enter__||Enter this policy.|
|Method||__exit__||Exit this policy.|
|Method||_isInTransaction||Is our store currently in a transaction?|
|Method||_checkNoTransaction||Verify that no transaction is ongoing.|
|Method||_flushPendingWrites||Flush any pending object changes to the database.|
|Method||_getCurrentPolicy||Read the database session's default transaction read-only policy.|
|Method||_setPolicy||Set the database session's default transaction read-only policy.|
Merely creating a policy has no effect. Use it with "with" to affect writability of database transactions.
|Parameters||store||The store to set policy on. Defaults to the main master store. You don't want to use this on a slave store!|
|read_only||Is this policy read-only?|
Commits the ongoing transaction, and sets the selected default read-only policy on the database.
|Raises||TransactionInProgress||if a transaction was already ongoing.|
Commits or aborts, depending on mode of exit, and restores the previous default read-only policy.
|Returns||True -- any exception will continue to propagate.|
|Raises||TransactionInProgress||if trying to exit normally from a read-write policy without closing its transaction first.|
|Parameters||error_msg||The error message to use if the user got this wrong (i.e. if we're in a transaction).|
|Raises||TransactionInProgress||if we're in a transaction.|
Flush any pending object changes to the database.
If you see an
InternalError exception during this flush, it probably
means one of two things:
The information is retrieved from the database, so this will give a sensible answer even when no DatabaseTransactionPolicy is in effect.
|Returns||True for read-only policy, False for read-write policy.|