Page Contents

Home > @loopback/pooling > PoolingService

PoolingService class

A singleton service to maintain a pool of resources. This pool service can be bound to different keys to represent multiple pools. Each binding is a singleton so that the state stays the same for injections into multiple instances for other artifacts.


export declare class PoolingService<T> implements LifeCycleObserver 

Implements: LifeCycleObserver


Some resources can be expensive to create/start. For example, a datasource has overhead to connect to the database. There will be performance penalty to use TRANSIENT scope and creates a new instance per request. But it is not feasible to be a singleton for some use cases, for example, each request may have different security contexts.

The pool service observes life cycle events to start and stop.


Constructor Modifiers Description
(constructor)(context, options)   Constructs a new instance of the PoolingService class


Property Modifiers Type Description
context readonly Context  
pool readonly Pool<T> The resource pool


Method Modifiers Description
acquire(requestCtx)   Acquire a new instance
destroy(resource)   Destroy a resource from the pool
release(resource)   Release the resource back to the pool.
run(task, requestCtx)   Run the task with an acquired resource from the pool. If task is completed successfully, the resource is returned to the pool. Otherwise, the resource is destroyed.
start()   Start the pool
stop()   Stop the pool