亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用 Golang 設置 Neo4J

使用 Golang 設置 Neo4J

Go
楊__羊羊 2022-10-24 16:22:39
我正在為其中一個微服務的實時項目設置 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實例,因為它不是線程安全的。



查看完整回答
反對 回復 2022-10-24
  • 1 回答
  • 0 關注
  • 292 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號