R
R
Raey2021-10-04 21:05:52
PostgreSQL
Raey, 2021-10-04 21:05:52

Why is DB Connections in RDS always 0?

Please explain why DB Connections in RDS is always 0. I perform operations with the database (GET, POST) - it always writes 0, although a connection to the database is created, the operation goes through and then connection.close() occurs (And you can still connect for 10 minutes will join, and after another operation a new one will be opened)
If I go to pgadmin, then after a while several DB Connections (2-4) will be shown.
Shouldn't every request to the database generate a connection that will be displayed in the monitoring? Or I do not quite understand how these connections work?

615b430f249ea139462845.png

Code examples:

DB connection
export class DB {
  private connectionManager: ConnectionManager;

  constructor() {
    this.connectionManager = getConnectionManager();
  }

  public async getConnection(): Promise<Connection> {
    const CONNECTION_NAME = `default`;

    let connection: Connection;

    if (this.connectionManager.has(CONNECTION_NAME)) {
      connection = this.connectionManager.get(CONNECTION_NAME);

      if (!connection.isConnected) {
        connection = await connection.connect();
      }
    } else {
      connection = await createConnection(options);
    }

    return connection;
  }
}


Service Example
class ItemService {
  private db: DB;
  constructor() {
    this.db = new DB();
  }
  async getItems(): Promise<Item[]> {
    const connection = await this.db.getConnection();
    try {
      return await connection
        .getRepository(ItemEntity)
        .find();
    } finally {
      await connection.close();
    }
  }
}
export const itemService = new ItemService()


Lambda
export const getItems = async (_event) => {
  try {
    const items = await itemsService.getItems();
    return sendResponse( { items }, 200 );
  } catch (error) {
    return sendError(error);
  }
};

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vitaly Karasik, 2021-10-05
@Raey

I think because the connections are short, and when the monitor checks, there are 0 of them.
There will be many function calls - you will see a non-zero number.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question