简体   繁体   English

Neo4j-ogm 中是否有关闭会话的 API?

[英]Is there an API to close Session in Neo4j-ogm?

I am using neo4j-ogm 1.1.4 version.我使用的是 neo4j-ogm 1.1.4 版本。 Since I use the org.springframework.data.neo4j.template.Neo4jTemplate which I create by myself using the session object, I am wondering is there a contract that once all my work is done, then I have to mark the session closed.由于我使用我自己使用会话对象创建的 org.springframework.data.neo4j.template.Neo4jTemplate,我想知道是否有一个合同,一旦我的所有工作完成,我就必须将会话标记为关闭。

I came across this link我遇到了这个链接

http://inner-loop.github.io/java-neo4j-ogm/ http://inner-loop.github.io/java-neo4j-ogm/

But the library I am using seems to have no close method on the Session class.但是我使用的库在 Session 类上似乎没有 close 方法。 Is there any other API I need to use to mark the session closed?我需要使用其他任何 API 来标记会话已关闭吗?

There is technically no need to "close" the Session in the Neo4j OGM.从技术上讲,没有必要在 Neo4j OGM 中“关闭”会话。 It does not represent a connection to the database, instead it maintains conversational state between your application and the database, allowing the OGM to generate efficient Cypher when you load and save objects within a "unit of work" (as defined by your application).它不代表与数据库的连接,而是维护应用程序和数据库之间的对话状态,当您在“工作单元”(由您的应用程序定义)中加载和保存对象时,允许 OGM 生成高效的 Cypher。

There are two ways to destroy this conversational state.有两种方法可以破坏这种对话状态。 They both have the same effect from the perspective of your application code.从您的应用程序代码的角度来看,它们都具有相同的效果。

reuse重用

session.clear() allows you to reuse the existing session object by deleting the existing conversational state. session.clear()允许您通过删除现有会话状态来重用现有会话对象。

replace替换

session = sessionFactory.openSession() will replace any current session object with a new one. session = sessionFactory.openSession()将用新的session对象替换任何当前session对象。

Both these operations will leave the OGM with no information about the synchronisation state of your domain objects vis-a-vis the graph.这两个操作都将使 OGM 不了解有关域对象相对于图形的同步状态的信息。 (In Hibernate terms, they are in a 'detached' state) The OGM currently has no mechanism to re-attach your domain objects to a new session, so you should always reload all the objects you want to use into the new session. (在 Hibernate 术语中,它们处于“分离”状态)OGM 当前没有将域对象重新附加到新会话的机制,因此您应该始终将要使用的所有对象重新加载到新会话中。

I had a requirement to handle multiples database connections in my backend.我需要在后端处理多个数据库连接。 So I implemented a SPRING BOOT unbounded pool that "works".所以我实现了一个“有效”的SPRING BOOT无界池。 This handles the clear mentioned in the @Vince answer;这处理了@Vince 回答中提到的clear

@Component
public class Neo4jOGMSessionPool implements ApplicationListener<RequestHandledEvent> {

  private @Value("${neo4j.uri}") String uri;
  private @Value("${neo4j.username}") String username;
  private @Value("${neo4j.password}") String password;

  private final Map<String, LinkedBlockingQueue<Session>> queues = new ConcurrentHashMap<>();
  private final ThreadLocal<Entry<String, Session>> threadLocal = new ThreadLocal<>();

  public synchronized Session getSession(String databaseName) {
    LinkedBlockingQueue<Session> queue = queues.computeIfAbsent(databaseName, k -> new LinkedBlockingQueue<>());
    Session session = queue.poll();
    if (session == null) {
      Configuration configuration = new Configuration.Builder().uri(uri).credentials(username, password).database(databaseName).verifyConnection(true).build();
      SessionFactory sessionFactory = new SessionFactory(configuration, "no.package");
      session = sessionFactory.openSession();
    }
    threadLocal.set(Maps.immutableEntry(databaseName, session));
    return session;
  }

  @Override
  public void onApplicationEvent(RequestHandledEvent event) {
    Entry<String, Session> entry = threadLocal.get();
    if (entry != null) {
      Session session = entry.getValue();
      session.clear();
      threadLocal.remove();
      queues.get(entry.getKey()).add(session);
    }
  }

}

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

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