如何处理idle_in_transaction_session_timeout?

时间:2019-12-05 10:45:55

标签: pg-promise

设置idle_in_transaction_session_timeout后,数据库将终止空闲一段时间的连接。
这可以按预期工作,但是我不知道我们应该如何在应用代码中处理这种情况。
我们正在使用pg-promise 10.3.1

测试的详细信息

  • 我们将连接池大小设置为1,这样我们只有一个会话
  • 我们将idle-transaction-session-timeout的设置为2.5秒:
    SET idle_in_transaction_session_timeout TO 2500
  • 现在我们开始交易并休眠5
  • 2.5秒后,数据库将终止会话并将错误发送给客户端:
    pgp-error error: terminating connection due to idle-in-transaction timeout
  • 再过2.5秒后,事务代码尝试发送查询(通过已终止的会话),这将按预期失败:
    dbIdle failed Error: Client has encountered a connection error and is not queryable
    • 然后pg-promise将尝试回滚该事务,该事务也会失败(我猜也是预期的)
  • 但是现在我们开始一个新查询,并且此查询失败,并带有
    dbCall failed Client has encountered a connection error and is not queryable
    • 这是预期的吗?我希望pg-promise可以以某种方式从池中删除“断开”的连接,并希望我们能得到一个新的连接。
    • 显然不是这种情况,那么我们应该如何处理这种情况:即如何恢复,以便我们可以向数据库发送新查询?

代码示例

import pgPromise, { IMain } from "pg-promise";
import * as dbConfig from "./db-config.json";
import { IConnectionParameters } from "pg-promise/typescript/pg-subset";

const cll = "pg";
console.time(cll);

const pgp: IMain = pgPromise({
  query(e) {
    console.timeLog(cll,`> ${e.query}`);
  },
  error(e, ctx) {
    console.timeLog(cll,"pgp-error", e);
  }
});

const connectParams: IConnectionParameters = {
  ...dbConfig,
  application_name: "pg-test",
  max: 1
};
const db = pgp(connectParams);

/**
 * @param timeoutMs 0 is no timeout
 */
async function setDbIdleInTxTimeout(timeoutMs: number = 0) {
  await db.any("SET idle_in_transaction_session_timeout TO $1;", timeoutMs);
}

async function dbIdle(sleepTimeSec: number) {
  console.timeLog(cll, `starting db idle ${sleepTimeSec}`);
  const result = await db.tx(async t => {
    await new Promise(resolve => setTimeout(resolve, sleepTimeSec * 1000));
    return t.one("Select $1 as sleep_sec", sleepTimeSec);
  });
  console.timeLog(cll, result);
}

async function main() {
  await setDbIdleInTxTimeout(2500);
  try {
    await dbIdle(5);
  } catch (e) {
    console.timeLog(cll, "dbIdle failed", e);
  }
  try {
    await db.one("Select 1+1 as res");
  } catch (e) {
    console.timeLog(cll, "dbCall failed", e);
  }
}

main().finally(() => {
  pgp.end();
});

控制台输出 t(删除了一些无用的行):

"C:\Program Files\nodejs\node.exe" D:\dev_no_backup\pg-promise-tx\dist\index.js
pg: 23.959ms > SET idle_in_transaction_session_timeout TO 2500;
pg: 28.696ms starting db idle 5
pg: 29.705ms > begin
pg: 2531.247ms pgp-error error: terminating connection due to idle-in-transaction timeout
    at TCP.onStreamRead (internal/stream_base_commons.js:182:23) {
  name: 'error',
  severity: 'FATAL',
  code: '25P03',
}
pg: 2533.569ms pgp-error Error: Connection terminated unexpectedly
pg: 5031.091ms > Select 5 as sleep_sec
pg: 5031.323ms pgp-error Error: Client has encountered a connection error and is not queryable
pg: 5031.489ms > rollback
pg: 5031.570ms pgp-error Error: Client has encountered a connection error and is not queryable
pg: 5031.953ms dbIdle failed Error: Client has encountered a connection error and is not queryable
pg: 5032.094ms > Select 1+1 as res
pg: 5032.164ms pgp-error Error: Client has encountered a connection error and is not queryable
pg: 5032.303ms dbCall failed Error: Client has encountered a connection error and is not queryable

Process finished with exit code 0

1 个答案:

答案 0 :(得分:0)

issue #680已在pg-promise 10.3.5中修复