在多线程中使用 titan graph 的最佳实践是什么?

What is best practice for using titan graph in multi threads?

我用 spring 启动构建了一个 Web 服务。我使用 titan graph 作为跨 Web 服务访问的单例。因此,如果对 web 服务有多个请求,web 服务器将生成线程来处理请求,并且 titan graph 将在这些线程中使用。这种方法有什么问题吗?我使用 titan graph 1.0.0 和 tinkerpop 3.0.1-incubating。所以 titan graph 对我来说很新,我不知道在多线程中使用 titan graph 是否有任何冲突。如果这种方法有问题,那么在多线程中使用 titan graph 的最佳实践是什么?感谢您的帮助。

以这种方式使用 Titan 是完全可以接受的。您只需要确保事务不会在请求之间泄漏。根据 TinkerPop 语义,Graph 实例上的事务绑定到当前线程。因此,每个HTTP请求的结束都需要根据请求的成功或失败以commit()rollback()关闭事务。如果您对始终关闭交易的能力甚至有点不确定,那么您应该考虑在请求开始时发出 rollback() 以清除前一个交易的任何陈旧状态。

是的,加油。如果您使用的是 Titan,请务必阅读 multi-threaded transactions 上提供的文档。

With Blueprints' default transaction handling, each thread automatically opens its own transaction against the graph database. To open a thread-independent transaction, use the newTransaction() method. The newTransaction() method returns a new TransactionalGraph object that represents this newly opened transaction. The graph object tx supports all of the methods that the original graph did, but does so without opening new transactions for each thread. This allows us to start multiple threads which all work concurrently in the same transaction and one of which finally commits the transaction when all threads have completed their work.

如果您使用的库可能跨多个线程承载单个概念事务,例如,这可能会更安全。通过使用 deferreds 或 promises。