我正在為其中一個微服務的實時項目設置 Go with Neo4j我瀏覽了有關設置相同的文檔,但它沒有顯示執行相同操作的最佳實踐(特別是全局并在整個應用程序中傳遞會話實例)這就是我正在做的設置,想知道這是否是正確的方法:// app.goimport ""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正在執行查詢的包中
1 回答

DIEA
TA貢獻1820條經驗 獲得超2個贊
自述文件中的示例說明如下:
// 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
實例,因為它不是線程安全的。
- 1 回答
- 0 關注
- 292 瀏覽
添加回答
舉報
0/150
提交
取消