Neo4j AuraDB Isn't Consistently Allowing Requests

Hi! I am working on a small web application for a personal project. I am trying to use Neo4j AuraDB as a way to communicate with my javascript web application. To secure a connection I run the code (With the appropriate username and password):

const neo4j = require("neo4j-driver");

const driver = neo4j.driver(
"bolt+s://2314d5f6.databases.neo4j.io",
neo4j.auth.basic("Username", "password")
);

However, many times when I try to make a query, I get the error: Neo4jError: Pool is closed, it is no more able to serve requests.

I happens almost every time, but not every time. One example of a query I make is as follows:

async function checkPartyCode() {
let done = false;
let pString = "";
const session1 = driver.session();
//while (!done) {
const pCode = Math.floor(Math.random() * 900000) + 100000;
console.log(pCode.toString().substring(0, 3) + "-" + pCode.toString().substring(3));
pString = pCode.toString().substring(0, 3) + "-" + pCode.toString().substring(3);
try {
const result = await session1.run(
"MATCH (p:Party {partyCode: $partyCode}) RETURN EXISTS(p) as exists",
{ partyCode: pString }
);
if (!result.records[0].get('exists')) {
done = true;
console.log("success!");
} else {
console.log(already a party ID: ${pString}, trying another);
}
} catch (error) {
console.error(error);
};
//}
session1.close();
localStorage.setItem("partyCode", pString);
return pString;
}

I could really use some assistance in figuring out why my web application can't seem to have a stable connecting with Neo4j AuraDB

Instead of the implicit session.run function, try using the session.executeRead transaction function. Transaction functions have automatic re-try and other logic more appropriate for connections to Aura.

Here's the example from the docs:


const session = driver.session()
const titles = []
try {
  const result = await session.executeRead(tx =>
    tx.run('MATCH (p:Product) WHERE p.id = $id RETURN p.title', { id: 0 })
  )

  const records = result.records
  for (let i = 0; i < records.length; i++) {
    const title = records[i].get(0)
    titles.push(title)
  }
} finally {
  await session.close()
}```