title: What is the proper way to close a session using neo4j-javascript-driver?
body: <p>Right now I am executing <a href="https://neo4j.com/docs/driver-manual/1.7/sessions-transactions/#driver-transactions-transaction-functions" rel="nofollow noreferrer">writeTransactions</a> closing the neo4j driver session as suggested by the documentation.</p>
  
  <p>However, I haven't found how to handle the <code>session.close()</code> when there is an error. This might be leading to all the connections of the pool never closing and I'm receiving a lot of errors like this after some days that the application is up:</p>
  
  <pre><code>(node:1) UnhandledPromiseRejectionWarning:
  Unhandled promise rejection (rejection id: 5725): 
  Neo4jError: Connection acquisition timed out in 60000 ms.
  </code></pre>
  
  <p>This is the driver configuration:</p>
  
  <pre><code>const driver = neo4j.driver(
    process.env.NEO4J_URI,
    neo4j.auth.basic(process.env.NEO4J_USER, process.env.NEO4J_PASSWORD),
    {
      maxConnectionLifetime: 60 * 60 * 1000, // 1 hour
      maxConnectionPoolSize: 300,
      encrypted: "ENCRYPTION_ON",
      trust: "TRUST_CUSTOM_CA_SIGNED_CERTIFICATES",
      trustedCertificates: [process.env.NEO4J_TRUSTED_CERTS],
      logging: {
        level: 'debug',
        logger: (level, message) =&gt; console.log('+++' + level + ' ' + message)
      }
    }
  );
  </code></pre>
  
  <p>These are the 2 ways I'm using the driver with transactions:</p>
  
  <p><strong>1. Using async/await and catch</strong></p>
  
  <pre><code>const neo4jsession = driver.session();
  
  var result = await neo4jsession.writeTransaction(tx =&gt;
    tx.run("Cypher Query")
  ).catch(err =&gt; {
    try {
      neo4jsession.close();
    } finally {
      reject(err);
    }
  });
  
  neo4jsession.close();
  
  // Do something with result if not undefined
  </code></pre>
  
  <p><strong>2. Using then/catch</strong></p>
  
  <pre><code>const neo4jsession = driver.session();
  
  var result = neo4jsession.writeTransaction(tx =&gt;
    tx.run("Cypher Query")
  );
  
  result.then(items =&gt; {
    neo4jsession.close();
    // do something with items
  }).catch(error =&gt; {
    try {
      neo4jsession.close();
    } finally {
      reject(error);
    }
  });
  </code></pre>
  
  <p>Am I correctly closing the sessions in both cases? If not, what's the proper way of closing the session for both cases?</p>
  
  <p>Please help, this has been a headache for days =(</p>