Class Transaction

This type of transaction is the only way to write 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.

Calling either or signals that the transaction is finished and no further requests will be made. If for some reason you decide not to call one of the aformentioned methods, call to release the underlying Session.

Running a transaction via or automatically re-runs the transaction on ABORTED errors.

returns a plain Transaction object, requiring the user to retry manually.

Snapshot

Inheritance

Dml > Transaction

Package

@google-cloud/spanner!

Constructors

(constructor)(session, options, queryOptions)

constructor(session: Session, options?: spannerClient.spanner.v1.TransactionOptions.ReadWrite, queryOptions?: IQueryOptions);

Execute a DML statement and get the affected row count.

Transaction#runUpdate

Parameters
TypeNameDescription
Sessionsession
google.spanner.v1.TransactionOptions.ReadWriteoptions
IQueryOptionsqueryOptions

Properties

commitTimestamp

commitTimestamp?: PreciseDate;
Property Value
TypeDescription
PreciseDate

commitTimestampProto

commitTimestampProto?: spannerClient.protobuf.ITimestamp;
Property Value
TypeDescription
spannerClient.protobuf.ITimestamp

Methods

batchUpdate(queries, options)

batchUpdate(queries: Array<string | Statement>, options?: BatchUpdateOptions | CallOptions): Promise<BatchUpdateResponse>;
Parameters
TypeNameDescription
Array<string | Statement>queries
BatchUpdateOptions | CallOptionsoptions
Returns
TypeDescription
Promise<BatchUpdateResponse>

batchUpdate(queries, callback)

batchUpdate(queries: Array<string | Statement>, callback: BatchUpdateCallback): void;
Parameters
TypeNameDescription
Array<string | Statement>queries
BatchUpdateCallbackcallback
Returns
TypeDescription
void

batchUpdate(queries, options, callback)

batchUpdate(queries: Array<string | Statement>, options: BatchUpdateOptions | CallOptions, callback: BatchUpdateCallback): void;
Parameters
TypeNameDescription
Array<string | Statement>queries
BatchUpdateOptions | CallOptionsoptions
BatchUpdateCallbackcallback
Returns
TypeDescription
void

commit(options)

commit(options?: CommitOptions | CallOptions): Promise<CommitResponse>;
Parameter
TypeNameDescription
CommitOptions | CallOptionsoptions
Returns
TypeDescription
Promise<CommitResponse>

commit(callback)

commit(callback: CommitCallback): void;
Parameter
TypeNameDescription
CommitCallbackcallback
Returns
TypeDescription
void

commit(options, callback)

commit(options: CommitOptions | CallOptions, callback: CommitCallback): void;
Parameters
TypeNameDescription
CommitOptions | CallOptionsoptions
CommitCallbackcallback
Returns
TypeDescription
void

deleteRows(table, keys)

deleteRows(table: string, keys: Key[]): void;

Delete rows from a table.

[Commit API Documentation](https://cloud.google.com/spanner/docs/reference/rpc/google.spanner.v1#google.spanner.v1.Spanner.Commit)

Parameters
TypeNameDescription
stringtable

The name of the table.

Key[]keys

The keys for the rows to delete. If using a composite key, provide an array within this array. See the example below.

Returns
TypeDescription
void

getUniqueKeys(rows)

static getUniqueKeys(rows: object[]): string[];

Takes a list of rows and returns all unique column names.

Parameter
TypeNameDescription
object[]rows

The rows.

Returns
TypeDescription
string[]

{string[]}

insert(table, rows)

insert(table: string, rows: object | object[]): void;

Insert rows of data into this table.

[Commit API Documentation](https://cloud.google.com/spanner/docs/reference/rpc/google.spanner.v1#google.spanner.v1.Spanner.Commit)

Parameters
TypeNameDescription
stringtable

The name of the table.

object | object[]rows

A map of names to values of data to insert into this table.

Returns
TypeDescription
void

replace(table, rows)

replace(table: string, rows: object | object[]): void;

Replace rows of data within a table.

[Commit API Documentation](https://cloud.google.com/spanner/docs/reference/rpc/google.spanner.v1#google.spanner.v1.Spanner.Commit)

Parameters
TypeNameDescription
stringtable

The table to read from.

object | object[]rows

A map of names to values of data to insert into this table.

Returns
TypeDescription
void

rollback(gaxOptions)

rollback(gaxOptions?: CallOptions): Promise<void>;
Parameter
TypeNameDescription
CallOptionsgaxOptions
Returns
TypeDescription
Promise<void>

rollback(callback)

rollback(callback: spannerClient.spanner.v1.Spanner.RollbackCallback): void;
Parameter
TypeNameDescription
google.spanner.v1.Spanner.RollbackCallbackcallback
Returns
TypeDescription
void

rollback(gaxOptions, callback)

rollback(gaxOptions: CallOptions, callback: spannerClient.spanner.v1.Spanner.RollbackCallback): void;
Parameters
TypeNameDescription
CallOptionsgaxOptions
google.spanner.v1.Spanner.RollbackCallbackcallback
Returns
TypeDescription
void

update(table, rows)

update(table: string, rows: object | object[]): void;

Update rows of data within a table.

[Commit API Documentation](https://cloud.google.com/spanner/docs/reference/rpc/google.spanner.v1#google.spanner.v1.Spanner.Commit)

Parameters
TypeNameDescription
stringtable

The table to read from.

object | object[]rows

A map of names to values of data to insert into this table.

Returns
TypeDescription
void

upsert(table, rows)

upsert(table: string, rows: object | object[]): void;

Insert or update rows of data within a table.

[Commit API Documentation](https://cloud.google.com/spanner/docs/reference/rpc/google.spanner.v1#google.spanner.v1.Spanner.Commit)

Parameters
TypeNameDescription
stringtable

The table to read from.

object | object[]rows

A map of names to values of data to insert into this table.

Returns
TypeDescription
void