Class SessionPool

Class used to manage connections to Spanner.

**You don't need to use this class directly, connections will be handled for you.**

{EventEmitter}

Inheritance

EventEmitter > SessionPool

Package

@google-cloud/spanner!

Constructors

(constructor)(database, options)

  constructor 
 ( 
 database 
 : 
  
 Database 
 , 
  
 options 
 ?: 
  
 SessionPoolOptions 
 ); 
 

Constructs a new instance of the SessionPool class

Parameters
Type Name Description
Database
database

The DB instance.

SessionPoolOptions
options

Configuration options.

Properties

_acquires

  _acquires 
 : 
  
 PQueue 
 ; 
 
Property Value
Type Description
PQueue

_evictHandle

  _evictHandle 
 : 
  
 NodeJS 
 . 
 Timer 
 ; 
 
Property Value
Type Description
NodeJS.Timer

_inventory

  _inventory 
 : 
  
 SessionInventory 
 ; 
 
Property Value
Type Description
SessionInventory

_numInProcessPrepare

  _numInProcessPrepare 
 : 
  
 number 
 ; 
 
Property Value
Type Description
number

_onClose

  _onClose 
 : 
  
 Promise<void> 
 ; 
 
Property Value
Type Description
Promise <void>

_pending

  _pending 
 : 
  
 number 
 ; 
 
Property Value
Type Description
number

_pendingPrepare

  _pendingPrepare 
 : 
  
 number 
 ; 
 
Property Value
Type Description
number

_pingHandle

  _pingHandle 
 : 
  
 NodeJS 
 . 
 Timer 
 ; 
 
Property Value
Type Description
NodeJS.Timer

_requests

  _requests 
 : 
  
 PQueue 
 ; 
 
Property Value
Type Description
PQueue

_traces

  _traces 
 : 
  
 Map<string 
 , 
  
 trace 
 . 
 StackFrame 
 []>; 
 
Property Value
Type Description
Map <string, trace. StackFrame []>

_waiters

  _waiters 
 : 
  
 Waiters 
 ; 
 
Property Value
Type Description
Waiters

available

  get 
  
 available 
 () 
 : 
  
 number 
 ; 
 

Total number of available sessions. {number}

Property Value
Type Description
number

borrowed

  get 
  
 borrowed 
 () 
 : 
  
 number 
 ; 
 

Total number of borrowed sessions. {number}

Property Value
Type Description
number

currentWriteFraction

  get 
  
 currentWriteFraction 
 () 
 : 
  
 number 
 ; 
 

Current fraction of write-prepared sessions in the pool. {number}

Property Value
Type Description
number

database

  database 
 : 
  
 Database 
 ; 
 
Property Value
Type Description
Database

isFull

  get 
  
 isFull 
 () 
 : 
  
 boolean 
 ; 
 

Flag to determine if Pool is full. {boolean}

Property Value
Type Description
boolean

isOpen

  isOpen 
 : 
  
 boolean 
 ; 
 
Property Value
Type Description
boolean

numReadWaiters

  get 
  
 numReadWaiters 
 () 
 : 
  
 number 
 ; 
 

Current number of waiters for a read-only session. {number}

Property Value
Type Description
number

numWriteWaiters

  get 
  
 numWriteWaiters 
 () 
 : 
  
 number 
 ; 
 

Current number of waiters for a read/write session. {number}

Property Value
Type Description
number

options

  options 
 : 
  
 SessionPoolOptions 
 ; 
 
Property Value
Type Description
SessionPoolOptions

pendingPrepare

  get 
  
 pendingPrepare 
 () 
 : 
  
 number 
 ; 
 

Number of sessions currently being prepared for a read/write transaction before being released into the pool. This number does not include the number of sessions being prepared for a read/write transaction that have already been checked out of the pool. {number}

Property Value
Type Description
number

reads

  get 
  
 reads 
 () 
 : 
  
 number 
 ; 
 

Total number of read sessions. {number}

Property Value
Type Description
number

size

  get 
  
 size 
 () 
 : 
  
 number 
 ; 
 

Total size of pool. {number}

Property Value
Type Description
number

totalPending

  get 
  
 totalPending 
 () 
 : 
  
 number 
 ; 
 

Number of sessions being created or prepared for a read/write transaction. {number}

Property Value
Type Description
number

totalWaiters

  get 
  
 totalWaiters 
 () 
 : 
  
 number 
 ; 
 

Sum of read and write waiters. {number}

Property Value
Type Description
number

writes

  get 
  
 writes 
 () 
 : 
  
 number 
 ; 
 

Total number of write sessions. {number}

Property Value
Type Description
number

Methods

_acquire(type)

  _acquire 
 ( 
 type 
 : 
  
 types 
 ) 
 : 
  
 Promise<Session> 
 ; 
 

Attempts to borrow a session from the pool.

Parameter
Type Name Description
types
type

The desired type to borrow.

Returns
Type Description
Promise < Session >

{Promise

_borrow(session)

  _borrow 
 ( 
 session 
 : 
  
 Session 
 ) 
 : 
  
 void 
 ; 
 

Moves a session into the borrowed group.

Parameter
Type Name Description
session

The session object.

Returns
Type Description
void

_borrowFrom(type)

  _borrowFrom 
 ( 
 type 
 : 
  
 types 
 ) 
 : 
  
 Session 
 ; 
 

Borrows the first session from specific group. This method may only be called if the inventory actually contains a session of the desired type.

Parameter
Type Name Description
types
type

The desired session type. {Session}

Returns
Type Description
Session

_borrowNextAvailableSession(type)

  _borrowNextAvailableSession 
 ( 
 type 
 : 
  
 types 
 ) 
 : 
  
 Session 
 ; 
 

Grabs the next available session.

Parameter
Type Name Description
types
type

The desired session type.

Returns
Type Description
Session

{Promise

_createSession(type)

  _createSession 
 ( 
 type 
 : 
  
 types 
 ) 
 : 
  
 Promise<void> 
 ; 
 

Attempts to create a single session of a certain type.

Parameter
Type Name Description
types
type

The desired type to create.

Returns
Type Description
Promise <void>

{Promise}

_createSessions({ reads, writes, })

  _createSessions 
 ({ 
  
 reads 
 , 
  
 writes 
 , 
  
 } 
 : 
  
 CreateSessionsOptions 
 ) 
 : 
  
 Promise<void> 
 ; 
 

Batch creates sessions and prepares any necessary transactions.

Parameter
Type Name Description
CreateSessionsOptions
{ reads, writes, }
Returns
Type Description
Promise <void>

{Promise} SessionPool#createError

_destroy(session)

  _destroy 
 ( 
 session 
 : 
  
 Session 
 ) 
 : 
  
 Promise<void> 
 ; 
 

Attempts to delete a session, optionally creating a new one of the same type if the pool is still open and we're under the configured min value.

SessoinPool#error

Parameter
Type Name Description
session

The session to delete.

Returns
Type Description
Promise <void>

{Promise}

_evictIdleSessions()

  _evictIdleSessions 
 () 
 : 
  
 void 
 ; 
 

Deletes idle sessions that exceed the maxIdle configuration.

Returns
Type Description
void

_fill()

  _fill 
 () 
 : 
  
 Promise<void> 
 ; 
 

Fills the pool with the minimum number of sessions.

{Promise}

Returns
Type Description
Promise <void>

_getIdleSessions()

  _getIdleSessions 
 () 
 : 
  
 Session 
 []; 
 

Retrieves a list of all the idle sessions.

Returns
Type Description
Session []

{Session[]}

_getLeaks()

  _getLeaks 
 () 
 : 
  
 string 
 []; 
 

Returns stack traces for sessions that have not been released.

{string[]}

Returns
Type Description
string[]

_getSession(type, startTime)

  _getSession 
 ( 
 type 
 : 
  
 types 
 , 
  
 startTime 
 : 
  
 number 
 ) 
 : 
  
 Promise<Session> 
 ; 
 

Attempts to get a session of a specific type. If the type is unavailable it may try to use a different type.

Parameters
Type Name Description
types
type

The desired session type.

number
startTime

Timestamp to use when determining timeouts.

Returns
Type Description
Promise < Session >

{Promise

_hasSessionUsableFor(type)

  _hasSessionUsableFor 
 ( 
 type 
 : 
  
 types 
 ) 
 : 
  
 boolean 
 ; 
 

Returns true if the pool has a session that is usable for the specified type, i.e. if a read-only session is requested, it returns true if the pool has a read-only or a read/write session. If a read/write session is requested, the method only returns true if the pool has a read/write session available.

Parameter
Type Name Description
types
type

The type of session.

Returns
Type Description
boolean

_isValidSession(session)

  _isValidSession 
 ( 
 session 
 : 
  
 Session 
 ) 
 : 
  
 boolean 
 ; 
 

Checks to see whether or not session is expired.

Parameter
Type Name Description
session

The session to check.

Returns
Type Description
boolean

{boolean}

_ping(session)

  _ping 
 ( 
 session 
 : 
  
 Session 
 ) 
 : 
  
 Promise<void> 
 ; 
 

Pings an individual session.

Parameter
Type Name Description
session

The session to ping.

Returns
Type Description
Promise <void>

{Promise}

_pingIdleSessions()

  _pingIdleSessions 
 () 
 : 
  
 Promise<void> 
 ; 
 

Makes a keep alive request to all the idle sessions.

Returns
Type Description
Promise <void>

{Promise}

_prepareTransaction(session)

  _prepareTransaction 
 ( 
 session 
 : 
  
 Session 
 ) 
 : 
  
 Promise<void> 
 ; 
 

Creates a transaction for a session.

Parameter
Type Name Description
session

The session object.

Returns
Type Description
Promise <void>

{Promise}

_release(session)

  _release 
 ( 
 session 
 : 
  
 Session 
 ) 
 : 
  
 void 
 ; 
 

Releases a session back into the pool.

SessionPool#available SessionPool#readonly-available SessionPool#readwrite-available

Parameter
Type Name Description
session

The session object.

Returns
Type Description
void

_startHouseKeeping()

  _startHouseKeeping 
 () 
 : 
  
 void 
 ; 
 

Starts housekeeping (pinging/evicting) of idle sessions.

Returns
Type Description
void

_stopHouseKeeping()

  _stopHouseKeeping 
 () 
 : 
  
 void 
 ; 
 

Stops housekeeping.

Returns
Type Description
void

close(callback)

  close 
 ( 
 callback 
 : 
  
 SessionPoolCloseCallback 
 ) 
 : 
  
 void 
 ; 
 

Closes and the pool.

SessionPool#close

Parameter
Type Name Description
SessionPoolCloseCallback
callback

The callback function.

Returns
Type Description
void

formatTrace(frames)

  static 
  
 formatTrace 
 ( 
 frames 
 : 
  
 trace 
 . 
 StackFrame 
 []) 
 : 
  
 string 
 ; 
 

Formats stack trace objects into Node-like stack trace.

Parameter
Type Name Description
trace. StackFrame []
frames
Returns
Type Description
string

getReadSession(callback)

  getReadSession 
 ( 
 callback 
 : 
  
 GetReadSessionCallback 
 ) 
 : 
  
 void 
 ; 
 

Retrieve a read session.

Parameter
Type Name Description
GetReadSessionCallback
callback

The callback function.

Returns
Type Description
void

getWriteSession(callback)

  getWriteSession 
 ( 
 callback 
 : 
  
 GetWriteSessionCallback 
 ) 
 : 
  
 void 
 ; 
 

Retrieve a read/write session.

Parameter
Type Name Description
GetWriteSessionCallback
callback

The callback function.

Returns
Type Description
void

open()

  open 
 () 
 : 
  
 void 
 ; 
 

Opens the pool, filling it to the configured number of read and write sessions.

SessionPool#open {Promise}

Returns
Type Description
void

release(session)

  release 
 ( 
 session 
 : 
  
 Session 
 ) 
 : 
  
 void 
 ; 
 

Releases session back into the pool. If the session is a write session it will also prepare a new transaction before releasing it.

Parameter
Type Name Description
session

The session to release.

Returns
Type Description
void
Design a Mobile Site
View Site in Mobile | Classic
Share by: