# Module transaction (2.25.0)

2.25.0 (latest) 2.24.0 2.23.0 2.22.0 2.21.0 2.20.2 2.19.0 2.18.0 2.17.0 2.16.1 2.15.2 2.14.0 2.13.2 2.12.0 2.11.1 2.10.0 2.9.0 2.8.3 2.7.2 2.6.2 2.5.1 2.4.0 2.3.0 2.2.0 2.1.6 2.0.1 1.15.5 1.14.0 1.13.2 1.12.0 1.11.0 1.10.0 1.9.0 Create / interact with Google Cloud Datastore transactions.

## Classes

### [Transaction](https://docs.cloud.google.com/python/docs/reference/datastore/latest/google.cloud.datastore.transaction.Transaction)

    Transaction(client, read_only=False, read_time=None, begin_later=False)

An abstraction representing datastore Transactions.

Transactions can be used to build up a bulk mutation and ensure all
or none succeed (transactionally).

For example, the following snippet of code will put the two `save`
operations (either `insert` or `upsert`) into the same
mutation, and execute those within a transaction:

.. testsetup:: txn

    import uuid

    from google.cloud import https://docs.cloud.google.com/python/docs/reference/datastore/latest

    unique = str(uuid.uuid4())[0:8]
    client = https://docs.cloud.google.com/python/docs/reference/datastore/latest.https://docs.cloud.google.com/python/docs/reference/datastore/latest/google.cloud.datastore.client.Client.html(namespace='ns{}'.format(unique))

.. doctest:: txn

    >>> entity1 = datastore.Entity(client.key('EntityKind', 1234))
    >>> entity2 = datastore.Entity(client.key('EntityKind', 2345))
    >>> with client.transaction():
    ...     client.put_multi([entity1, entity2])

Because it derives from xref_Batch,
`Transaction` also provides `put` and `delete` methods:

.. doctest:: txn
> > > with client.transaction() as xact:
> > > ... xact.put(entity1)
> > > ... xact.delete(entity2.key)

By default, the transaction is rolled back if the transaction block
exits with an error:

.. doctest:: txn

    >>> def do_some_work():
    ...    return
    >>> class SomeException(Exception):
    ...    pass
    >>> with client.transaction():
    ...     do_some_work()
    ...     raise SomeException  # rolls back
    Traceback (most recent call last):
      ...
    SomeException

If the transaction block exits without an exception, it will commit
by default.

<br />

> [!WARNING]
> **Warning:** Inside a transaction, automatically assigned IDs for entities will not be available at save time! That means, if you try: .. doctest:: txn
>
>
> > > > with client.transaction():
> > > > ... thing1 = datastore.Entity(key=client.key('Thing'))
> > > > ... client.put(thing1)

Once you exit the transaction (or call `commit`), the automatically generated ID will be assigned to the entity:

<br />

.. doctest:: txn

      >>> with client.transaction():
      ...     thing2 = datastore.Entity(key=client.key('Thing'))
      ...     client.put(thing2)
      ...     print(thing2.key.is_partial)  # There is no ID on this key.
      ...
      True
      >>> print(thing2.key.is_partial)  # There *is* an ID.
      False

If you don't want to use the context manager you can initialize a
transaction manually:

.. doctest:: txn
> > > transaction = client.transaction()
> > > transaction.begin()
> > >
> > > thing3 = datastore.Entity(key=client.key('Thing'))
> > > transaction.put(thing3)
> > >
> > > transaction.commit()

.. testcleanup:: txn

    with client.batch() as batch:
        batch.delete(client.key('EntityKind', 1234))
        batch.delete(client.key('EntityKind', 2345))
        batch.delete(thing1.key)
        batch.delete(thing2.key)
        batch.delete(thing3.key)

| **Parameters** ||
|---|---|
| **Name** | **Description** |
| `client` | `https://docs.cloud.google.com/python/docs/reference/datastore/latest/google.cloud.datastore.client.Client` the client used to connect to datastore. |
| `read_only` | `bool` indicates the transaction is read only. |
| `read_time` | `datetime` (Optional) Time at which the transaction reads entities. Only allowed when `read_only=True`. This feature is in private preview. |
| `begin_later` | `bool` (Optional) If True, the transaction will be started lazily (i.e. when the first RPC is made). If False, the transaction will be started as soon as the context manager is entered. `self.begin()` can also be called manually to begin the transaction at any time. Default is False. |

| **Exceptions** ||
|---|---|
| **Type** | **Description** |
| `` `ValueError `` | if read_time is specified when `read_only=False`. |