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 C++ Downloads
#include <CkStringBuilder.h>
#include <CkJsonObject.h>
#include <CkRest.h>
#include <CkAuthAzureStorage.h>
#include <CkSocket.h>
#include <CkPrng.h>
#include <CkBinData.h>
#include <CkMime.h>
#include <CkHashtable.h>
void ChilkatSample(void)
{
bool success = false;
// StringBuilder compares text by value in every generated language.
CkStringBuilder textCompare;
bool applyChange = false;
if (applyChange != true) {
std::cout << "This example writes an atomic transaction. Review the target and deliberately enable applyChange." << "\r\n";
return;
}
// Assumes Chilkat is unlocked. See Global Unlock Sample.
success = false;
CkJsonObject config;
config.put_Strict(true);
success = config.LoadFile("qa_data/azure/storage.json");
if (success != true) {
std::cout << "Could not load private Azure configuration." << "\r\n";
return;
}
textCompare.Clear();
textCompare.SetString(config.stringOf("account"));
bool sameText1 = textCompare.ContentsEqual("",true);
if ((config.JsonTypeOf("account") != 1) || (sameText1 == true)) {
std::cout << "Supply the storage account name." << "\r\n";
return;
}
// access_key is the base64 account key, not a connection string or SAS.
textCompare.Clear();
textCompare.SetString(config.stringOf("access_key"));
bool sameText2 = textCompare.ContentsEqual("",true);
if ((config.JsonTypeOf("access_key") != 1) || (sameText2 == true)) {
std::cout << "Supply the base64 account access key." << "\r\n";
return;
}
CkStringBuilder accountText;
success = accountText.Append(config.stringOf("account"));
if (success != true) {
std::cout << "Could not prepare the request." << "\r\n";
return;
}
if ((accountText.get_Length() < 3) || (accountText.get_Length() > 24)) {
std::cout << "Storage account names need 3-24 lowercase letters or digits." << "\r\n";
return;
}
CkStringBuilder allowedAccountChars;
success = allowedAccountChars.Append("abcdefghijklmnopqrstuvwxyz0123456789");
if (success != true) {
std::cout << "Could not prepare the request." << "\r\n";
return;
}
int charIndex = 0;
while ((charIndex < accountText.get_Length())) {
if (allowedAccountChars.Contains(accountText.getRange(charIndex,1,false),true) != true) {
std::cout << "Storage account names need lowercase letters or digits." << "\r\n";
return;
}
charIndex = charIndex + 1;
}
CkStringBuilder endpoint;
success = endpoint.Append(config.stringOf("account"));
if (success != true) {
std::cout << "Could not prepare the request." << "\r\n";
return;
}
success = endpoint.Append(".table.core.windows.net");
if (success != true) {
std::cout << "Could not prepare the request." << "\r\n";
return;
}
// Table names contain only letters and digits and are case-insensitive.
// Entity PartitionKey and RowKey values are case-sensitive.
const char *resourcePath = "/$batch";
CkRest rest;
rest.put_ConnectTimeoutMs(20000);
rest.put_IdleTimeoutMs(60000);
CkAuthAzureStorage azAuth;
azAuth.put_Account(config.stringOf("account"));
azAuth.put_AccessKey(config.stringOf("access_key"));
azAuth.put_Service("Table");
azAuth.put_Scheme("SharedKey");
azAuth.put_XMsVersion("2026-04-06");
success = rest.SetAuthAzureStorage(azAuth);
if (success != true) {
std::cout << "Could not configure Shared Key signing." << "\r\n";
return;
}
CkSocket connection;
connection.put_RequireSslCertVerify(true);
connection.put_MaxReadIdleMs(60000);
connection.put_MaxSendIdleMs(60000);
success = connection.Connect(endpoint.getAsString(),443,true,20000);
if (success != true) {
std::cout << "TLS connection failed; inspect diagnostics privately." << "\r\n";
return;
}
// Do not replay a write automatically after losing its response.
success = rest.UseConnection(connection,false);
if (success != true) {
std::cout << "Could not prepare the request." << "\r\n";
return;
}
const char *responseText = 0;
CkJsonObject result;
result.put_Strict(true);
success = rest.AddHeader("DataServiceVersion","3.0;NetFx");
if (success != true) {
std::cout << "Could not prepare the request." << "\r\n";
return;
}
success = rest.AddHeader("MaxDataServiceVersion","3.0;NetFx");
if (success != true) {
std::cout << "Could not prepare the request." << "\r\n";
return;
}
success = rest.AddHeader("Content-Type","application/json");
if (success != true) {
std::cout << "Could not prepare the request." << "\r\n";
return;
}
success = rest.AddHeader("Accept","application/json;odata=minimalmetadata");
if (success != true) {
std::cout << "Could not prepare the request." << "\r\n";
return;
}
// 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.
CkPrng random;
const char *nonce = random.genRandom(16,"hex");
if (random.get_LastMethodSuccess() != true) {
std::cout << "Could not create MIME boundaries." << "\r\n";
return;
}
CkStringBuilder batchBoundary;
CkStringBuilder changesetBoundary;
success = batchBoundary.Append("batch_");
if (success != true) {
std::cout << "Could not prepare the request." << "\r\n";
return;
}
success = batchBoundary.Append(nonce);
if (success != true) {
std::cout << "Could not prepare the request." << "\r\n";
return;
}
success = changesetBoundary.Append("changeset_");
if (success != true) {
std::cout << "Could not prepare the request." << "\r\n";
return;
}
success = changesetBoundary.Append(nonce);
if (success != true) {
std::cout << "Could not prepare the request." << "\r\n";
return;
}
CkStringBuilder wire;
success = wire.Append("--");
if (success != true) {
std::cout << "Could not prepare the request." << "\r\n";
return;
}
success = wire.Append(batchBoundary.getAsString());
if (success != true) {
std::cout << "Could not prepare the request." << "\r\n";
return;
}
success = wire.Append("\r\nContent-Type: multipart/mixed; boundary=");
if (success != true) {
std::cout << "Could not prepare the request." << "\r\n";
return;
}
success = wire.Append(changesetBoundary.getAsString());
if (success != true) {
std::cout << "Could not prepare the request." << "\r\n";
return;
}
success = wire.Append("\r\n\r\n");
if (success != true) {
std::cout << "Could not prepare the request." << "\r\n";
return;
}
success = wire.Append("--");
if (success != true) {
std::cout << "Could not prepare the request." << "\r\n";
return;
}
success = wire.Append(changesetBoundary.getAsString());
if (success != true) {
std::cout << "Could not prepare the request." << "\r\n";
return;
}
success = wire.Append("\r\nContent-Type: application/http\r\nContent-Transfer-Encoding: binary\r\nContent-ID: 1\r\n\r\nPOST https://");
if (success != true) {
std::cout << "Could not prepare the request." << "\r\n";
return;
}
success = wire.Append(endpoint.getAsString());
if (success != true) {
std::cout << "Could not prepare the request." << "\r\n";
return;
}
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 != true) {
std::cout << "Could not prepare the request." << "\r\n";
return;
}
CkJsonObject item1;
success = item1.UpdateString("PartitionKey","customers-west");
if (success != true) {
std::cout << "Could not prepare the request." << "\r\n";
return;
}
success = item1.UpdateString("RowKey","customer-001");
if (success != true) {
std::cout << "Could not prepare the request." << "\r\n";
return;
}
success = item1.UpdateString("Address","Batch address 1");
if (success != true) {
std::cout << "Could not prepare the request." << "\r\n";
return;
}
success = wire.Append("Content-Type: application/json\r\nPrefer: return-no-content\r\n\r\n");
if (success != true) {
std::cout << "Could not prepare the request." << "\r\n";
return;
}
success = wire.Append(item1.emit());
if (success != true) {
std::cout << "Could not prepare the request." << "\r\n";
return;
}
success = wire.Append("\r\n");
if (success != true) {
std::cout << "Could not prepare the request." << "\r\n";
return;
}
success = wire.Append("--");
if (success != true) {
std::cout << "Could not prepare the request." << "\r\n";
return;
}
success = wire.Append(changesetBoundary.getAsString());
if (success != true) {
std::cout << "Could not prepare the request." << "\r\n";
return;
}
success = wire.Append("\r\nContent-Type: application/http\r\nContent-Transfer-Encoding: binary\r\nContent-ID: 2\r\n\r\nPOST https://");
if (success != true) {
std::cout << "Could not prepare the request." << "\r\n";
return;
}
success = wire.Append(endpoint.getAsString());
if (success != true) {
std::cout << "Could not prepare the request." << "\r\n";
return;
}
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 != true) {
std::cout << "Could not prepare the request." << "\r\n";
return;
}
CkJsonObject item2;
success = item2.UpdateString("PartitionKey","customers-west");
if (success != true) {
std::cout << "Could not prepare the request." << "\r\n";
return;
}
success = item2.UpdateString("RowKey","customer-002");
if (success != true) {
std::cout << "Could not prepare the request." << "\r\n";
return;
}
success = item2.UpdateString("Address","Batch address 2");
if (success != true) {
std::cout << "Could not prepare the request." << "\r\n";
return;
}
success = wire.Append("Content-Type: application/json\r\nPrefer: return-no-content\r\n\r\n");
if (success != true) {
std::cout << "Could not prepare the request." << "\r\n";
return;
}
success = wire.Append(item2.emit());
if (success != true) {
std::cout << "Could not prepare the request." << "\r\n";
return;
}
success = wire.Append("\r\n");
if (success != true) {
std::cout << "Could not prepare the request." << "\r\n";
return;
}
success = wire.Append("--");
if (success != true) {
std::cout << "Could not prepare the request." << "\r\n";
return;
}
success = wire.Append(changesetBoundary.getAsString());
if (success != true) {
std::cout << "Could not prepare the request." << "\r\n";
return;
}
success = wire.Append("--\r\n");
if (success != true) {
std::cout << "Could not prepare the request." << "\r\n";
return;
}
success = wire.Append("--");
if (success != true) {
std::cout << "Could not prepare the request." << "\r\n";
return;
}
success = wire.Append(batchBoundary.getAsString());
if (success != true) {
std::cout << "Could not prepare the request." << "\r\n";
return;
}
success = wire.Append("--\r\n");
if (success != true) {
std::cout << "Could not prepare the request." << "\r\n";
return;
}
CkBinData payloadBytes;
success = payloadBytes.AppendString(wire.getAsString(),"utf-8");
if (success != true) {
std::cout << "Could not prepare the request." << "\r\n";
return;
}
if (payloadBytes.get_NumBytes() > 4194304) {
std::cout << "Batch exceeds the 4 MiB limit including multipart overhead." << "\r\n";
return;
}
CkStringBuilder contentType;
success = contentType.Append("multipart/mixed; boundary=");
if (success != true) {
std::cout << "Could not prepare the request." << "\r\n";
return;
}
success = contentType.Append(batchBoundary.getAsString());
if (success != true) {
std::cout << "Could not prepare the request." << "\r\n";
return;
}
success = rest.AddHeader("Content-Type",contentType.getAsString());
if (success != true) {
std::cout << "Could not prepare the request." << "\r\n";
return;
}
success = rest.AddHeader("Accept","multipart/mixed");
if (success != true) {
std::cout << "Could not prepare the request." << "\r\n";
return;
}
responseText = rest.fullRequestString("POST",resourcePath,wire.getAsString());
if (rest.get_LastMethodSuccess() != true) {
std::cout << "Request did not complete; inspect the target before retrying a write." << "\r\n";
return;
}
if (rest.get_ResponseStatusCode() != 202) {
std::cout << "HTTP status: " << rest.get_ResponseStatusCode() << "\r\n";
std::cout << "Request ID: " << rest.responseHdrByName("x-ms-request-id") << "\r\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.
return;
}
// Outer 202 only acknowledges the batch envelope, not transaction success.
CkStringBuilder responseMimeText;
success = responseMimeText.Append("Content-Type: ");
if (success != true) {
std::cout << "Could not prepare the request." << "\r\n";
return;
}
success = responseMimeText.Append(rest.responseHdrByName("Content-Type"));
if (success != true) {
std::cout << "Could not prepare the request." << "\r\n";
return;
}
success = responseMimeText.Append("\r\nMIME-Version: 1.0\r\n\r\n");
if (success != true) {
std::cout << "Could not prepare the request." << "\r\n";
return;
}
success = responseMimeText.Append(responseText);
if (success != true) {
std::cout << "Could not prepare the request." << "\r\n";
return;
}
CkMime envelope;
CkMime changeset;
CkMime part;
success = envelope.LoadMime(responseMimeText.getAsString());
if (success != true) {
std::cout << "Invalid MIME batch response; reconcile before retrying." << "\r\n";
return;
}
textCompare.Clear();
textCompare.SetString(envelope.contentType());
bool sameText3 = textCompare.ContentsEqual("multipart/mixed",true);
if ((sameText3 != true) || (envelope.get_NumParts() != 1)) {
std::cout << "Expected one changeset response." << "\r\n";
return;
}
success = envelope.PartAt(0,changeset);
if (success != true) {
std::cout << "Could not prepare the request." << "\r\n";
return;
}
textCompare.Clear();
textCompare.SetString(changeset.contentType());
bool sameText4 = textCompare.ContentsEqual("multipart/mixed",true);
if ((sameText4 != true) || (changeset.get_NumParts() < 1) || (changeset.get_NumParts() > 2)) {
std::cout << "Invalid changeset response." << "\r\n";
return;
}
CkHashtable responseIds;
CkStringBuilder inner;
CkStringBuilder statusLine;
CkMime innerHeaders;
int partIndex = 0;
while ((partIndex < changeset.get_NumParts())) {
success = changeset.PartAt(partIndex,part);
if (success != true) {
std::cout << "Could not prepare the request." << "\r\n";
return;
}
textCompare.Clear();
textCompare.SetString(part.contentType());
bool sameText5 = textCompare.ContentsEqual("application/http",true);
if (sameText5 != true) {
std::cout << "Expected application/http response part." << "\r\n";
return;
}
success = inner.SetString(part.getBodyDecoded());
if (success != true) {
std::cout << "Could not prepare the request." << "\r\n";
return;
}
if (inner.Contains("\r\n\r\n",true) != true) {
std::cout << "Incomplete inner HTTP response." << "\r\n";
return;
}
success = statusLine.SetString(inner.getBefore("\r\n",true));
if (success != true) {
std::cout << "Could not prepare the request." << "\r\n";
return;
}
if (statusLine.StartsWith("HTTP/1.1 ",true) != true) {
std::cout << "Invalid inner HTTP status line." << "\r\n";
return;
}
success = innerHeaders.LoadMime(inner.getAsString());
if (success != true) {
std::cout << "Invalid inner response headers." << "\r\n";
return;
}
const char *contentId = innerHeaders.getHeaderField("Content-ID");
textCompare.Clear();
textCompare.SetString(contentId);
bool sameText6 = textCompare.ContentsEqual("1",true);
textCompare.Clear();
textCompare.SetString(contentId);
bool sameText7 = textCompare.ContentsEqual("2",true);
if ((sameText6 != true) && (sameText7 != true)) {
std::cout << "Unexpected or missing response Content-ID." << "\r\n";
return;
}
if (responseIds.Contains(contentId) == true) {
std::cout << "Duplicate response Content-ID." << "\r\n";
return;
}
success = responseIds.AddStr(contentId,"seen");
if (success != true) {
std::cout << "Could not prepare the request." << "\r\n";
return;
}
textCompare.Clear();
textCompare.SetString(statusLine.getNth(1," ",false,false));
bool sameText8 = textCompare.ContentsEqual("204",true);
if (sameText8 != true) {
std::cout << "Atomic transaction did not report success. Operation Content-ID: " << contentId << "\r\n";
std::cout << "Inner HTTP status: " << statusLine.getNth(1," ",false,false) << "\r\n";
// Keep the full error body private. A service-reported operation failure rolls back the changeset.
return;
}
partIndex = partIndex + 1;
}
if ((changeset.get_NumParts() != 2) || (responseIds.Contains("1") != true) || (responseIds.Contains("2") != true)) {
std::cout << "Incomplete success response; inspect state before replaying." << "\r\n";
return;
}
std::cout << "Both operations succeeded in one atomic transaction." << "\r\n";
}