我正在為其中一個微服務的實時專案設定 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正在執行查詢的包中
uj5u.com熱心網友回復:
自述檔案中的示例說明如下:
// 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實體,因為它不是執行緒安全的。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/414539.html
標籤:
