Class Session (7.5.0)

Create a Session object to interact with a Cloud Spanner session.

**It is unlikely you will need to interact with sessions directly. By default, sessions are created and utilized for maximum performance automatically.**

Inheritance

common_3.GrpcServiceObject > Session

Package

@google-cloud/spanner

Example


const {Spanner} = require('@google-cloud/spanner');
const spanner = new Spanner();

const instance = spanner.instance('my-instance');
const database = instance.database('my-database');

//-
// To create a session manually, don't provide a name.
//-
const session = database.session();

session.create(function(err) {
  if (err) {
    // Error handling omitted.
  }

  // Session created successfully.
  // `session.id` = The name of the session.
});

//-
// To access a previously-created session, provide a name.
//-
const session = database.session('session-name');

Constructors

(constructor)(database, name)

constructor(database: Database, name?: string);

Constructs a new instance of the Session class

Parameters
NameDescription
database Database
name string

Properties

formattedName_

formattedName_?: string;

id

id: string;

lastError

lastError?: grpc.ServiceError;

lastUsed

lastUsed?: number;

resourceHeader_

resourceHeader_: {
        [k: string]: string;
    };

txn

txn?: Transaction;

Methods

delete(gaxOptions)

delete(gaxOptions?: CallOptions): Promise<DeleteSessionResponse>;

Delete a session.

Wrapper around .

Parameter
NameDescription
gaxOptions CallOptions

Request configuration options, See CallOptions for more details.

Returns
TypeDescription
Promise<DeleteSessionResponse>

{Promise

Example

session.delete(function(err, apiResponse) {
  if (err) {
    // Error handling omitted.
  }

  // Session deleted successfully.
});

//-
//Returns a Promise if the callback is omitted.
//-
session.delete().then(function(data) {
  const apiResponse = data[0];
});

delete(callback)

delete(callback: DeleteSessionCallback): void;
Parameter
NameDescription
callback DeleteSessionCallback
Returns
TypeDescription
void

delete(gaxOptions, callback)

delete(gaxOptions: CallOptions, callback: DeleteSessionCallback): void;
Parameters
NameDescription
gaxOptions CallOptions
callback DeleteSessionCallback
Returns
TypeDescription
void

formatName_(databaseName, name)

static formatName_(databaseName: string, name: string): string;

Format the session name to include the parent database's name.

Parameters
NameDescription
databaseName string

The parent database's name.

name string

The instance name.

Returns
TypeDescription
string

{string}

Example

Session.formatName_('my-database', 'my-session');
// 'projects/grape-spaceship-123/instances/my-instance/' +
// 'databases/my-database/sessions/my-session'

getMetadata(gaxOptions)

getMetadata(gaxOptions?: CallOptions): Promise<GetSessionMetadataResponse>;

Get the session's metadata.

Wrapper around .

Parameter
NameDescription
gaxOptions CallOptions

Request configuration options, See CallOptions for more details.

Returns
TypeDescription
Promise<GetSessionMetadataResponse>

{Promise

Example

session.getMetadata(function(err, metadata, apiResponse) {});

//-
//Returns a Promise if the callback is omitted.
//-
session.getMetadata().then(function(data) {
  const metadata = data[0];
  const apiResponse = data[1];
});

getMetadata(callback)

getMetadata(callback: GetSessionMetadataCallback): void;
Parameter
NameDescription
callback GetSessionMetadataCallback
Returns
TypeDescription
void

getMetadata(gaxOptions, callback)

getMetadata(gaxOptions: CallOptions, callback: GetSessionMetadataCallback): void;
Parameters
NameDescription
gaxOptions CallOptions
callback GetSessionMetadataCallback
Returns
TypeDescription
void

keepAlive(gaxOptions)

keepAlive(gaxOptions?: CallOptions): Promise<KeepAliveResponse>;

Ping the session with SELECT 1 to prevent it from expiring.

Parameter
NameDescription
gaxOptions CallOptions

Request configuration options, See CallOptions for more details.

Returns
TypeDescription
Promise<KeepAliveResponse>

{Promise

Example

session.keepAlive(function(err) {
  if (err) {
    // An error occurred while trying to keep this session alive.
  }
});

keepAlive(callback)

keepAlive(callback: KeepAliveCallback): void;
Parameter
NameDescription
callback KeepAliveCallback
Returns
TypeDescription
void

keepAlive(gaxOptions, callback)

keepAlive(gaxOptions: CallOptions, callback: KeepAliveCallback): void;
Parameters
NameDescription
gaxOptions CallOptions
callback KeepAliveCallback
Returns
TypeDescription
void

partitionedDml()

partitionedDml(): PartitionedDml;

Create a PartitionedDml transaction.

Returns
TypeDescription
PartitionedDml

{PartitionedDml}

Example

const transaction = session.partitionedDml();

snapshot(options, queryOptions)

snapshot(options?: TimestampBounds, queryOptions?: google.spanner.v1.ExecuteSqlRequest.IQueryOptions): Snapshot;

Create a Snapshot transaction.

Parameters
NameDescription
options TimestampBounds

The timestamp bounds.

queryOptions IQueryOptions

The default query options to use.

Returns
TypeDescription
Snapshot

{Snapshot}

Example

const snapshot = session.snapshot({strong: false});

transaction(queryOptions, requestOptions)

transaction(queryOptions?: google.spanner.v1.ExecuteSqlRequest.IQueryOptions, requestOptions?: Pick<IRequestOptions, 'transactionTag'>): Transaction;

Create a read write Transaction.

Parameters
NameDescription
queryOptions IQueryOptions

The default query options to use. {Transaction}

requestOptions Pick<IRequestOptions, 'transactionTag'>
Returns
TypeDescription
Transaction
Example

const transaction = session.transaction();