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.
Signature:
export declare class PoolingService<T> implements LifeCycleObserver
Implements: LifeCycleObserver
Remarks
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.
Constructors
| Constructor | Modifiers | Description |
|---|---|---|
| [(constructor)(context, options)](/doc/en/lb4/apidocs.pooling.poolingservice._constructor_.html) | Constructs a new instance of the `PoolingService` class |
Properties
| Property | Modifiers | Type | Description |
|---|---|---|---|
| [context](/doc/en/lb4/apidocs.pooling.poolingservice.context.html) | `readonly` | [Context](/doc/en/lb4/apidocs.context.context.html) | |
| [pool](/doc/en/lb4/apidocs.pooling.poolingservice.pool.html) | `readonly` | Pool<T> | The resource pool |
Methods
| Method | Modifiers | Description |
|---|---|---|
| [acquire(requestCtx)](/doc/en/lb4/apidocs.pooling.poolingservice.acquire.html) | Acquire a new instance | |
| [destroy(resource)](/doc/en/lb4/apidocs.pooling.poolingservice.destroy.html) | Destroy a resource from the pool | |
| [release(resource)](/doc/en/lb4/apidocs.pooling.poolingservice.release.html) | Release the resource back to the pool. | |
| [run(task, requestCtx)](/doc/en/lb4/apidocs.pooling.poolingservice.run.html) | 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()](/doc/en/lb4/apidocs.pooling.poolingservice.start.html) | Start the pool | |
| [stop()](/doc/en/lb4/apidocs.pooling.poolingservice.stop.html) | Stop the pool |