You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

46 lines
1.3 KiB

2 months ago
package com.dsideal.Res.Plugin;
2 months ago
import lombok.Getter;
2 months ago
import org.neo4j.driver.AuthTokens;
import org.neo4j.driver.Driver;
import org.neo4j.driver.GraphDatabase;
2 months ago
import org.neo4j.driver.Session;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
2 months ago
@Getter
public class Neo4jPlugin {
private static Neo4jPlugin instance;
2 months ago
private Driver driver;
2 months ago
private static final Logger logger = LoggerFactory.getLogger(Neo4jPlugin.class);
2 months ago
2 months ago
private Neo4jPlugin() {}
2 months ago
2 months ago
public static Neo4jPlugin getInstance() {
2 months ago
if (instance == null) {
2 months ago
instance = new Neo4jPlugin();
2 months ago
}
return instance;
}
2 months ago
public void init(String uri, String user, String password) {
2 months ago
try {
driver = GraphDatabase.driver(uri, AuthTokens.basic(user, password));
// 验证连接
try (Session session = driver.session()) {
session.run("RETURN 1").consume();
logger.info("Neo4j连接成功");
}
} catch (Exception e) {
logger.error("Neo4j初始化失败: " + e.getMessage(), e);
throw new RuntimeException("Neo4j初始化失败", e);
}
2 months ago
}
public void close() {
if (driver != null) {
driver.close();
}
}
}