thingdom / node-neo4j

[RETIRED] Neo4j graph database driver (REST API client) for Node.js
Apache License 2.0
925 stars 135 forks source link

Better handling for multiple events in a transaction #172

Open jrgleason opened 8 years ago

jrgleason commented 8 years ago

Currently if try an async thread loop firing off Cypher requests Neo4J barfs saying 'you are trying to run requests while another is running'.

My work around for now is using the asynch library....

async.eachSeries(regulation.sources, function iterator(source, callback){
    mergeSource(source, transaction, callback);
}, function(err){
    if(err){
      transaction.rollback("Problem adding source");
      throw err;
    }
    transaction.commit(function () { });
}); 

It would be nice if there was a way to handle this in node-neo4j

hilkeheremans commented 8 years ago

@jrgleason Try this using bluebird Promises

Promise.map(regulation.sources, function(source) {
    return mergeSourceAsync(source,transaction)
},{concurrency:1}).then(function(value) {
    // commit
}).catch(function(error) {
    // rollback
})

Easiest to handle this using async/await or coroutines/yield. I'm also assuming that you'll either promisify mergeSource() using bluebird or manually construct a quick wrapper around it so that it returns a promise instead of firing a callback.

(Edit for future readers:) Note the addition of {concurrency: 1} as an option, which ensures only one element of the array is executed at a time until its promise resolves.

jrgleason commented 8 years ago

Right but I am pretty sure this doesn't work in a transaction since you are not waiting for the previous call to finish. This is why I needed to remove my promises

jrgleason commented 8 years ago

Ahh nm missed the concurrency thing at the end. Interesting.

hilkeheremans commented 8 years ago

Yes, that's definitely the kicker. It'll make sure every promise is executed sequentially. It's great fun :-)

aseemk commented 8 years ago

Sorry for the delay folks. Glad to hear you're unblocked for now.

As mentioned in https://github.com/thingdom/node-neo4j/issues/164#issuecomment-142730727:

You have an interesting and good point that node-neo4j could queue the queries under the hood. I hesitate with something like that though, as part of node-neo4j's goals is to be transparent. It feels like it's worth knowing that the queries aren't running concurrently, rather than node-neo4j hiding that.

Curious what you guys think?