I'd like to provide a filtered readonly view to a large neo4J database. Each time, if the user changes the filter params the previous cypher request will become obsolete and need to be cancelled if it is still running.
Just ignoring the obsolete responses would lead in wasting computing time and is no option.
Cancelling the HTTP request seems to not stop the process in the database and thus is no option as well.
Closing the last open session while the request is still running leads to an exception in the next query. Although I get a new session from the driver I get "you cannot run more transactions on a closed session.". (see example code)
const delay = (ms: number) => new Promise(resolve => setTimeout(() => resolve(null), ms))
const time = () => new Date().getTime()
let t0 = time()
let log = (msg: string, ...params: unknown[]) => console.log(`${time() - t0}: ${msg}`, ...params)
let session: Session | null = null
const userRequests = [
'match (n) return count(n)',
'match (a)-[r]->(b) return r',
'match (m) return m',
'match (n) return n']
for (const cypher of userRequests) {
if (session) {
log('CLOSE...')
// await delay(0)
await session.close()
log('... CLOSED')
session = null
}
log('RUN...')
session = d.session()
session.run(cypher, {})
.then(res => log(' ... DONE', res.summary.query.text))
.catch(err => log('... ERROR: ' + (err as Neo4jError).message))
.finally(() => {
void session?.close();
session = null;
})
}
console
0: RUN...
0: CLOSE...
4: ... CLOSED
4: RUN...
4: CLOSE...
4: ... ERROR: You cannot run more transactions on a closed session.
6: ... CLOSED
6: RUN...
7: CLOSE...
7: ... ERROR: You cannot run more transactions on a closed session.
9: ... CLOSED
9: RUN...
9: ... ERROR: You cannot run more transactions on a closed session.
11: ... ERROR: You cannot run more transactions on a closed session.
With the 4 requests I would expected 3x ERROR and 1xDONE. But however, maybe closing a session to cancel the request is not intended. Is it?
So, how can I cancel a javascript neo4j driver cypher request?
Many thanks for hints or a solution!