Setting up Neo4J with Golang

huangapple go评论80阅读模式
英文:

Setting up Neo4J with Golang

问题

我正在为一个微服务的实时项目设置Go与Neo4j。我已经阅读了关于设置的文档,但没有显示如何最佳实践地执行相同的操作(特别是在整个应用程序中全局传递会话实例)。

以下是我设置相同操作的步骤,想知道这是否是正确的方法:

// app.go

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

type App struct {
	Router *mux.Router
	DB *sqlx.DB
	Neo4j neo4j.Session // 在全局范围内设置neo4j会话以进行注入
}

// =============================
// Neo4j初始化
// =============================
driver, err2 := neo4j.NewDriver(
	neo4jConfig.connstring,
	neo4j.BasicAuth(neo4jConfig.username, neo4jConfig.password, ""),
	func(c *neo4j.Config){
		c.Encrypted = false
	},
)
checkForErrors(err2, "无法连接到NEO4J")
defer driver.Close()
session, err3 := driver.NewSession(neo4j.SessionConfig{})
a.Neo4j = session // 分配会话实例

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

英文:

I am setting up Go with Neo4j on a live project for one of the microservices

I went through the docs around setting up the same but it does not show the best practice to do the same (specifically globally and pass around the session instance throughout the application)

This is what I am doing to setup the same, was wondering if this is the right approach:

// 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

Now this will be injected as a dependency in the repo package where the queries are being executed

答案1

得分: 3

自述文件中的示例中提到了以下内容:

// 会话是短暂的,创建成本低且不是线程安全的。通常在 Web 应用程序中的每个请求中创建一个或多个会话。
// 在完成后,请确保调用会话的 Close 方法。
// 对于多数据库支持,请将 sessionConfig.DatabaseName 设置为所请求的数据库。
// 如果只使用读取,请将会话配置为读取模式。
session := driver.NewSession(neo4j.SessionConfig{})

因此,拥有一个全局的 driver 实例不是问题,但是你不应该使用全局的 session 实例,因为它不是线程安全的。

英文:

The example in the readme says the following:

// 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{})

So having a global driver instance is not an issue, but you should not be using a global session instance since it is not thread safe.

huangapple
  • 本文由 发表于 2022年1月15日 02:38:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/70715147.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定