node-mysql2: Concurrent call to mysql breaks after some interaction
I have created an singleton connection to use the same connection_id() so the rest app can make use of TEMPORARY TABLES. After some concurrent calls, the connection is broken with this error:
node_modules/mysql2/lib/connection.js:127
connection.threadId = handshakeCommand.handshake.connectionId;
TypeError: Cannot read property 'connectionId' of null
at ClientHandshake.<anonymous>
He is the code
import { Me } from '../shared/Me';
import * as knex from 'knex';
export class BucketDb {
private static _connection: { [sessionId: string]: knex } = {};
public static connect(connectionData) {
if (!this._connection || (this._connection && !this._connection[Me.profile().sessionId])) {
this._connection[Me.profile().sessionId] = knex({
client: 'mysql2',
connection: connectionData,
pool: {
min: 0, max: 1
}
});
}
// this._connection[Me.profile().sessionId].raw('select connection_id()').then((r) => {
// console.log(Me.profile().sessionId, r[0][0]);
// });
return this._connection[Me.profile().sessionId];
}
}
About this issue
- Original URL
- State: open
- Created 7 years ago
- Comments: 15 (8 by maintainers)
Commits related to this issue
- add test for #606 — committed to sidorares/node-mysql2 by sidorares 7 years ago
I think it’s a bad DB design. Mysql does not like too many connections, and typically connection limit is st to 100. Each new connection lives in own os thread with all related overhead - usually >2mb stack space per thread, context switching etc etc.
“other solutions” might be: store temp data in redis or have it in mysql but in one (non-temporary) single table with session id as one of the keys