我使用 Neo4j java driver在我的游戏应用程序中。
目前,我为每个 Controller (即每个 http 调用)初始化一个新的驱动程序。当 autoclosable close 方法运行时,它似乎会阻塞整个调用大约两秒钟。在不关闭驱动程序的情况下运行(显然是一个坏主意)将我的测试时间从 25 秒缩短到 5 秒。
我确实怀疑我以错误的方式使用了驱动程序,我想我应该在应用程序范围内使用一个驱动程序,但不知道如何使用。在play框架中使用java Neo4j驱动程序的正确方法是什么?
最佳答案
Driver 的 Javadoc界面状态:
Driver implementations are typically thread-safe, act as a template for Session creation and host a connection pool. All configuration and authentication settings are held immutably by the Driver. Should different settings be required, a new Driver instance should be created.
A driver maintains a connection pool for each remote Neo4j server. Therefore the most efficient way to make use of a Driver is to use the same instance across the application.
因此,一般来说,您应该使用单个 Driver
实例。
共享同一实例的一种方法是实现一个提供 singleton 的工厂类。 驱动程序
实例。这是一个非常基本的线程安全示例:
class DriverFactory {
private static Driver instance;
public static synchronized Driver getDriver() {
if (instance == null) {
instance = GraphDatabase.driver(...);
}
return instance;
}
}
关于java - Neo4j 驱动程序正在运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50709595/