使用 Golang 设置 Neo4J

我正在为其中一个微服务的实时项目设置 Go with Neo4j


我浏览了有关设置相同的文档,但它没有显示执行相同操作的最佳实践(特别是全局并在整个应用程序中传递会话实例)


这就是我正在做的设置,想知道这是否是正确的方法:


// app.go


import ""github.com/neo4j/neo4j-go-driver/neo4j""


type App struct {

    Router *mux.Router

    DB *sqlx.DB

    Neo4j neo4j.Session // setting neo4j session globally for injection

}


// =============================

// Neo4j initialization 

// =============================

    driver, err2 := neo4j.NewDriver(

        neo4jConfig.connstring,

        neo4j.BasicAuth(neo4jConfig.username, neo4jConfig.password, ""),

        func(c *neo4j.Config){

            c.Encrypted = false

        },

    )

    checkForErrors(err2, "Cannot connect to NEO4J")

    defer driver.Close()

    session, err3 := driver.NewSession(neo4j.SessionConfig{})

    a.Neo4j = session // 👈 assigning the session instance

现在,这将作为依赖项注入到repo正在执行查询的包中


杨__羊羊
浏览 258回答 1
1回答

DIEA

自述文件中的示例说明如下:// Sessions are short-lived, cheap to create and NOT thread safe. Typically create one or more sessions// per request in your web application. Make sure to call Close on the session when done.// For multi-database support, set sessionConfig.DatabaseName to requested database// Session config will default to write mode, if only reads are to be used configure session for// read mode.session := driver.NewSession(neo4j.SessionConfig{})所以拥有一个全局driver实例不是问题,但你不应该使用全局session实例,因为它不是线程安全的。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go