Azure Table: Insert Two Entities in an Atomic Batch
See more Azure Table Service Examples
Inserts two distinct entities in a single atomic changeset. Existing entities cause conflict and roll back the transaction. Disabled by default. Uses exactly two entities with one fixed table/partition, unique Content-IDs, CRLF multipart framing, and a byte-count check against the 4 MiB request limit. Dynamic batches must additionally preserve the 100-operation, single-partition and unique-entity constraints.
Parses nested MIME responses using Chilkat.Mime, validates inner HTTP status and Content-ID uniqueness/completeness, and requires 204 for both operations. Outer 202 alone is not success. A lost or malformed response has an uncertain outcome; inspect state before replaying. Separate batches are not atomic together.
Targets Azure Storage Table service in the public cloud, not Azure Cosmos DB for Table. Uses Chilkat 11.6.0 or later and an unlocked library. Direct data requests pin x-ms-version 2026-04-06 and OData 3.0. Shared Key examples read private qa_data/azure/storage.json with account and access_key string fields. Authentication examples explain their additional fields. Create private local directories before running; keep credentials, tokens, SAS, entity values, checkpoints and diagnostics outside public content and source control.
HTTPS certificates are verified. Transport completion and HTTP status are checked separately. Writes are not automatically retried after an uncertain outcome. Entity keys, ETags and continuation tokens remain strings. Examples operate on the existing ExampleCustomers table unless noted; review the targets before adapting them. Microsoft Entra ID with suitable Table data roles is preferred where available.
Chilkat PHP ActiveX Downloads
<?php
$success = 0;
// StringBuilder compares text by value in every generated language.
$textCompare = new COM("Chilkat.StringBuilder");
$applyChange = 0;
if ($applyChange != 1) {
print 'This example writes an atomic transaction. Review the target and deliberately enable applyChange.' . "\n";
exit;
}
// Assumes Chilkat is unlocked. See Global Unlock Sample.
$success = 0;
$config = new COM("Chilkat.JsonObject");
$config->Strict = 1;
$success = $config->LoadFile('qa_data/azure/storage.json');
if ($success != 1) {
print 'Could not load private Azure configuration.' . "\n";
exit;
}
$textCompare->Clear();
$textCompare->SetString($config->stringOf('account'));
$sameText1 = $textCompare->ContentsEqual('',1);
if (($config->JsonTypeOf('account') != 1) or ($sameText1 == 1)) {
print 'Supply the storage account name.' . "\n";
exit;
}
// access_key is the base64 account key, not a connection string or SAS.
$textCompare->Clear();
$textCompare->SetString($config->stringOf('access_key'));
$sameText2 = $textCompare->ContentsEqual('',1);
if (($config->JsonTypeOf('access_key') != 1) or ($sameText2 == 1)) {
print 'Supply the base64 account access key.' . "\n";
exit;
}
$accountText = new COM("Chilkat.StringBuilder");
$success = $accountText->Append($config->stringOf('account'));
if ($success != 1) {
print 'Could not prepare the request.' . "\n";
exit;
}
if (($accountText->Length < 3) or ($accountText->Length > 24)) {
print 'Storage account names need 3-24 lowercase letters or digits.' . "\n";
exit;
}
$allowedAccountChars = new COM("Chilkat.StringBuilder");
$success = $allowedAccountChars->Append('abcdefghijklmnopqrstuvwxyz0123456789');
if ($success != 1) {
print 'Could not prepare the request.' . "\n";
exit;
}
$charIndex = 0;
while (($charIndex < $accountText->Length)) {
if ($allowedAccountChars->Contains($accountText->getRange($charIndex,1,0),1) != 1) {
print 'Storage account names need lowercase letters or digits.' . "\n";
exit;
}
$charIndex = $charIndex + 1;
}
$endpoint = new COM("Chilkat.StringBuilder");
$success = $endpoint->Append($config->stringOf('account'));
if ($success != 1) {
print 'Could not prepare the request.' . "\n";
exit;
}
$success = $endpoint->Append('.table.core.windows.net');
if ($success != 1) {
print 'Could not prepare the request.' . "\n";
exit;
}
// Table names contain only letters and digits and are case-insensitive.
// Entity PartitionKey and RowKey values are case-sensitive.
$resourcePath = '/$batch';
$rest = new COM("Chilkat.Rest");
$rest->ConnectTimeoutMs = 20000;
$rest->IdleTimeoutMs = 60000;
$azAuth = new COM("Chilkat.AuthAzureStorage");
$azAuth->Account = $config->stringOf('account');
$azAuth->AccessKey = $config->stringOf('access_key');
$azAuth->Service = 'Table';
$azAuth->Scheme = 'SharedKey';
$azAuth->XMsVersion = '2026-04-06';
$success = $rest->SetAuthAzureStorage($azAuth);
if ($success != 1) {
print 'Could not configure Shared Key signing.' . "\n";
exit;
}
$connection = new COM("Chilkat.Socket");
$connection->RequireSslCertVerify = 1;
$connection->MaxReadIdleMs = 60000;
$connection->MaxSendIdleMs = 60000;
$success = $connection->Connect($endpoint->getAsString(),443,1,20000);
if ($success != 1) {
print 'TLS connection failed; inspect diagnostics privately.' . "\n";
exit;
}
// Do not replay a write automatically after losing its response.
$success = $rest->UseConnection($connection,0);
if ($success != 1) {
print 'Could not prepare the request.' . "\n";
exit;
}
$result = new COM("Chilkat.JsonObject");
$result->Strict = 1;
$success = $rest->AddHeader('DataServiceVersion','3.0;NetFx');
if ($success != 1) {
print 'Could not prepare the request.' . "\n";
exit;
}
$success = $rest->AddHeader('MaxDataServiceVersion','3.0;NetFx');
if ($success != 1) {
print 'Could not prepare the request.' . "\n";
exit;
}
$success = $rest->AddHeader('Content-Type','application/json');
if ($success != 1) {
print 'Could not prepare the request.' . "\n";
exit;
}
$success = $rest->AddHeader('Accept','application/json;odata=minimalmetadata');
if ($success != 1) {
print 'Could not prepare the request.' . "\n";
exit;
}
// Exactly two distinct entities in the same table and partition, by construction.
// Dynamic plans must also enforce <=100 operations, one operation per entity, and one partition.
$random = new COM("Chilkat.Prng");
$nonce = $random->genRandom(16,'hex');
if ($random->LastMethodSuccess != 1) {
print 'Could not create MIME boundaries.' . "\n";
exit;
}
$batchBoundary = new COM("Chilkat.StringBuilder");
$changesetBoundary = new COM("Chilkat.StringBuilder");
$success = $batchBoundary->Append('batch_');
if ($success != 1) {
print 'Could not prepare the request.' . "\n";
exit;
}
$success = $batchBoundary->Append($nonce);
if ($success != 1) {
print 'Could not prepare the request.' . "\n";
exit;
}
$success = $changesetBoundary->Append('changeset_');
if ($success != 1) {
print 'Could not prepare the request.' . "\n";
exit;
}
$success = $changesetBoundary->Append($nonce);
if ($success != 1) {
print 'Could not prepare the request.' . "\n";
exit;
}
$wire = new COM("Chilkat.StringBuilder");
$success = $wire->Append('--');
if ($success != 1) {
print 'Could not prepare the request.' . "\n";
exit;
}
$success = $wire->Append($batchBoundary->getAsString());
if ($success != 1) {
print 'Could not prepare the request.' . "\n";
exit;
}
$success = $wire->Append('\r\nContent-Type: multipart/mixed; boundary=');
if ($success != 1) {
print 'Could not prepare the request.' . "\n";
exit;
}
$success = $wire->Append($changesetBoundary->getAsString());
if ($success != 1) {
print 'Could not prepare the request.' . "\n";
exit;
}
$success = $wire->Append('\r\n\r\n');
if ($success != 1) {
print 'Could not prepare the request.' . "\n";
exit;
}
$success = $wire->Append('--');
if ($success != 1) {
print 'Could not prepare the request.' . "\n";
exit;
}
$success = $wire->Append($changesetBoundary->getAsString());
if ($success != 1) {
print 'Could not prepare the request.' . "\n";
exit;
}
$success = $wire->Append('\r\nContent-Type: application/http\r\nContent-Transfer-Encoding: binary\r\nContent-ID: 1\r\n\r\nPOST https://');
if ($success != 1) {
print 'Could not prepare the request.' . "\n";
exit;
}
$success = $wire->Append($endpoint->getAsString());
if ($success != 1) {
print 'Could not prepare the request.' . "\n";
exit;
}
$success = $wire->Append('/ExampleCustomers HTTP/1.1\r\nAccept: application/json;odata=minimalmetadata\r\nDataServiceVersion: 3.0;NetFx\r\nMaxDataServiceVersion: 3.0;NetFx\r\n');
if ($success != 1) {
print 'Could not prepare the request.' . "\n";
exit;
}
$item1 = new COM("Chilkat.JsonObject");
$success = $item1->UpdateString('PartitionKey','customers-west');
if ($success != 1) {
print 'Could not prepare the request.' . "\n";
exit;
}
$success = $item1->UpdateString('RowKey','customer-001');
if ($success != 1) {
print 'Could not prepare the request.' . "\n";
exit;
}
$success = $item1->UpdateString('Address','Batch address 1');
if ($success != 1) {
print 'Could not prepare the request.' . "\n";
exit;
}
$success = $wire->Append('Content-Type: application/json\r\nPrefer: return-no-content\r\n\r\n');
if ($success != 1) {
print 'Could not prepare the request.' . "\n";
exit;
}
$success = $wire->Append($item1->emit());
if ($success != 1) {
print 'Could not prepare the request.' . "\n";
exit;
}
$success = $wire->Append('\r\n');
if ($success != 1) {
print 'Could not prepare the request.' . "\n";
exit;
}
$success = $wire->Append('--');
if ($success != 1) {
print 'Could not prepare the request.' . "\n";
exit;
}
$success = $wire->Append($changesetBoundary->getAsString());
if ($success != 1) {
print 'Could not prepare the request.' . "\n";
exit;
}
$success = $wire->Append('\r\nContent-Type: application/http\r\nContent-Transfer-Encoding: binary\r\nContent-ID: 2\r\n\r\nPOST https://');
if ($success != 1) {
print 'Could not prepare the request.' . "\n";
exit;
}
$success = $wire->Append($endpoint->getAsString());
if ($success != 1) {
print 'Could not prepare the request.' . "\n";
exit;
}
$success = $wire->Append('/ExampleCustomers HTTP/1.1\r\nAccept: application/json;odata=minimalmetadata\r\nDataServiceVersion: 3.0;NetFx\r\nMaxDataServiceVersion: 3.0;NetFx\r\n');
if ($success != 1) {
print 'Could not prepare the request.' . "\n";
exit;
}
$item2 = new COM("Chilkat.JsonObject");
$success = $item2->UpdateString('PartitionKey','customers-west');
if ($success != 1) {
print 'Could not prepare the request.' . "\n";
exit;
}
$success = $item2->UpdateString('RowKey','customer-002');
if ($success != 1) {
print 'Could not prepare the request.' . "\n";
exit;
}
$success = $item2->UpdateString('Address','Batch address 2');
if ($success != 1) {
print 'Could not prepare the request.' . "\n";
exit;
}
$success = $wire->Append('Content-Type: application/json\r\nPrefer: return-no-content\r\n\r\n');
if ($success != 1) {
print 'Could not prepare the request.' . "\n";
exit;
}
$success = $wire->Append($item2->emit());
if ($success != 1) {
print 'Could not prepare the request.' . "\n";
exit;
}
$success = $wire->Append('\r\n');
if ($success != 1) {
print 'Could not prepare the request.' . "\n";
exit;
}
$success = $wire->Append('--');
if ($success != 1) {
print 'Could not prepare the request.' . "\n";
exit;
}
$success = $wire->Append($changesetBoundary->getAsString());
if ($success != 1) {
print 'Could not prepare the request.' . "\n";
exit;
}
$success = $wire->Append('--\r\n');
if ($success != 1) {
print 'Could not prepare the request.' . "\n";
exit;
}
$success = $wire->Append('--');
if ($success != 1) {
print 'Could not prepare the request.' . "\n";
exit;
}
$success = $wire->Append($batchBoundary->getAsString());
if ($success != 1) {
print 'Could not prepare the request.' . "\n";
exit;
}
$success = $wire->Append('--\r\n');
if ($success != 1) {
print 'Could not prepare the request.' . "\n";
exit;
}
$payloadBytes = new COM("Chilkat.BinData");
$success = $payloadBytes->AppendString($wire->getAsString(),'utf-8');
if ($success != 1) {
print 'Could not prepare the request.' . "\n";
exit;
}
if ($payloadBytes->NumBytes > 4194304) {
print 'Batch exceeds the 4 MiB limit including multipart overhead.' . "\n";
exit;
}
$contentType = new COM("Chilkat.StringBuilder");
$success = $contentType->Append('multipart/mixed; boundary=');
if ($success != 1) {
print 'Could not prepare the request.' . "\n";
exit;
}
$success = $contentType->Append($batchBoundary->getAsString());
if ($success != 1) {
print 'Could not prepare the request.' . "\n";
exit;
}
$success = $rest->AddHeader('Content-Type',$contentType->getAsString());
if ($success != 1) {
print 'Could not prepare the request.' . "\n";
exit;
}
$success = $rest->AddHeader('Accept','multipart/mixed');
if ($success != 1) {
print 'Could not prepare the request.' . "\n";
exit;
}
$responseText = $rest->fullRequestString('POST',$resourcePath,$wire->getAsString());
if ($rest->LastMethodSuccess != 1) {
print 'Request did not complete; inspect the target before retrying a write.' . "\n";
exit;
}
if ($rest->ResponseStatusCode != 202) {
print 'HTTP status: ' . $rest->ResponseStatusCode . "\n";
print 'Request ID: ' . $rest->responseHdrByName('x-ms-request-id') . "\n";
// 404 is missing, 409 is conflict, and 412 is a stale ETag: none are success.
// Keep response bodies private. Reconcile uncertain writes before retrying.
exit;
}
// Outer 202 only acknowledges the batch envelope, not transaction success.
$responseMimeText = new COM("Chilkat.StringBuilder");
$success = $responseMimeText->Append('Content-Type: ');
if ($success != 1) {
print 'Could not prepare the request.' . "\n";
exit;
}
$success = $responseMimeText->Append($rest->responseHdrByName('Content-Type'));
if ($success != 1) {
print 'Could not prepare the request.' . "\n";
exit;
}
$success = $responseMimeText->Append('\r\nMIME-Version: 1.0\r\n\r\n');
if ($success != 1) {
print 'Could not prepare the request.' . "\n";
exit;
}
$success = $responseMimeText->Append($responseText);
if ($success != 1) {
print 'Could not prepare the request.' . "\n";
exit;
}
$envelope = new COM("Chilkat.Mime");
$changeset = new COM("Chilkat.Mime");
$part = new COM("Chilkat.Mime");
$success = $envelope->LoadMime($responseMimeText->getAsString());
if ($success != 1) {
print 'Invalid MIME batch response; reconcile before retrying.' . "\n";
exit;
}
$textCompare->Clear();
$textCompare->SetString($envelope->ContentType);
$sameText3 = $textCompare->ContentsEqual('multipart/mixed',1);
if (($sameText3 != 1) or ($envelope->NumParts != 1)) {
print 'Expected one changeset response.' . "\n";
exit;
}
$success = $envelope->PartAt(0,$changeset);
if ($success != 1) {
print 'Could not prepare the request.' . "\n";
exit;
}
$textCompare->Clear();
$textCompare->SetString($changeset->ContentType);
$sameText4 = $textCompare->ContentsEqual('multipart/mixed',1);
if (($sameText4 != 1) or ($changeset->NumParts < 1) or ($changeset->NumParts > 2)) {
print 'Invalid changeset response.' . "\n";
exit;
}
$responseIds = new COM("Chilkat.Hashtable");
$inner = new COM("Chilkat.StringBuilder");
$statusLine = new COM("Chilkat.StringBuilder");
$innerHeaders = new COM("Chilkat.Mime");
$partIndex = 0;
while (($partIndex < $changeset->NumParts)) {
$success = $changeset->PartAt($partIndex,$part);
if ($success != 1) {
print 'Could not prepare the request.' . "\n";
exit;
}
$textCompare->Clear();
$textCompare->SetString($part->ContentType);
$sameText5 = $textCompare->ContentsEqual('application/http',1);
if ($sameText5 != 1) {
print 'Expected application/http response part.' . "\n";
exit;
}
$success = $inner->SetString($part->getBodyDecoded());
if ($success != 1) {
print 'Could not prepare the request.' . "\n";
exit;
}
if ($inner->Contains('\r\n\r\n',1) != 1) {
print 'Incomplete inner HTTP response.' . "\n";
exit;
}
$success = $statusLine->SetString($inner->getBefore('\r\n',1));
if ($success != 1) {
print 'Could not prepare the request.' . "\n";
exit;
}
if ($statusLine->StartsWith('HTTP/1.1 ',1) != 1) {
print 'Invalid inner HTTP status line.' . "\n";
exit;
}
$success = $innerHeaders->LoadMime($inner->getAsString());
if ($success != 1) {
print 'Invalid inner response headers.' . "\n";
exit;
}
$contentId = $innerHeaders->getHeaderField('Content-ID');
$textCompare->Clear();
$textCompare->SetString($contentId);
$sameText6 = $textCompare->ContentsEqual('1',1);
$textCompare->Clear();
$textCompare->SetString($contentId);
$sameText7 = $textCompare->ContentsEqual('2',1);
if (($sameText6 != 1) and ($sameText7 != 1)) {
print 'Unexpected or missing response Content-ID.' . "\n";
exit;
}
if ($responseIds->Contains($contentId) == 1) {
print 'Duplicate response Content-ID.' . "\n";
exit;
}
$success = $responseIds->AddStr($contentId,'seen');
if ($success != 1) {
print 'Could not prepare the request.' . "\n";
exit;
}
$textCompare->Clear();
$textCompare->SetString($statusLine->getNth(1,' ',0,0));
$sameText8 = $textCompare->ContentsEqual('204',1);
if ($sameText8 != 1) {
print 'Atomic transaction did not report success. Operation Content-ID: ' . $contentId . "\n";
print 'Inner HTTP status: ' . $statusLine->getNth(1,' ',0,0) . "\n";
// Keep the full error body private. A service-reported operation failure rolls back the changeset.
exit;
}
$partIndex = $partIndex + 1;
}
if (($changeset->NumParts != 2) or ($responseIds->Contains('1') != 1) or ($responseIds->Contains('2') != 1)) {
print 'Incomplete success response; inspect state before replaying.' . "\n";
exit;
}
print 'Both operations succeeded in one atomic transaction.' . "\n";
?>