|
|
|
@ -1,14 +1,14 @@
|
|
|
|
|
package com.dsideal.Res.Plugin;
|
|
|
|
|
|
|
|
|
|
import lombok.Getter;
|
|
|
|
|
import org.neo4j.driver.AuthTokens;
|
|
|
|
|
import org.neo4j.driver.Driver;
|
|
|
|
|
import org.neo4j.driver.GraphDatabase;
|
|
|
|
|
|
|
|
|
|
import org.neo4j.driver.Session;
|
|
|
|
|
import org.neo4j.driver.*;
|
|
|
|
|
import org.neo4j.driver.async.AsyncSession;
|
|
|
|
|
import org.neo4j.driver.Config;
|
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
|
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
|
|
|
|
@Getter
|
|
|
|
|
public class Neo4jPlugin {
|
|
|
|
|
private static Neo4jPlugin instance;
|
|
|
|
@ -26,11 +26,23 @@ public class Neo4jPlugin {
|
|
|
|
|
|
|
|
|
|
public void init(String uri, String user, String password) {
|
|
|
|
|
try {
|
|
|
|
|
driver = GraphDatabase.driver(uri, AuthTokens.basic(user, password));
|
|
|
|
|
// 配置连接池
|
|
|
|
|
Config config = Config.builder()
|
|
|
|
|
.withMaxConnectionLifetime(30, TimeUnit.MINUTES) // 连接最大生命周期
|
|
|
|
|
.withMaxConnectionPoolSize(50) // 连接池最大连接数
|
|
|
|
|
.withConnectionAcquisitionTimeout(5, TimeUnit.SECONDS) // 获取连接超时时间
|
|
|
|
|
.withConnectionTimeout(5, TimeUnit.SECONDS) // 连接超时时间
|
|
|
|
|
.withMaxTransactionRetryTime(15, TimeUnit.SECONDS) // 事务重试最大时间
|
|
|
|
|
.withFetchSize(1000) // 每次查询的默认获取数量
|
|
|
|
|
.build();
|
|
|
|
|
|
|
|
|
|
// 创建带连接池配置的驱动实例
|
|
|
|
|
driver = GraphDatabase.driver(uri, AuthTokens.basic(user, password), config);
|
|
|
|
|
|
|
|
|
|
// 验证连接
|
|
|
|
|
try (Session session = driver.session()) {
|
|
|
|
|
session.run("RETURN 1").consume();
|
|
|
|
|
logger.info("Neo4j连接成功");
|
|
|
|
|
logger.info("Neo4j连接池初始化成功");
|
|
|
|
|
}
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
logger.error("Neo4j初始化失败: " + e.getMessage(), e);
|
|
|
|
@ -38,6 +50,23 @@ public class Neo4jPlugin {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Session getSession() {
|
|
|
|
|
if (driver == null) {
|
|
|
|
|
throw new IllegalStateException("Neo4j驱动未初始化");
|
|
|
|
|
}
|
|
|
|
|
return driver.session(SessionConfig.builder()
|
|
|
|
|
.withDefaultAccessMode(AccessMode.WRITE) // 默认访问模式
|
|
|
|
|
.withDatabase("neo4j") // 指定数据库名称
|
|
|
|
|
.build());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public AsyncSession getAsyncSession() {
|
|
|
|
|
if (driver == null) {
|
|
|
|
|
throw new IllegalStateException("Neo4j驱动未初始化");
|
|
|
|
|
}
|
|
|
|
|
return driver.session(AsyncSession.class);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void close() {
|
|
|
|
|
if (driver != null) {
|
|
|
|
|
driver.close();
|
|
|
|
|