Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add query timeout support for MySql #10846

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 7 additions & 1 deletion docs/data-source-options.md
Expand Up @@ -162,6 +162,11 @@ Different RDBMS-es have their own specific options.
- `ssl` - object with ssl parameters or a string containing the name of ssl profile.
See [SSL options](https://github.com/mysqljs/mysql#ssl-options).

- `enableQueryTimeout` - If a value is specified for maxQueryExecutionTime, in addition to generating a warning log when a
query exceeds this time limit,
the specified maxQueryExecutionTime value is also used as the timeout for the query.
For more information, check https://github.com/mysqljs/mysql?tab=readme-ov-file#timeouts

## `postgres` / `cockroachdb` data source options

- `url` - Connection url where perform connection to. Please note that other data source options will override parameters set from url.
Expand Down Expand Up @@ -242,6 +247,7 @@ Different RDBMS-es have their own specific options.
- `database` - Database name

## `mssql` data source options

Based on [tedious](https://tediousjs.github.io/node-mssql/) MSSQL implementation. See [SqlServerConnectionOptions.ts](..\src\driver\sqlserver\SqlServerConnectionOptions.ts) for details on exposed attributes.

- `url` - Connection url where perform connection to. Please note that other data source options will override parameters set from url.
Expand Down Expand Up @@ -547,10 +553,10 @@ The following TNS connection string will be used in the next explanations:
(SERVER=shared)))
)
```

- `sid` - The System Identifier (SID) identifies a specific database instance. For example, "sales".
- `serviceName` - The Service Name is an identifier of a database service. For example, `sales.us.example.com`.


## Data Source Options example

Here is a small example of data source options for mysql:
Expand Down
7 changes: 7 additions & 0 deletions src/driver/mysql/MysqlConnectionOptions.ts
Expand Up @@ -109,6 +109,13 @@ export interface MysqlConnectionOptions
*/
readonly connectorPackage?: "mysql" | "mysql2"

/**
* If a value is specified for maxQueryExecutionTime, in addition to generating a warning log when a query exceeds this time limit,
* the specified maxQueryExecutionTime value is also used as the timeout for the query.
* For more information, check https://github.com/mysqljs/mysql?tab=readme-ov-file#timeouts
*/
readonly enableQueryTimeout?: boolean

/**
* Replication setup.
*/
Expand Down
11 changes: 9 additions & 2 deletions src/driver/mysql/MysqlQueryRunner.ts
Expand Up @@ -202,10 +202,17 @@ export class MysqlQueryRunner extends BaseQueryRunner implements QueryRunner {
query,
parameters,
)

const enableQueryTimeout =
this.driver.options.enableQueryTimeout
const maxQueryExecutionTime =
this.driver.options.maxQueryExecutionTime
const queryPayload =
enableQueryTimeout && maxQueryExecutionTime
? { sql: query, timeout: maxQueryExecutionTime }
: query
const queryStartTime = +new Date()
databaseConnection.query(
query,
queryPayload,
parameters,
async (err: any, raw: any) => {
// log slow queries if maxQueryExecution time is set
Expand Down