为了账号安全,请及时绑定邮箱和手机立即绑定

使用 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 关注
  • 256 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号