# Query Runner

> 英文标题：Query Runner
> 官网原文：https://typeorm.io/docs/query-runner
> 文档版本：TypeORM v1.1
> 体例：英文原文（EN）与中文译文（ZH）逐块并列，共 21 块

---

<!-- EN -->
# Query Runner

<!-- ZH -->
# Query Runner

<!-- EN -->
## What is a QueryRunner?

<!-- ZH -->
## 什么是 QueryRunner？

<!-- EN -->
Each new `QueryRunner` instance takes a single connection from the connection pool, if the RDBMS supports connection pooling. For databases that do not support connection pools, it uses the same connection across the entire data source.

<!-- ZH -->
如果 RDBMS 支持连接池，每个新的 `QueryRunner` 实例都会从连接池中取走一个连接。对不支持连接池的数据库，它在整个数据源范围内使用同一个连接。

<!-- EN -->
The full QueryRunner API is documented in the [migrations section](https://typeorm.io/docs/migrations/api).

<!-- ZH -->
完整的 QueryRunner API 记录在[迁移章节](https://typeorm.io/docs/migrations/api)中。

<!-- EN -->
## Creating a new `QueryRunner` instance

<!-- ZH -->
## 创建新的 `QueryRunner` 实例

<!-- EN -->
Use the `createQueryRunner` method to create a new `QueryRunner`:

<!-- ZH -->
使用 `createQueryRunner` 方法创建一个新的 `QueryRunner`：

<!-- EN -->
```typescript
const queryRunner = dataSource.createQueryRunner()
```

<!-- ZH -->
```typescript
const queryRunner = dataSource.createQueryRunner()
```

<!-- EN -->
## Using `QueryRunner`

<!-- ZH -->
## 使用 `QueryRunner`

<!-- EN -->
After creating a new instance of `QueryRunner`, a connection will be acquired from the pool when you issue the first query:

<!-- ZH -->
创建 `QueryRunner` 的新实例后，当你发出第一条查询时就会从池中获取一个连接：

<!-- EN -->
```typescript
const queryRunner = dataSource.createQueryRunner()
await queryRunner.query("SELECT 1")
await queryRunner.release()
```

<!-- ZH -->
```typescript
const queryRunner = dataSource.createQueryRunner()
await queryRunner.query("SELECT 1")
await queryRunner.release()
```

<!-- EN -->
You can also use `connect` method to directly get a connection from the connection pool:

<!-- ZH -->
你也可以用 `connect` 方法直接从连接池中获取一个连接：

<!-- EN -->
```typescript
const queryRunner = dataSource.createQueryRunner()
const clientConnection = await queryRunner.connect()
await queryRunner.release()
```

<!-- ZH -->
```typescript
const queryRunner = dataSource.createQueryRunner()
const clientConnection = await queryRunner.connect()
await queryRunner.release()
```

<!-- EN -->
**Important**: make sure to release the `QueryRunner` when it is no longer necessary to return the connection back to the connection pool:

<!-- ZH -->
**重要**：当 `QueryRunner` 不再需要时，请务必释放它，把连接归还给连接池：

<!-- EN -->
```typescript
await queryRunner.release()
```

<!-- ZH -->
```typescript
await queryRunner.release()
```

<!-- EN -->
After the `QueryRunner` is released, it is no longer possible to use the query runner methods.

<!-- ZH -->
`QueryRunner` 被释放之后，就不能再使用它的方法了。

<!-- EN -->
`QueryRunner` also has its own `EntityManager` instance, which you can use through the `manager` property to run `EntityManager` queries on a particular database connection used by the `QueryRunner` instance:

<!-- ZH -->
`QueryRunner` 也有自己的 `EntityManager` 实例，你可以通过 `manager` 属性使用它，在该 `QueryRunner` 实例所使用的特定数据库连接上执行 `EntityManager` 查询：

<!-- EN -->
```typescript
let queryRunner: QueryRunner
try {
    queryRunner = dataSource.createQueryRunner()
    // use a single database connection to execute multiple queries
    await queryRunner.manager.update(
        Employee,
        { level: "junior" },
        { bonus: 0.2 },
    )
    await queryRunner.manager.update(
        Employee,
        { level: "senior" },
        { bonus: 0.1 },
    )
} catch (error) {
    console.error(error)
} finally {
    // remember to release connection after you are done using it
    await queryRunner.release()
}
```

<!-- ZH -->
```typescript
let queryRunner: QueryRunner
try {
    queryRunner = dataSource.createQueryRunner()
    // 使用单个数据库连接执行多条查询
    await queryRunner.manager.update(
        Employee,
        { level: "junior" },
        { bonus: 0.2 },
    )
    await queryRunner.manager.update(
        Employee,
        { level: "senior" },
        { bonus: 0.1 },
    )
} catch (error) {
    console.error(error)
} finally {
    // 用完之后记得释放连接
    await queryRunner.release()
}
```

<!-- EN -->
## Explicit Resource Management

<!-- ZH -->
## 显式资源管理

<!-- EN -->
`QueryRunner` also supports explicit resource management:

<!-- ZH -->
`QueryRunner` 也支持显式资源管理：

<!-- EN -->
```typescript
async function updateSalaries() {
    await using queryRunner = dataSource.createQueryRunner()
    await queryRunner.manager.update(
        Employee,
        { level: "junior" },
        { bonus: 0.2 },
    )
    await queryRunner.manager.update(
        Employee,
        { level: "senior" },
        { bonus: 0.1 },
    )
    // no need anymore to manually release the QueryRunner
}

try {
    await updateSalaries()
} catch (error) {
    console.error(error)
}
```

<!-- ZH -->
```typescript
async function updateSalaries() {
    await using queryRunner = dataSource.createQueryRunner()
    await queryRunner.manager.update(
        Employee,
        { level: "junior" },
        { bonus: 0.2 },
    )
    await queryRunner.manager.update(
        Employee,
        { level: "senior" },
        { bonus: 0.1 },
    )
    // 不再需要手动释放 QueryRunner
}

try {
    await updateSalaries()
} catch (error) {
    console.error(error)
}
```

<!-- EN -->
When declaring a query runner like this, it will be automatically released after the last statement in the containing scope was executed.

<!-- ZH -->
以这种方式声明 query runner 时，它会在所在作用域的最后一条语句执行完毕后被自动释放。
