简体   繁体   English

范围 Neo4j 驱动程序和 session 对象

[英]Scoping Neo4j driver and session objects

I am currently at the beginning of the learning curve with Neo4j , so I am making a lot of assumptions about scoping for Dependency Injection (currently experimenting in a Razor Pages application).我目前正处于Neo4j的学习曲线的开始阶段,所以我对依赖注入的范围做了很多假设(目前在 Razor 页面应用程序中进行试验)。

Assumption 1: Driver is a lightweight protocol wrapper that would be safe in Singleton scope.假设 1:驱动程序是一个轻量级协议包装器,在 Singleton scope 中是安全的。

Assumption 2: It might be useful to scope Session to the web request lifetime.假设 2:它可能对 scope Session到 web 请求生命周期有用。 For instance, I might want to add to a transaction in some middle-ware.例如,我可能想在一些中间件中添加事务。

Here's some example DI setup:这是一些示例 DI 设置:

public void ConfigureServices(IServiceCollection services)
{
    services
        .AddSingleton(_ => GraphDatabase.Driver("bolt://localhost:7687", AuthTokens.Basic("neo4j", "hubdb")))
        .AddScoped(_ => _.GetRequiredService<IDriver>().Session())
        .AddRazorPages();
}

And a trivial business object that will be wired up and injected into a PageModel class:还有一个琐碎的业务object 将被连接起来并注入到PageModel class 中:

public class SignupVendor
{
    private readonly ISession session;

    public SignupVendor(ISession session)
    {
        this.session = session;
    }

    public async Task RunAsync(string name)
    {
        using var transaction = await session.BeginTransactionAsync();

        try
        {
            var result = await transaction.RunAsync(
                "CREATE (v:Vendor {name:$name}) RETURN v", 
                new { name });

            await transaction.CommitAsync();
        }
        catch
        {
            await transaction.RollbackAsync();
            throw;
        }
    }
}

Can you confirm or comment on my assumptions?你能证实或评论我的假设吗?

Reference Neo4j 1.7 Driver Manual参考Neo4j 1.7驱动手册

Assumption 1: Driver is a lightweight protocol wrapper that would be safe in Singleton scope.假设 1:驱动程序是一个轻量级协议包装器,在 Singleton scope 中是安全的。

Short Answer: Yes简短的回答:是的

From Docs来自文档

The driver object驱动object

A Neo4j client application will require a driver instance in order to provide access to the database. Neo4j 客户端应用程序将需要驱动程序实例才能提供对数据库的访问。 This driver should be made available to all parts of the application that need to interact with Neo4j.此驱动程序应可用于需要与 Neo4j 交互的应用程序的所有部分。 In languages where thread safety is an issue, the driver can be considered thread-safe.在线程安全是一个问题的语言中,驱动程序可以被认为是线程安全的。

A note on lifecycle关于生命周期的说明

Applications will typically construct a driver instance on startup and destroy it on exit.应用程序通常会在启动时构建驱动程序实例并在退出时销毁它。 Destroying a driver instance will immediately shut down any connections previously opened via that driver;销毁驱动程序实例将立即关闭之前通过该驱动程序打开的所有连接; for drivers that contain a connection pool, the entire pool will be shut down.对于包含连接池的驱动程序,整个池将被关闭。

emphasis mine.强调我的。


Assumption 2: It might be useful to scope Session to the web request lifetime.假设 2:它可能对 scope Session 到 web 请求生命周期有用。 For instance, I might want to add to a transaction in some middle-ware.例如,我可能想在一些中间件中添加事务。

Short Answer: Yes简短的回答:是的

From the Docs从文档

Sessions会话

A session is a container for a sequence of transactions. session 是一系列事务的容器。 Sessions borrow connections from a pool as required and so should be considered lightweight and disposable.会话根据需要从池中借用连接,因此应被视为轻量级和一次性的。 In languages where thread safety is an issue, a session should not be considered thread-safe.在线程安全成为问题的语言中,不应将 session 视为线程安全的。 In languages that support them, sessions are usually scoped within a context block.在支持它们的语言中,会话通常位于上下文块内。 This ensures that they are properly closed and that any underlying connections are released and not leaked.这确保了它们被正确关闭,并且任何底层连接都被释放并且不会泄露。

emphasis mine强调我的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM