Ejemplo n.º 1
0
        public async Task RollbackAsync()
        {
            string projectId   = _fixture.ProjectId;
            string namespaceId = _fixture.NamespaceId;

            // Snippet: RollbackAsync(*)
            DatastoreDb db         = DatastoreDb.Create(projectId, namespaceId);
            KeyFactory  keyFactory = db.CreateKeyFactory("message");

            // Dispose automatically rolls back an uncommitted transaction synchronously.
            // To roll back asynchronously,
            bool committed = false;
            DatastoreTransaction transaction = await db.BeginTransactionAsync();

            try
            {
                Entity message = new Entity
                {
                    Key      = keyFactory.CreateIncompleteKey(),
                    ["text"] = "Hello",
                };
                // This adds the entity to a collection in memory: nothing
                // is sent to the server.
                db.Insert(message);

                // Attempt to commit the transaction asynchronously.
                await transaction.CommitAsync();

                committed = true;
            }
            finally
            {
                if (!committed)
                {
                    // Roll back asynchronously if anything failed.
                    await transaction.RollbackAsync();
                }
            }
            // End snippet
        }