Перечисление всех вершин определенного класса в OrientDB
Недавно я начал изучать базы данных Graph (в частности, Neo4j и OrientDB) и столкнулся с проблемой, на которую, похоже, не могу понять.
Я использую локальную установку OrientDB (сервер OrientDB v2.0-M3 активен). Я использую Tinkerpops для подключения и выполнения запросов к графику. Я использую Java и Spring на локальном сервере Tomcat 7. Тестируя мой API, я использую Postman в Chrome.
Вот мой ошибочный метод GET:
@RequestMapping(value = "/articles", method = RequestMethod.GET)
public
@ResponseBody
Vector<Article> list() {
OrientGraph graph = new OrientGraph("remote:/local/path/to/orientdb/databases/mydb", "user", "password");
FramedGraphFactory factory = new FramedGraphFactory();
FramedGraph manager = factory.create(graph);
Vector<Article> articles = new Vector<>();
try {
Iterable<Vertex> vertices = graph.getVerticesOfClass("Article", false);
Iterator<Vertex> it = vertices.iterator();
if (it.hasNext()) {
do {
Article a = (Article) manager.frame(it.next(), Article.class);
articles.add(a);
} while (it.hasNext());
}
} catch (Exception e) {
e.printStackTrace();
} finally {
graph.shutdown();
}
return articles;
}
Это приводит к следующей ошибке:
{
"timestamp": 1418562889304,
"status": 500,
"error": "Internal Server Error",
"exception": "org.springframework.http.converter.HttpMessageNotWritableException",
"message": "Could not write JSON: Database instance is not set in current thread. Assure to set it with: ODatabaseRecordThreadLocal.INSTANCE.set(db); (through reference chain: java.util.Vector[0]->$Proxy43[\"name\"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: Database instance is not set in current thread. Assure to set it with: ODatabaseRecordThreadLocal.INSTANCE.set(db); (through reference chain: java.util.Vector[0]->$Proxy43[\"name\"])",
"path": "/articles"
}
Я пытался понять это, пытаясь исправить ситуацию, которую предлагает ошибка. Я также пытался использовать TransactionalGraph вместо OrientGraph.
Вот подвох... Я также использую аналогичный метод для получения одного ресурса. Этот метод работает, только если я использую "System.out.println", в противном случае он завершается с той же ошибкой.
@RequestMapping(value = "/article", method = RequestMethod.GET)
public
@ResponseBody
Article get(
@RequestParam(value = "number", required = true) long number
) {
TransactionalGraph graph = new OrientGraph("remote:/path/to/local/orientdb/orientdb/databases/mydb", "user", "password");
FramedGraphFactory factory = new FramedGraphFactory();
FramedGraph manager = factory.create(graph);
Article article = null;
try {
Iterable<Article> articles = manager.getVertices("number", number, Article.class);
article = articles.iterator().next();
System.out.println(article.getName());
} catch (Exception e) {
e.printStackTrace();
} finally {
graph.shutdown();
}
return article;
}
Любая помощь приветствуется!
1 ответ
Вы должны оставить график (= соединение) открытым, пока вы используете результат. Вы можете переместить graph.shutdown()
после просмотра вашего набора результатов?