Most Powerful Open Source ERP

How To Use Transactional Variable

showing how to use transactional variable or data-slots to hold variables during transactions.
  • Last Update:2016-02-11
  • Version:001
  • Language:en

Transactional variables are data slots which can store arbitrary values during a transaction. Transactional variables are guaranteed to vanish when a transaction finishes or aborts, and specific to a single thread and a single transaction. They are never shared by different threads.

Table of Contents

Background

Transactional variables are different from user-defined data in a REQUEST object, in the sense that one request may execute multiple transactions, but not vice versa. If data should persist beyond a transaction, but must not persist over a single request, you should use a REQUEST object instead of transactional variables.

Also, transactional variables are different from volatile attributes, because transactional variables may not disappear within a transaction.

The constraint is that each key must be hashable, so that it can be used as a key to a dictionary.

Example:

from Products.ERP5Type.TransactionalVariable import getTransactionalVariable
tv = getTransactionalVariable()
try:
  toto = tv['toto']
except KeyError:
  toto = tv['toto'] = getToto()

Naming Transactional Variable Keys

The following example shows how to name a transactional variable in order to prevent name conflicts:

acquisition_key = ('_getDefaultAcquiredProperty', self.getPath(), key, base_category,
                    portal_type, copy_value, mask_value, sync_value,
                    accessor_id, depends, storage_id, alt_accessor_id, is_list_type, is_tales_type)

The general form to name transactional variables for ERP5 document methods is:

key = (<>, self.getPath(), arg1, arg2, arg3, etc.)

and if the transactional variable not used on an ERP5 document method:

key = (<>, arg1, arg2, arg3, etc.)

Related Articles