Groupe d'entités unique transactionnel en lecture seule
Restez organisé à l'aide des collections
Enregistrez et classez les contenus selon vos préférences.
Utiliser une transaction en lecture seule.
En savoir plus
Pour obtenir une documentation détaillée incluant cet exemple de code, consultez les articles suivants :
Exemple de code
Sauf indication contraire, le contenu de cette page est régi par une licence Creative Commons Attribution 4.0, et les échantillons de code sont régis par une licence Apache 2.0. Pour en savoir plus, consultez les Règles du site Google Developers. Java est une marque déposée d'Oracle et/ou de ses sociétés affiliées.
[[["Facile à comprendre","easyToUnderstand","thumb-up"],["J'ai pu résoudre mon problème","solvedMyProblem","thumb-up"],["Autre","otherUp","thumb-up"]],[["Difficile à comprendre","hardToUnderstand","thumb-down"],["Informations ou exemple de code incorrects","incorrectInformationOrSampleCode","thumb-down"],["Il n'y a pas l'information/les exemples dont j'ai besoin","missingTheInformationSamplesINeed","thumb-down"],["Problème de traduction","translationIssue","thumb-down"],["Autre","otherDown","thumb-down"]],[],[[["\u003cp\u003eThe provided code examples demonstrate how to use read-only transactions in Cloud Datastore across multiple programming languages like C#, Go, Java, Node.js, PHP, Python, and Ruby.\u003c/p\u003e\n"],["\u003cp\u003eRead-only transactions are initiated using the \u003ccode\u003eTransactionOptions.CreateReadOnly()\u003c/code\u003e method in C#, \u003ccode\u003edatastore.ReadOnly\u003c/code\u003e in Go, \u003ccode\u003eTransactionOptions.newBuilder().setReadOnly()\u003c/code\u003e in Java, \u003ccode\u003ereadOnly: true\u003c/code\u003e in Node.js, \u003ccode\u003ereadOnlyTransaction()\u003c/code\u003e in PHP, \u003ccode\u003eread_only=True\u003c/code\u003e in python and \u003ccode\u003eread_only_transaction\u003c/code\u003e in Ruby.\u003c/p\u003e\n"],["\u003cp\u003eThe code samples query entities of a specific kind (\u003ccode\u003eTask\u003c/code\u003e) that have a specified ancestor (\u003ccode\u003etaskListKey\u003c/code\u003e), within the context of a read-only transaction.\u003c/p\u003e\n"],["\u003cp\u003eThe code retrieves entities by using a query with the ancestor \u003ccode\u003etaskListKey\u003c/code\u003e and then runs that query within the context of a read-only transaction, and the results are committed or rolled back.\u003c/p\u003e\n"],["\u003cp\u003eTo get started, users are referred to the Datastore mode client libraries and authentication setup instructions in the documentation.\u003c/p\u003e\n"]]],[],null,["# Transactional single entity group read-only\n\nUse a read-only 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 Entity taskList;\n IReadOnlyList\u003cEntity\u003e tasks;\n using (var transaction = _db.BeginTransaction(TransactionOptions.CreateReadOnly()))\n {\n taskList = transaction.Lookup(taskListKey);\n var query = new Query(\"Task\")\n {\n Filter = Filter.HasAncestor(taskListKey)\n };\n tasks = transaction.RunQuery(query).Entities;\n transaction.Commit();\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 tx, err := client.NewTransaction(ctx, datastore.ReadOnly)\n if err != nil {\n \tlog.Fatalf(\"client.NewTransaction: %v\", err)\n }\n defer tx.Rollback() // Transaction only used for read.\n\n ancestor := datastore.NameKey(\"TaskList\", \"default\", nil)\n query := datastore.NewQuery(\"Task\").Ancestor(ancestor).Transaction(tx)\n var tasks []Task\n _, err = client.GetAll(ctx, query, &tasks)\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 Entity taskList;\n QueryResults\u003cEntity\u003e tasks;\n Transaction txn =\n datastore.newTransaction(\n TransactionOptions.newBuilder().setReadOnly(ReadOnly.newBuilder().build()).build());\n try {\n taskList = txn.get(taskListKey);\n Query\u003cEntity\u003e query =\n Query.newEntityQueryBuilder()\n .setKind(\"Task\")\n .setFilter(PropertyFilter.hasAncestor(taskListKey))\n .build();\n tasks = txn.run(query);\n txn.commit();\n } finally {\n if (txn.isActive()) {\n txn.rollback();\n }\n }\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 getTaskListEntities() {\n const transaction = datastore.transaction({readOnly: true});\n try {\n const taskListKey = datastore.key(['TaskList', 'default']);\n\n await transaction.run();\n const [taskList] = await transaction.get(taskListKey);\n const query = datastore.createQuery('Task').hasAncestor(taskListKey);\n const [taskListEntities] = await transaction.runQuery(query);\n await transaction.commit();\n return [taskList, taskListEntities];\n } catch (err) {\n await transaction.rollback();\n }\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 $transaction = $datastore-\u003ereadOnlyTransaction();\n $taskListKey = $datastore-\u003ekey('TaskList', 'default');\n $query = $datastore-\u003equery()\n -\u003ekind('Task')\n -\u003ehasAncestor($taskListKey);\n $result = $transaction-\u003erunQuery($query);\n $taskListEntities = [];\n $num = 0;\n /* @var Entity $task */\n foreach ($result as $task) {\n $taskListEntities[] = $task;\n $num += 1;\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 with client.https://cloud.google.com/python/docs/reference/datastore/latest/google.cloud.datastore.client.Client.html#google_cloud_datastore_client_Client_transaction(read_only=True):\n task_list_key = client.https://cloud.google.com/python/docs/reference/datastore/latest/google.cloud.datastore.client.Client.html#google_cloud_datastore_client_Client_key(\"TaskList\", \"default\")\n\n task_list = client.https://cloud.google.com/python/docs/reference/datastore/latest/google.cloud.datastore.client.Client.html#google_cloud_datastore_client_Client_get(task_list_key)\n\n query = client.https://cloud.google.com/python/docs/reference/datastore/latest/google.cloud.datastore.client.Client.html#google_cloud_datastore_client_Client_query(kind=\"Task\", ancestor=task_list_key)\n tasks_in_list = list(query.fetch())\n\n return task_list, tasks_in_list\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 # task_list_name = \"default\"\n task_list_key = datastore.key \"TaskList\", task_list_name\n datastore.read_only_transaction do |tx|\n task_list = tx.find task_list_key\n query = datastore.query(\"Task\").ancestor(task_list)\n tasks_in_list = tx.run query\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)."]]