question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Can't add new command when connection is in closed state

See original GitHub issue

Issue type:

[ ] question [x ] bug report [ ] feature request [ ] documentation issue

Database system/driver:

[ ] cordova [ ] mongodb [ ] mssql [x] mysql / mariadb [ ] oracle [ ] postgres [ ] cockroachdb [ ] sqlite [ ] sqljs [ ] react-native [ ] expo

TypeORM version:

[ ] latest [ ] @next [x] ^0.2.18 (or put your version here)

Steps to reproduce or a small repository showing the problem:

{ QueryFailedError: Can't add new command when connection is in closed state
 |        at new QueryFailedError (/usr/src/my-app/src/error/QueryFailedError.ts:9:9)
 |        at Query.onResult (/usr/src/my-app/src/driver/mysql/MysqlQueryRunner.ts:166:37)
 |        at PoolConnection._addCommandClosedState (/usr/src/my-app/node_modules/mysql2/lib/connection.js:139:11)
 |        at PoolConnection.query (/usr/src/my-app/node_modules/mysql2/lib/connection.js:534:17)
 |        at MysqlQueryRunner.<anonymous> (/usr/src/my-app/src/driver/mysql/MysqlQueryRunner.ts:155:36)
 |        at step (/usr/src/my-app/node_modules/tslib/tslib.js:136:27)
 |        at Object.next (/usr/src/my-app/node_modules/tslib/tslib.js:117:57)
 |        at fulfilled (/usr/src/my-app/node_modules/tslib/tslib.js:107:62)
 |        at process._tickCallback (internal/process/next_tick.js:68:7)
 |      message: 'Can\'t add new command when connection is in closed state',
 |      fatal: true,
 |      name: 'QueryFailedError',
 |      query: 'COMMIT',
 |      parameters: [] } }

With no reason, the underlying mysql connection seems to be closed. It seems related to connection pooling (see https://github.com/sidorares/node-mysql2/issues/939)

How can we fix that using typeorm ?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:9

github_iconTop GitHub Comments

1reaction
Destreyfcommented, Jan 10, 2020

In the case you mentioned where you’re using a repository that’s from outside the transaction, yes that would cause issues.

You need to use the @TransactionRepository() in your method instead of your global one in the constructor as the repository likely isn’t bound to a pooled connection once it’s inside the connection manager.

See here: https://github.com/typeorm/typeorm/blob/master/docs/transactions.md#transaction-decorators

0reactions
daweedmcommented, Jan 11, 2020

@Destreyf No problem, thanks for your help !

Read more comments on GitHub >

github_iconTop Results From Across the Web

Can't add new command when connection is in closed state
I am using the Amazon EC2 with Ubuntu. I am running the server using the PM2, as pm2 start bin/www --log-date-format "YYYY-MM-DD ...
Read more >
Error: Can't add new command when connection is in closed ...
{ Error: Can't add new command when connection is in closed state at PoolConnection._addCommandClosedState. I use mysql 2 and connect Pool.
Read more >
Can't add new command when connection is in closed state ...
I had a similar problem and ended up having to put the connection request in it's own .js file and import it into...
Read more >
Can't add new command when connection is in closed state
Node+Express+MySQLを使ってデータ取得時に2回目から以下のエラーが発生しましたので、原因と解決…
Read more >
Can't add new command when connection is in closed state
Can't add new command when connection is in closed state. ... db.on('error', err => console.error('Keyv connection error:', err));.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found