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.

AlreadyHasActiveConnectionError: Cannot create a new connection named "default", because connection with such name already exist and it now has an active connection session

See original GitHub issue

I’m submitting a…


[ ] Regression 
[ x ] Bug report
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.

Current behavior

I am getting this issue while trying to use the application context for my job queues. While NestFactory.createApplicationContext(AppModule) is initialising the application module TypeOrm is creating connection with same default name. How do I prevent it?

[Nest] 11985   - 2019-1-16 16:08:34   [TypeOrmModule] Unable to connect to the database. Retrying (1)... +31ms
AlreadyHasActiveConnectionError: Cannot create a new connection named "default", because connection with such name already exist and it now has an active connection session.
    at new AlreadyHasActiveConnectionError (/var/www/sxapi/src/error/AlreadyHasActiveConnectionError.ts:8:9)
    at ConnectionManager.create (/var/www/sxapi/src/connection/ConnectionManager.ts:57:23)
    at Object.<anonymous> (/var/www/sxapi/src/index.ts:219:35)
    at step (/var/www/sxapi/node_modules/typeorm/index.js:32:23)
    at Object.next (/var/www/sxapi/node_modules/typeorm/index.js:13:53)
    at /var/www/sxapi/node_modules/typeorm/index.js:7:71
    at new Promise (<anonymous>)
    at __awaiter (/var/www/sxapi/node_modules/typeorm/index.js:3:12)
    at Object.createConnection (/var/www/sxapi/node_modules/typeorm/index.js:215:12)
    at rxjs_1.defer (/var/www/sxapi/node_modules/@nestjs/typeorm/dist/typeorm-core.module.js:137:29)
    at Observable._subscribe (/var/www/sxapi/node_modules/rxjs/src/internal/observable/defer.ts:57:15)
    at Observable._trySubscribe (/var/www/sxapi/node_modules/rxjs/src/internal/Observable.ts:224:19)
    at Observable.subscribe (/var/www/sxapi/node_modules/rxjs/src/internal/Observable.ts:205:14)
    at RetryWhenOperator.call (/var/www/sxapi/node_modules/rxjs/src/internal/operators/retryWhen.ts:39:19)
    at Observable.subscribe (/var/www/sxapi/node_modules/rxjs/src/internal/Observable.ts:200:16)
    at /var/www/sxapi/node_modules/rxjs/src/internal/Observable.ts:345:12

My Job Processor:

const VisitorCheckinNotificationProcessor = async (
  job: Job,
  done: DoneCallback,
) => {
  // application context
  const context = await NestFactory.createApplicationContext(AppModule);
  let data: any = job.data;

  console.log('[VISITOR_CHECKIN_NOTIFICATION_SENT]', data);

  try {
    await context.close();
  } catch (e) {
    console.log('[EXCEPTION WHILE TERMINATING APPLICATION CONTEXT]', e);
  }

  done(null, data);
};

app.module.ts

@Module({
  imports: [
    TypeOrmModule.forRootAsync({
      useFactory: (config: ConfigService) => ({
        type: 'mysql',
        host: config.mysqlHost,
        port: config.mysqlPort,
        username: config.mysqlUsername,
        password: config.mysqlPassword,
        database: config.databaseName,
        entities: [__dirname + '/**/*.entity{.ts,.js}'],
        synchronize: config.databaseSyncEnabled,
        logging: config.databaseLoggingEnabled,
      }),
      inject: [ConfigService],
    }),
    CoreModule,
    UserModule,
  ],
  controllers: [AppController],
})
export class AppModule {}

Environment


Nest version: 5.4.0

 
For Tooling issues:
- Node version: 9.4  
- Platform:  Linux (Ubuntu 18.04) 

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

64reactions
kamilmysliwieccommented, Jan 19, 2019

Set keepConnectionAlive option to true in the TypeOrmModule options.

18reactions
iamsayantancommented, Jan 22, 2019

@kamilmysliwiec, Yep it solves the issue. Could you please explain what happens?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Nest: Cannot create a new connection named "default ...
Nest: Cannot create a new connection named "default", because connection with such name already exist and i t now has an active connection...
Read more >
typeorm/typeorm - Gitter
... Cannot create a new connection named "default", because connection with such name already exist and it now has an active connection session....
Read more >
[TypeScript][PostgreSQL]Try TypeORM - DEV Community ‍ ‍
... create a new connection named "default", because connection with such name already exist and it now has an active connection session. at...
Read more >
Cannot create a new connection named "default", because ...
Cannot create a new connection named "default", because connection with such name already exist and it now has an active connection session.
Read more >
GraphQL+NestJS+TypeORM+MySQL+Serverless(offline)の ...
AlreadyHasActiveConnectionError : Cannot create a new connection named "default", because connection with such name already exist and it now ...
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