Wiederholen
Mit Sammlungen den Überblick behalten
Sie können Inhalte basierend auf Ihren Einstellungen speichern und kategorisieren.
Transaktion noch einmal versuchen
Weitere Informationen
Eine ausführliche Dokumentation, die dieses Codebeispiel enthält, finden Sie hier:
Codebeispiel
Nächste Schritte
Wenn Sie nach Codebeispielen für andere Google Cloud -Produkte suchen und filtern möchten, können Sie den Google Cloud -Beispielbrowser verwenden.
Sofern nicht anders angegeben, sind die Inhalte dieser Seite unter der Creative Commons Attribution 4.0 License und Codebeispiele unter der Apache 2.0 License lizenziert. Weitere Informationen finden Sie in den Websiterichtlinien von Google Developers. Java ist eine eingetragene Marke von Oracle und/oder seinen Partnern.
[[["Leicht verständlich","easyToUnderstand","thumb-up"],["Mein Problem wurde gelöst","solvedMyProblem","thumb-up"],["Sonstiges","otherUp","thumb-up"]],[["Schwer verständlich","hardToUnderstand","thumb-down"],["Informationen oder Beispielcode falsch","incorrectInformationOrSampleCode","thumb-down"],["Benötigte Informationen/Beispiele nicht gefunden","missingTheInformationSamplesINeed","thumb-down"],["Problem mit der Übersetzung","translationIssue","thumb-down"],["Sonstiges","otherDown","thumb-down"]],[],[[["\u003cp\u003eThis document provides code samples demonstrating how to retry transactions in Google Cloud Datastore across various programming languages, including C#, Go, Java, Node.js, PHP, Python, and Ruby.\u003c/p\u003e\n"],["\u003cp\u003eThe code examples implement retry mechanisms to handle potential transaction conflicts or exceptions, like \u003ccode\u003eGrpc.Core.RpcException\u003c/code\u003e or \u003ccode\u003eGoogle.Cloud.Core.Exception.ConflictException\u003c/code\u003e, which can occur in concurrent environments.\u003c/p\u003e\n"],["\u003cp\u003eMost of the provided examples showcase implementing retries with a loop and a counter, with some using exponential backoff for improved efficiency.\u003c/p\u003e\n"],["\u003cp\u003eThe document provides links to Datastore mode client libraries, API reference documentations and authentication setup information for each programming language.\u003c/p\u003e\n"],["\u003cp\u003eThe code samples show how to use transactions with in a variety of languages in order to transfer funds between two accounts.\u003c/p\u003e\n"]]],[],null,["# Retry a transaction.\n\nExplore further\n---------------\n\n\nFor detailed documentation that includes this code sample, see the following:\n\n- [Cloud Datastore Transactions](/datastore/docs/concepts/cloud-datastore-transactions)\n- [Transactions](/datastore/docs/concepts/transactions)\n\nCode sample\n-----------\n\n### C#\n\n\nTo learn how to install and use the client library for Datastore mode, see\n[Datastore mode client libraries](/datastore/docs/reference/libraries).\n\n\nFor more information, see the\n[Datastore mode C# API\nreference documentation](https://cloud.google.com/dotnet/docs/reference/Google.Cloud.Datastore.V1/latest).\n\n\nTo authenticate to Datastore mode, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n /// \u003csummary\u003e\n /// Retry the action when a Grpc.Core.RpcException is thrown.\n /// \u003c/summary\u003e\n private T RetryRpc\u003cT\u003e(Func\u003cT\u003e action)\n {\n List\u003cGrpc.Core.RpcException\u003e exceptions = null;\n var delayMs = _retryDelayMs;\n for (int tryCount = 0; tryCount \u003c _retryCount; ++tryCount)\n {\n try\n {\n return action();\n }\n catch (Grpc.Core.RpcException e)\n {\n if (exceptions == null)\n exceptions = new List\u003cGrpc.Core.RpcException\u003e();\n exceptions.Add(e);\n }\n System.Threading.Thread.Sleep(delayMs);\n delayMs *= 2; // Exponential back-off.\n }\n throw new AggregateException(exceptions);\n }\n\n private void RetryRpc(Action action)\n {\n RetryRpc(() =\u003e { action(); return 0; });\n }\n\n [Fact]\n public void TestTransactionalRetry()\n {\n int tryCount = 0;\n var keys = UpsertBalances();\n RetryRpc(() =\u003e\n {\n using (var transaction = _db.BeginTransaction())\n {\n TransferFunds(keys[0], keys[1], 10, transaction);\n // Insert a conflicting transaction on the first try.\n if (tryCount++ == 0)\n TransferFunds(keys[1], keys[0], 5);\n transaction.Commit();\n }\n });\n Assert.Equal(2, tryCount);\n }\n\n### Go\n\n\nTo learn how to install and use the client library for Datastore mode, see\n[Datastore mode client libraries](/datastore/docs/reference/libraries).\n\n\nFor more information, see the\n[Datastore mode Go API\nreference documentation](https://cloud.google.com/go/docs/reference/cloud.google.com/go/datastore/latest).\n\n\nTo authenticate to Datastore mode, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n type BankAccount struct {\n \tBalance int\n }\n\n const amount = 50\n _, err := client.RunInTransaction(ctx, func(tx *datastore.Transaction) error {\n \tkeys := []*datastore.Key{to, from}\n \taccs := make([]BankAccount, 2)\n \tif err := tx.GetMulti(keys, accs); err != nil {\n \t\treturn err\n \t}\n \taccs[0].Balance += amount\n \taccs[1].Balance -= amount\n \t_, err := tx.PutMulti(keys, accs)\n \treturn err\n })\n\n### Java\n\n\nTo learn how to install and use the client library for Datastore mode, see\n[Datastore mode client libraries](/datastore/docs/reference/libraries).\n\n\nFor more information, see the\n[Datastore mode Java API\nreference documentation](https://cloud.google.com/java/docs/reference/google-cloud-datastore/latest/history).\n\n\nTo authenticate to Datastore mode, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n int retries = 5;\n while (true) {\n try {\n transferFunds(fromKey, toKey, 10);\n break;\n } catch (DatastoreException e) {\n if (retries == 0) {\n throw e;\n }\n --retries;\n }\n }\n // Retry handling can also be configured and automatically applied using google-cloud-java.\n\n### Node.js\n\n\nTo learn how to install and use the client library for Datastore mode, see\n[Datastore mode client libraries](/datastore/docs/reference/libraries).\n\n\nFor more information, see the\n[Datastore mode Node.js API\nreference documentation](https://cloud.google.com/nodejs/docs/reference/datastore/latest).\n\n\nTo authenticate to Datastore mode, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n async function transferFundsWithRetry() {\n const maxTries = 5;\n\n async function tryRequest(currentAttempt, delay) {\n try {\n await transferFunds(fromKey, toKey, 10);\n } catch (err) {\n if (currentAttempt \u003c= maxTries) {\n // Use exponential backoff\n setTimeout(async () =\u003e {\n await tryRequest(currentAttempt + 1, delay * 2);\n }, delay);\n }\n throw err;\n }\n }\n\n await tryRequest(1, 100);\n }\n\n### PHP\n\n\nTo learn how to install and use the client library for Datastore mode, see\n[Datastore mode client libraries](/datastore/docs/reference/libraries).\n\n\nFor more information, see the\n[Datastore mode PHP API\nreference documentation](https://googleapis.github.io/google-cloud-php/#/docs/cloud-datastore/latest).\n\n\nTo authenticate to Datastore mode, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n $retries = 5;\n for ($i = 0; $i \u003c $retries; $i++) {\n try {\n require_once __DIR__ . '/transfer_funds.php';\n transfer_funds($fromKeyId, $toKeyId, 10, $namespaceId);\n } catch (\\Google\\Cloud\\Core\\Exception\\ConflictException $e) {\n // if $i \u003e= $retries, the failure is final\n continue;\n }\n // Succeeded!\n break;\n }\n\n### Python\n\n\nTo learn how to install and use the client library for Datastore mode, see\n[Datastore mode client libraries](/datastore/docs/reference/libraries).\n\n\nFor more information, see the\n[Datastore mode Python API\nreference documentation](https://cloud.google.com/python/docs/reference/datastore/latest).\n\n\nTo authenticate to Datastore mode, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n from google.cloud import https://cloud.google.com/python/docs/reference/datastore/latest/\n\n # For help authenticating your client, visit\n # https://cloud.google.com/docs/authentication/getting-started\n client = https://cloud.google.com/python/docs/reference/datastore/latest/.https://cloud.google.com/python/docs/reference/datastore/latest/google.cloud.datastore.client.Client.html()\n\n import google.cloud.exceptions\n\n for _ in range(5):\n try:\n transfer_funds(client, account1.key, account2.key, 50)\n break\n except google.cloud.exceptions.Conflict:\n continue\n else:\n print(\"Transaction failed.\")\n\n### Ruby\n\n\nTo learn how to install and use the client library for Datastore mode, see\n[Datastore mode client libraries](/datastore/docs/reference/libraries).\n\n\nFor more information, see the\n[Datastore mode Ruby API\nreference documentation](/ruby/docs/reference/google-cloud-datastore/latest).\n\n\nTo authenticate to Datastore mode, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n (1..5).each do |i|\n begin\n return transfer_funds from_key, to_key, amount\n rescue Google::Cloud::Error =\u003e e\n raise e if i == 5\n end\n end\n\nWhat's next\n-----------\n\n\nTo search and filter code samples for other Google Cloud products, see the\n[Google Cloud sample browser](/docs/samples?product=datastore)."]]