如何在Neo4J Node.Js驱动程序中打开/关闭驱动程序和会话?

问题描述 投票:4回答:1

我正在使用Neo4J Javascript Driver从我的Node.Js应用程序查询数据库。

假设我使用以下构造将几个查询发送到Neo4J(遍历它们)。

我什么时候需要关闭session,什么时候需要关闭driver

我应该在每个周期的末尾(如下所示)还是在所有周期结束后执行此操作?如果是后者,我该怎么办?

还有另一个问题-我真的必须关闭driver吗?如果我的应用程序连续运行怎么办?如果由于某些错误而退出并重新启动该怎么办?

var driver = neo4j.driver("bolt://localhost", neo4j.auth.basic("neo4j", "neo4j"));

var session = driver.session();

var transactionQueries = ['MATCH ...', 'MATCH ...'];

for (var key in transactionQueries) {
session
.run(transactionQueries[key])
.subscribe({
onNext: function (record) {
  console.log(record.get('name'));
},
onCompleted: function () {
  session.close();
},
onError: function (error) {
  console.log(error);
}
});
}


driver.close();
javascript node.js neo4j neo4j-javascript
1个回答
0
投票

我认为您可以保留连接(驱动程序),但应按照neo4j-javascript-driver上的说明打开和关闭会话>

// Create a session to run Cypher statements in.
// Note: Always make sure to close sessions when you are done using them!
var session = driver.session()

session
  .run('MERGE (james:Person {name : $nameParam}) RETURN james.name AS name', {
    nameParam: 'James'
  })
  .then(result => {
    result.records.forEach(record => {
      console.log(record.get('name'))
    })
  })
  .catch(error => {
    console.log(error)
  })
  .then(() => session.close())

其他人认为我建议您看看是否有一个OGM可以像neo4j-node-ogm那样帮助您更好地进行编码>

© www.soinside.com 2019 - 2024. All rights reserved.