CannotConnectAlreadyConnectedError: Cannot create a "test" connection because connection to the database already established. TypeORM

Viewed 85

I'm trying to create a TypeORM-Graphql Server but I'm having trouble to make a simple connection to


import reflect_metadata from "reflect-metadata"
import server from './graphql/index'
import express from 'express'
import {Connection, createConnection} from "typeorm"
reflect_metadata

const main = async () => {
    const app = express()
    const graphql_serv = await server()
    let connection:Connection
    connection = await createConnection('test')
    await connection.connect()
    await graphql_serv.start()
    graphql_serv.applyMiddleware({app})
    app.listen(4000, () => {
        console.log("server ready")
    })
}

main()

This is my configFile:

{
    "name": "test",
    "type": "mysql",
    "host": "localhost",
    "port": [REDACTED],
    "username": "[REDACTED]",
    "password": "[REDACTED]",
    "database": "test",
    "synchronize": true,
    "logging": true
}

The graphql server works fine, TypeORM connects successfully with the database but when I try to connect the instance I get this error:


query: START TRANSACTION
query: SELECT * FROM `INFORMATION_SCHEMA`.`COLUMNS` WHERE `TABLE_SCHEMA` = 'test' AND `TABLE_NAME` = 'typeorm_metadata'
query: SELECT SCHEMA() AS `schema_name`
query: COMMIT
/[My computer path]/server/node_modules/typeorm/error/CannotConnectAlreadyConnectedError.js:11
        var _this = _super.call(this) || this;
                           ^

CannotConnectAlreadyConnectedError: Cannot create a "test" connection because connection to the database already established.
    at new CannotConnectAlreadyConnectedError ([My computer path]/server/node_modules/typeorm/error/CannotConnectAlreadyConnectedError.js:11:28)
    at Connection.<anonymous> ([My computer path]/server/node_modules/typeorm/connection/Connection.js:110:35)
    at step ([My computer path]/server/node_modules/tslib/tslib.js:143:27)
    at Object.next ([My computer path]/server/node_modules/tslib/tslib.js:124:57)
    at [My computer path]/server/node_modules/tslib/tslib.js:117:75
    at new Promise (<anonymous>)
    at Object.__awaiter ([My computer path]/server/node_modules/tslib/tslib.js:113:16)
    at Connection.connect ([My computer path]/server/node_modules/typeorm/connection/Connection.js:104:24)
    at [My computer path]/server/dist/index.js:25:22
    at Generator.next (<anonymous>)
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

This is the only file where createConnection is called once, I deleted node_modules and reinstalled all packages, so I can't understand why there's a problem with this simple connection. I followed the code from the TypeORM docs.

I hope you can help me with this.

0 Answers
Related