Class Session (7.5.0) Stay organized with collections Save and categorize content based on your preferences.
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 >SessionPackage
@google-cloud/spannerExample
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 | |
|---|---|
| Name | Description |
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 | |
|---|---|
| Name | Description |
gaxOptions | CallOptionsRequest configuration options, SeeCallOptions for more details. |
| Returns | |
|---|---|
| Type | Description |
Promise<DeleteSessionResponse> | {Promise |
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 | |
|---|---|
| Name | Description |
callback | DeleteSessionCallback |
| Returns | |
|---|---|
| Type | Description |
void | |
delete(gaxOptions, callback)
delete(gaxOptions:CallOptions,callback:DeleteSessionCallback):void;| Parameters | |
|---|---|
| Name | Description |
gaxOptions | CallOptions |
callback | DeleteSessionCallback |
| Returns | |
|---|---|
| Type | Description |
void | |
formatName_(databaseName, name)
staticformatName_(databaseName:string,name:string):string;Format the session name to include the parent database's name.
| Parameters | |
|---|---|
| Name | Description |
databaseName | stringThe parent database's name. |
name | stringThe instance name. |
| Returns | |
|---|---|
| Type | Description |
string | {string} |
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 | |
|---|---|
| Name | Description |
gaxOptions | CallOptionsRequest configuration options, SeeCallOptions for more details. |
| Returns | |
|---|---|
| Type | Description |
Promise<GetSessionMetadataResponse> | {Promise |
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 | |
|---|---|
| Name | Description |
callback | GetSessionMetadataCallback |
| Returns | |
|---|---|
| Type | Description |
void | |
getMetadata(gaxOptions, callback)
getMetadata(gaxOptions:CallOptions,callback:GetSessionMetadataCallback):void;| Parameters | |
|---|---|
| Name | Description |
gaxOptions | CallOptions |
callback | GetSessionMetadataCallback |
| Returns | |
|---|---|
| Type | Description |
void | |
keepAlive(gaxOptions)
keepAlive(gaxOptions?:CallOptions):Promise<KeepAliveResponse>;Ping the session withSELECT 1 to prevent it from expiring.
| Parameter | |
|---|---|
| Name | Description |
gaxOptions | CallOptionsRequest configuration options, SeeCallOptions for more details. |
| Returns | |
|---|---|
| Type | Description |
Promise<KeepAliveResponse> | {Promise |
session.keepAlive(function(err){if(err){// An error occurred while trying to keep this session alive.}});keepAlive(callback)
keepAlive(callback:KeepAliveCallback):void;| Parameter | |
|---|---|
| Name | Description |
callback | KeepAliveCallback |
| Returns | |
|---|---|
| Type | Description |
void | |
keepAlive(gaxOptions, callback)
keepAlive(gaxOptions:CallOptions,callback:KeepAliveCallback):void;| Parameters | |
|---|---|
| Name | Description |
gaxOptions | CallOptions |
callback | KeepAliveCallback |
| Returns | |
|---|---|
| Type | Description |
void | |
partitionedDml()
partitionedDml():PartitionedDml;Create a PartitionedDml transaction.
| Returns | |
|---|---|
| Type | Description |
PartitionedDml | {PartitionedDml} |
consttransaction=session.partitionedDml();snapshot(options, queryOptions)
snapshot(options?:TimestampBounds,queryOptions?:google.spanner.v1.ExecuteSqlRequest.IQueryOptions):Snapshot;Create a Snapshot transaction.
| Parameters | |
|---|---|
| Name | Description |
options | TimestampBoundsThe timestamp bounds. |
queryOptions | IQueryOptionsThe default query options to use. |
| Returns | |
|---|---|
| Type | Description |
Snapshot | {Snapshot} |
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 | |
|---|---|
| Name | Description |
queryOptions | IQueryOptionsThe default query options to use. {Transaction} |
requestOptions | Pick<IRequestOptions, 'transactionTag'> |
| Returns | |
|---|---|
| Type | Description |
Transaction | |
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.