java - Neo4j 驱动程序正在运行

标签 java neo4j playframework connection-pooling autocloseable

我使用 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/

相关文章:

java - 将 Java 编译器转换为 Linux

java - Intellij 和 sbt 0.13.5 "URI has an authority component"

java - Play : Using a configuration property as the value of an annotation

javascript - 多级 HTML 表单

java - Android Studio Java 文件未找到异常

java - 用 do while 循环提示,似乎无法进入循环

java - 如何在 weblogic server 10.3 中定义自定义服务器属性

具有密码保护的 Neo4J 网络管理员

java - 是否有用于旧依赖项的 Neo4j maven 存储库?

Neo4j 根据条件统计子节点