首页 > 解决方案 > 为什么要发布 QueryRunner?

问题描述

我有一个看起来像这样的大型工作流程

@Transaction()
doAlotOfStuff(
  @TransactionManager() txEntityManager: EntityManager
) {
  const myEntity txEntityManager.getRepository(MyEntity).find(id);

  doSomething(txEntityManager, myEntity);
  doSomethingElse(txEntityManager, myEntity);
  txEntityManager.getRepository(MyEntity).save(myEntity);

  const data = txEntityManager.getRepository(AnotherEntity).find( /*some complex find conditions*/ )
  doMoreStuff(txEntityManager, datat);
}

不幸的是,真正的功能过于复杂,无法在此处发布。但问题是,在运行该函数的过程中,QueryRunner它会被释放,当它尝试运行更多查询时它会崩溃。我确保每个查询都使用txEntityManager. 我不明白为什么它一直被释放。我调试并发现使用该find函数的查询正在释放QueryRunner.

查找功能如下所示

public async getHistoricalCustomerOrders(entityManager: EntityManager, superOrder: SuperOrder, customerIds: string[]) : Promise<CustomerOrder[]> {
        return await entityManager.find(CustomerOrder, {
            where: qb => {
              qb.where({
                customerId: In(customerIds),
              })
                .andWhere('"CustomerOrder__order__superOrder"."start_date_period" > :startDate', {
                  startDate: sub(superOrder.startDatePeriod, { years: 1 }),
                })
                .andWhere('"CustomerOrder__order__superOrder"."start_date_period" < :endDate', {
                  endDate: superOrder.startDatePeriod,
                });
            },
            relations: [
              'some relations'
            ],
          });

标签: node.jsnestjstypeorm

解决方案


推荐阅读