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
Name
Description
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
CallOptions

Request configuration options, See CallOptions for more details.

Returns
Type
Description
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
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)

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

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

Parameters
Name
Description
databaseName
string

The parent database's name.

name
string

The instance name.

Returns
Type
Description
string

{string}

Example
  Session 
 . 
 formatName_ 
 ( 
 'my-database' 
 , 
  
 'my-session' 
 ); 
 // 'projects/grape-spaceship-123/instances/my-instance/' + 
 // 'databases/my-database/sessions/my-session' 
 
  getMetadata 
 ( 
 gaxOptions 
 ?: 
  
 CallOptions 
 ) 
 : 
  
 Promise<GetSessionMetadataResponse> 
 ; 
 

Get the session's metadata.

Wrapper around .

Parameter
Name
Description
gaxOptions
CallOptions

Request configuration options, See CallOptions for more details.

Returns
Type
Description
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 
 : 
  
 GetSessionMetadataCallback 
 ) 
 : 
  
 void 
 ; 
 
Parameter
Name
Description
callback
GetSessionMetadataCallback
Returns
Type
Description
void
  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 with SELECT 1 to prevent it from expiring.

Parameter
Name
Description
gaxOptions
CallOptions

Request configuration options, See CallOptions for more details.

Returns
Type
Description
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
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}

Example
  const 
  
 transaction 
  
 = 
  
 session 
 . 
 partitionedDml 
 (); 
 

snapshot(options, queryOptions)

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

Create a Snapshot transaction.

Parameters
Name
Description
options
TimestampBounds

The timestamp bounds.

queryOptions
IQueryOptions

The default query options to use.

Returns
Type
Description

{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
Name
Description
queryOptions
IQueryOptions

The default query options to use. {Transaction}

requestOptions
Pick < IRequestOptions , 'transactionTag'>
Returns
Type
Description
Example
  const 
  
 transaction 
  
 = 
  
 session 
 . 
 transaction 
 (); 
 
Design a Mobile Site
View Site in Mobile | Classic
Share by: