Advanced Session Pool Topics

Custom Session Pool Implementations

You can supply your own pool implementation, which must satisfy thecontract laid out inAbstractSessionPool:

from google.cloud.spanner importAbstractSessionPoolclass MyCustomPool(AbstractSessionPool):     def __init__(self, custom_param):         super(MyCustomPool, self).__init__()         self.custom_param = custom_param     def bind(self, database):         ...     def get(self, read_only=False):         ...     def put(self, session, discard_if_full=True):         ...pool = MyCustomPool(custom_param=42)database = instance.database(DATABASE_NAME, pool=pool)

Lowering latency for read / query operations

Some applications may need to minimize latency for read operations, includingparticularly the overhead of making an API request to create or refresh asession.PingingPool is designed for suchapplications, which need to configure a background thread to do the work ofkeeping the sessions fresh.

Create an instance ofPingingPool:

from google.cloud.spanner importClient,PingingPoolclient = Client()instance =client.instance(INSTANCE_NAME)pool = PingingPool(size=10, default_timeout=5, ping_interval=300)database = instance.database(DATABASE_NAME, pool=pool)

Set up a background thread to ping the pool’s session, keeping themfrom becoming stale:

import threadingdef background_loop():   while True:      # (Optional) Perform other background tasks here      pool.ping()background = threading.Thread(target=background_loop, name='ping-pool')background.daemon = Truebackground.start()

Lowering latency for mixed read-write operations

Some applications may need to minimize latency for read write operations,including particularly the overhead of making an API request to create orrefresh a session or to begin a session’s transaction.TransactionPingingPool is designed forsuch applications, which need to configure a background thread to do the workof keeping the sessions fresh and starting their transactions after use.

Create an instance ofTransactionPingingPool:

from google.cloud.spanner importClient,TransactionPingingPoolclient = Client()instance =client.instance(INSTANCE_NAME)pool = TransactionPingingPool(size=10, default_timeout=5, ping_interval=300)database = instance.database(DATABASE_NAME, pool=pool)

Set up a background thread to ping the pool’s session, keeping themfrom becoming stale, and ensuring that each session has a new transactionstarted before it is used:

import threadingdef background_loop():   while True:      # (Optional) Perform other background tasks here      pool.ping()      pool.begin_pending_transactions()background = threading.Thread(target=background_loop, name='ping-pool')background.daemon = Truebackground.start()

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-07-18 UTC.