cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

Neo4jError: You cannot run more transactions on a closed session

mail2michaelenn
Graph Buddy

I have this same problem as this SO question (https://stackoverflow.com/questions/61389805/neo4jerror-run-from-within-the-transaction-or-use-a-dif...) but I am using neo4j 3.5.
I am getting this: Neo4jError: You cannot run more transactions on a closed session. I am not running any concurrent stuff. This happens on startup of my Nodejs server running on DigitalOcean. In my app.js I have two .js modules that access the neo4j DB to initialize two loki tables which always work fine for three years now and no recent changes have been made to those modules.

The nodejs server starts up fine but when I make an API call that access the DB, I use postman to test the API call (or from my Angular app), I get this error. All my neo4j calls uses the same pattern to close the session. I use the following pattern:

router.updateCountryTable = (data, callback) => {

     commons.session.run(`WITH $paramCountry AS countries

                         UNWIND countries AS country

                         MATCH (c:Country)  WHERE c.countryCurrencyCode = country[0]

                         SET c.baseCurrencyXrate = country[1], c.createdAt = $paramCreatedAt

                         RETURN c AS Countries`,

                           {

                            paramCountry: data,

                            paramCreatedAt: moment().format()

                      })

                    .then( result => {

                        commons.session.close();

                        if (!result.records[0]) {

                         return callback("error...there is no country in db");

                        }

                       // response = userData.records[0].get('user').properties;

                       return callback(null, result)

                     })

                    .catch((err) => callback(err.stack))

                    

  }

This is a stack trace of the error:

Neo4jError: You cannot run more transactions on a closed session.
7/29/2020 7:00:00 AM
7/29/2020 7:00:00 AM    at captureStacktrace (/ng-app/node_modules/neo4j-driver/lib/v1/result.js:199:15)
7/29/2020 7:00:00 AM    at new Result (/ng-app/node_modules/neo4j-driver/lib/v1/result.js:65:19)
7/29/2020 7:00:00 AM    at Session._run (/ng-app/node_modules/neo4j-driver/lib/v1/session.js:154:14)
7/29/2020 7:00:00 AM    at Session.run (/ng-app/node_modules/neo4j-driver/lib/v1/session.js:130:19)
7/29/2020 7:00:00 AM    at Function.router.updateCountryTable (/ng-app/server/model/user.js:126:22)
7/29/2020 7:00:00 AM    at /ng-app/server/model/pdmanagement.js:113:27
7/29/2020 7:00:00 AM    at IncomingMessage.resp.on (/ng-app/server/model/user.js:95:18)
7/29/2020 7:00:00 AM    at emitNone (events.js:111:20)
7/29/2020 7:00:00 AM    at IncomingMessage.emit (events.js:208:7)
7/29/2020 7:00:00 AM    at endReadableNT (_stream_readable.js:1064:12)
7/29/2020 7:00:00 AM    at _combinedTickCallback (internal/process/next_tick.js:138:11)
7/29/2020 7:00:00 AM    at process._tickCallback (internal/process/next_tick.js:180:9)

I am a lost because I don't get this error running the code from my local machine using the DB on DigitalOcean....only when I run the code on DigitalOcean.

Any ideas how I can solve this?

6 REPLIES 6

anthapu
Graph Fellow

Can you do the session close after reading from results is complete. I don't think you can close the session before you read the whole result.

As per documentation it states this.

Session lifetime begins with session construction. A session then exists until it is closed, which is typically set to occur after its contained query results have been consumed.

yes i can... and have... the result is actually consumed any time after the .then ... i have also added a . then session.close() after the . catch... as done sometimes .... neither makes a difference

any solutions? :?

This could very well be an ignorant conclusion, but looking at the JSDoc and code of the session.run(...) return type it's type Result:

run(query: Query, parameters?: any, transactionConfig?: TransactionConfig😞 Result;

``Looking at the code associated with Result:

/**
* A stream of {@link Record} representing the result of a query.
* Can be consumed eagerly as {@link Promise} resolved with array of records and {@link ResultSummary}
* summary, or rejected with error that contains {@link string} code and {@link string} message.
* Alternatively can be consumed lazily using {@link Result#subscribe} function.
* @access public
*/
declare class Result implements Promise<QueryResult> { ... }
 
Reading the part about "Can be consumed eagerly as {@link Promise}...." made me decide to attempt to await it's completion. That cleared the issue for me. I hope this helps! 🙂

Note that the first emoji in this response shouldn't be a sad-face. It was copied directly from code in my node_modules. The second smiley is intentional. lol

You seem to reuse a session stored on commons. Don't do that.

Create a new session for every operation and then close it after you're done but *after* you consumed the results.