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.

Closing a connection doesn't fully clean it up

See original GitHub issue

Issue type: [x] question

Database system/driver: [x] postgres

TypeORM version: [x] @next

I am writing different API test suites that initialise the app once per suite. When starting my application I initialise the connection to a postgres db this way:

const connection = await createConnection();

This works fine for the first suite but from that moment on, I get the following error:

Unhandled promise rejection (rejection id: 12): TypeError: Cannot read property 'connect' of undefined

Which is coming from https://github.com/typeorm/typeorm/blob/master/src/driver/postgres/PostgresDriver.ts#L636.

After a thorough investigation I found that the following snippet would fail when requesting users for the second time:

  const connection1 = await createConnection();
  const result1 = await getUsers();
  await connection1.close();

  const connection2 = await createConnection();
  const result2 = await getUsers();
  await connection2.close();

This is due to:

  • When we close the connection, this doesn’t become null. It is still set to the Connection object, however, isConnected is false.
  • Hence, when you are trying to connect to the database again, createConnection doesn’t return what it did the first time and following queries fail with the aforementioned error.

They way I got it working is by replacing:

const connection = await createConnection();

with:

let connection;
try {
   connection = await getConnection();
   if (!connection.isConnected) {
     await connection.connect();
   }
} catch (e) {
  // no connection created yet, nothing to get
  connection = await createConnection();
 }

My question is: should not this be handled inside the createConnection function?

As far as I can see, the issue is down to the connection.close() not leaving things as they were before the createConnection and that is causing the issue. If we are happy to leave this with still a connection object with isConnected to false then, when requesting a new connection, that function should be clever enough to do the logic above (createConnection VS getConnection & connect). It doesn’t feel like from an app code point of view I should be worried to do the above if I explicitly closed the connection.

What is your point of view on this? If so, I am happy to do the changes myself and submit a PR.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:5
  • Comments:11 (4 by maintainers)

github_iconTop GitHub Comments

10reactions
djGrillcommented, Jul 2, 2018

Hi, any progress on this?

3reactions
jbernalvallejocommented, Apr 30, 2018

Just trying to make it more obvious:

try {
  await getConnection(); // throws exception
} catch(e) {}

let connection = await createConnection();
await getUsers(); // works ok
await connection.close();

connection = getConnection(); // this does not retrieve an exception as before, returns connection with isConnected=false

connection = await createConnection();
await getUsers(); // throws TypeError: Cannot read property 'connect' of undefined

Not sure why by the root cause for the error is that this.master is undefined in obtainMasterConnection when getUsers tries to get hold of the connection.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Connection close method doesnt work, too many clients error
In my opinion,when using the c3p0, connection.close() is not really closing the connection, just put it back in the pool.if you want to ......
Read more >
What happends if I dont close the connection — oracle-tech
When the program exits, the operating system will close any open network connections; the database server will also recognize that the network ...
Read more >
How to End a Relationship the Right Way - Verywell Mind
Ending a relationship properly calls for mutual respect, grace, and maturity. Here's how to break up with someone as gently and effectively ...
Read more >
How to perform a clean boot in Windows - Microsoft Support
On the Services tab of System Configuration, select Hide all Microsoft services, and then select Disable all. Select Apply. Important: If the computer...
Read more >
Close an app on your iPhone or iPod touch - Apple Support (CA)
If an app won't respond or seems frozen, you can close it, then open it again.
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