在 Neo4j-ogm 中是否有 API 来关闭会话?
Is there an API to close Session in Neo4j-ogm?
我正在使用 neo4j-ogm 1.1.4 版本。
由于我使用 org.springframework.data.neo4j.template.Neo4jTemplate 是我自己使用会话对象创建的,我想知道是否有一个合同,一旦我完成所有工作,我就必须将会话标记为关闭.
我遇到了这个link
http://inner-loop.github.io/java-neo4j-ogm/
但是我用的库好像没有关闭Session的方法class。我需要使用任何其他 API 来标记会话关闭吗?
技术上不需要 "close" Neo4j OGM 中的会话。它不代表与数据库的连接,而是维护您的应用程序和数据库之间的对话状态,允许 OGM 在您在 "unit of work"(由您的应用程序定义)中加载和保存对象时生成高效的 Cypher。
有两种方法可以破坏这种对话状态。从您的应用程序代码的角度来看,它们都具有相同的效果。
重用
session.clear()
允许您通过删除现有会话状态来重用现有会话对象。
替换
session = sessionFactory.openSession()
将用新对象替换任何当前的 session
对象。
这两个操作都会使 OGM 没有关于域对象相对于图形的同步状态的信息。 (在 Hibernate 术语中,它们处于 'detached' 状态)OGM 当前没有将域对象重新附加到新会话的机制,因此您应该 always 重新加载所有您要在新会话中使用的对象。
我需要在后端处理多个数据库连接。所以我实现了一个“有效”的 SPRING BOOT
无界池。这处理了@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);
}
}
}
我正在使用 neo4j-ogm 1.1.4 版本。 由于我使用 org.springframework.data.neo4j.template.Neo4jTemplate 是我自己使用会话对象创建的,我想知道是否有一个合同,一旦我完成所有工作,我就必须将会话标记为关闭.
我遇到了这个link
http://inner-loop.github.io/java-neo4j-ogm/
但是我用的库好像没有关闭Session的方法class。我需要使用任何其他 API 来标记会话关闭吗?
技术上不需要 "close" Neo4j OGM 中的会话。它不代表与数据库的连接,而是维护您的应用程序和数据库之间的对话状态,允许 OGM 在您在 "unit of work"(由您的应用程序定义)中加载和保存对象时生成高效的 Cypher。
有两种方法可以破坏这种对话状态。从您的应用程序代码的角度来看,它们都具有相同的效果。
重用
session.clear()
允许您通过删除现有会话状态来重用现有会话对象。
替换
session = sessionFactory.openSession()
将用新对象替换任何当前的 session
对象。
这两个操作都会使 OGM 没有关于域对象相对于图形的同步状态的信息。 (在 Hibernate 术语中,它们处于 'detached' 状态)OGM 当前没有将域对象重新附加到新会话的机制,因此您应该 always 重新加载所有您要在新会话中使用的对象。
我需要在后端处理多个数据库连接。所以我实现了一个“有效”的 SPRING BOOT
无界池。这处理了@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);
}
}
}