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');constspanner=newSpanner();constinstance=spanner.instance('my-instance');constdatabase=instance.database('my-database');//-// To create a session manually, don't provide a name.//-constsession=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.//-constsession=database.session('session-name');

Constructors

(constructor)(database, name)

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

Constructs a new instance of theSession class

Parameters
NameDescription
databaseDatabase
namestring

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
gaxOptionsCallOptions

Request configuration options, SeeCallOptions 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){constapiResponse=data[0];});

delete(callback)

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

delete(gaxOptions, callback)

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

formatName_(databaseName, name)

staticformatName_(databaseName:string,name:string):string;

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

Parameters
NameDescription
databaseNamestring

The parent database's name.

namestring

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
gaxOptionsCallOptions

Request configuration options, SeeCallOptions 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){constmetadata=data[0];constapiResponse=data[1];});

getMetadata(callback)

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

getMetadata(gaxOptions, callback)

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

keepAlive(gaxOptions)

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

Ping the session withSELECT 1 to prevent it from expiring.

Parameter
NameDescription
gaxOptionsCallOptions

Request configuration options, SeeCallOptions 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
callbackKeepAliveCallback
Returns
TypeDescription
void

keepAlive(gaxOptions, callback)

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

partitionedDml()

partitionedDml():PartitionedDml;

Create a PartitionedDml transaction.

Returns
TypeDescription
PartitionedDml

{PartitionedDml}

Example
consttransaction=session.partitionedDml();

snapshot(options, queryOptions)

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

Create a Snapshot transaction.

Parameters
NameDescription
optionsTimestampBounds

The timestamp bounds.

queryOptionsIQueryOptions

The default query options to use.

Returns
TypeDescription
Snapshot

{Snapshot}

Example
constsnapshot=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
queryOptionsIQueryOptions

The default query options to use. {Transaction}

requestOptionsPick<IRequestOptions, 'transactionTag'>
Returns
TypeDescription
Transaction
Example
consttransaction=session.transaction();

Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2025-10-30 UTC.