Class SpannerTransaction
Represents a SQL transaction to be made in a Spanner database. A transaction in Cloud Spanner is a set of reads and writes that execute atomically at a single logical point in time across columns, rows, and tables in a database.
Inherited Members
Namespace: Google.Cloud.Spanner.Data
Assembly: Google.Cloud.Spanner.Data.dll
Syntax
public sealed class SpannerTransaction : SpannerTransactionBase, IDbTransaction, IDisposable, IAsyncDisposable
Properties
CommitPriority
The RPC priority to use for the commit RPC of this transaction. This can only be set for read/write transactions. This priority is not used for commands that are executed on this transaction. Use Priority to set the priority of commands. The default priority is Unspecified.
Declaration
public Priority CommitPriority { get; set; }
Property Value
Type | Description |
---|---|
Priority |
CommitTimeout
Gets or sets the wait time before terminating the attempt to Commit() or Rollback() and generating an error. Defaults to the timeout from the connection string. A value of '0' normally indicates that no timeout should be used (it waits an infinite amount of time). However, if you specify AllowImmediateTimeouts=true in the connection string, '0' will cause a timeout that expires immediately. This is normally used only for testing purposes.
Declaration
public int CommitTimeout { get; set; }
Property Value
Type | Description |
---|---|
int |
DbConnection
Specifies the DbConnection object associated with the transaction.
Declaration
protected override DbConnection DbConnection { get; }
Property Value
Type | Description |
---|---|
DbConnection | The DbConnection object associated with the transaction. |
Overrides
DisposeBehavior
Specifies how resources are treated when Dispose(bool) is called. Defaults to Default. For a pooled transaction, Default will cause transactional resources to be sent back into a shared pool for re-use. For a detached transaction the default behaviour is to do nothing with transactional resources. If set to CloseResources all transactional resource will be closed. A detached transaction must have one process choose CloseResources to avoid leaks of transactional resources.
Declaration
public DisposeBehavior DisposeBehavior { get; set; }
Property Value
Type | Description |
---|---|
DisposeBehavior |
IsDetached
Whether this transaction is detached or not. A detached transaction's resources are not pooled, so the transaction may be shared across processes for instance, for partitioned reads.
Declaration
public bool IsDetached { get; }
Property Value
Type | Description |
---|---|
bool |
IsolationLevel
Specifies the IsolationLevel for this transaction.
Declaration
public override IsolationLevel IsolationLevel { get; }
Property Value
Type | Description |
---|---|
IsolationLevel | The IsolationLevel for this transaction. |
Overrides
LogCommitStats
Specifies whether this transaction should request commit statistics from the backend and log these. This property is by default equal to the value set on the SpannerConnection of this transaction, but can be overridden for a specific transaction.
Declaration
public bool LogCommitStats { get; set; }
Property Value
Type | Description |
---|---|
bool |
MaxCommitDelay
The maximum amount of time the commit may be delayed server side for batching with other commits. The bigger the delay, the better the throughput (QPS), but at the expense of commit latency. If set to Zero, commit batching is disabled. May be null, in which case commits will continue to be batched as they had been before this configuration option was made available to Spanner API consumers. May be set to any value between Zero and 500ms.
Declaration
public TimeSpan? MaxCommitDelay { get; set; }
Property Value
Type | Description |
---|---|
TimeSpan? |
Mode
Indicates the TransactionMode for the transaction.
Declaration
public TransactionMode Mode { get; }
Property Value
Type | Description |
---|---|
TransactionMode |
Remarks
Cloud Spanner supports two transaction modes:
- Locking read-write transactions are the only transaction type that support writing data into Cloud Spanner. These transactions rely on pessimistic locking and, if necessary, two-phase commit. Locking read-write transactions may abort, requiring the application to retry.
- Read-only transactions provide guaranteed consistency across several reads, but do not allow writes. Read-only transactions can be configured to read at timestamps in the past. Read-only transactions do not need to be committed and do not take locks.
ReadTimestamp
The read timestamp of the read-only transaction if
ReturnReadTimestamp is true, else null
.
Declaration
public Timestamp ReadTimestamp { get; }
Property Value
Type | Description |
---|---|
Timestamp |
Tag
The transaction tag to use for this transaction. This can only be set for read/write transactions, and must be set before any statements are executed on the transaction.
Declaration
public string Tag { get; set; }
Property Value
Type | Description |
---|---|
string |
TimestampBound
Tells Cloud Spanner how to choose a timestamp at which to read the data for read-only transactions.
Declaration
public TimestampBound TimestampBound { get; }
Property Value
Type | Description |
---|---|
TimestampBound |
Remarks
The types of timestamp bounds are:
- Strong (the default): read the latest data.
- Bounded staleness: read a version of the data that's no staler than a bound.
- Exact staleness: read the version of the data at an exact timestamp.
TransactionId
Identifying information about this transaction.
Declaration
public TransactionId TransactionId { get; }
Property Value
Type | Description |
---|---|
TransactionId |
Methods
Commit()
Commits the database transaction.
Declaration
public override void Commit()
Overrides
Commit(out DateTime)
Commits the database transaction synchronously, returning the database timestamp for the commit via timestamp
.
Declaration
public void Commit(out DateTime timestamp)
Parameters
Type | Name | Description |
---|---|---|
DateTime | timestamp | Returns the UTC timestamp when the data was written to the database. |
CommitAsync(CancellationToken)
Commits the database transaction asynchronously, returning the commit timestamp.
Declaration
public Task<DateTime> CommitAsync(CancellationToken cancellationToken = default)
Parameters
Type | Name | Description |
---|---|---|
CancellationToken | cancellationToken | A cancellation token used for this task. |
Returns
Type | Description |
---|---|
Task<DateTime> | Returns the UTC timestamp when the data was written to the database. |
CreateBatchDmlCommand()
Creates a new SpannerBatchCommand to execute batched DML statements within this transaction. You can add commands to the batch by using Add(SpannerCommand), Add(SpannerCommandTextBuilder, SpannerParameterCollection) and Add(string, SpannerParameterCollection).
Declaration
public SpannerBatchCommand CreateBatchDmlCommand()
Returns
Type | Description |
---|---|
SpannerBatchCommand |
Dispose(bool)
Releases the unmanaged resources used by the DbTransaction and optionally releases the managed resources.
Declaration
protected override void Dispose(bool disposing)
Parameters
Type | Name | Description |
---|---|---|
bool | disposing | If true, this method releases all resources held by any managed objects that this DbTransaction references. |
Overrides
Rollback()
Rolls back a transaction from a pending state.
Declaration
public override void Rollback()
Overrides
RollbackAsync(CancellationToken)
Rolls back a transaction asynchronously.
Declaration
public override Task RollbackAsync(CancellationToken cancellationToken = default)
Parameters
Type | Name | Description |
---|---|---|
CancellationToken | cancellationToken | A cancellation token used for this task. |
Returns
Type | Description |
---|---|
Task |