Batch/Transaction
Do multiple operations in one call
Introduction
Transaction interaction allows for several interactions using one HTTP request. There are two types of transaction interaction (type is specified by field type
): batch and transaction. The first one executes requests one by one, and the second one does the same but rolls back all changes if any request fails.
The body of such a request contains one resource of type Bundle, which contains field entry with an array of interactions, for example:
Each element of the entry array contains a resource field (body of the request) and a request field (request line in terms of the HTTP request).
Using references
Sometimes, we may need to reference a resource within a bundle that has not yet been created. For this purpose, you can use the fullUrl
field, available for each resource, which acts as a unique identifier at the bundle level (a logical reference).
You can then reference this identifier from another resource using a FHIR reference, e.g.,"reference": "urn:uuid:<uuid|string>"
. Those references are temporary (existing during bundle processing) and will be translated to valid FHIR references when the bundle is processed.
Processing rules
Differences from the FHIR transaction processing rules:
Each entry is processed in the order provided in a bundle
For
type: batch
references to resources inside a bundle won't be resolved.For
type: transaction
before processing interactions, all references in a resource will attempt to resolve. In this example, Patient will refer to a newly created Organization:
Multiple resources with the same id
If you have multiple entries with the same resource id, Aidbox will execute them one by one and thus you can create a resource with a history within a single transaction:
Response of GET /fhir/Patient/pt-1/_history
:
Change transaction isolation level
By default Aidbox uses SERIALIZABLE
transaction isolation level. This may lead to some transactions being rejected when there are many concurrent requests.
See more about transaction isolation in Postgres documentation.
The best way to handle rejected transactions is to retry them. If it is not possible, you can set the maximum isolation level with the HTTP header or environment variable. If both the HTTP header and environment variable are provided, the header will be used.
Using an isolation level lower than serializable may lead to data serialization anomalies.
Example:
Last updated