Listing all Vertices of specific class in OrientDB

672 views Asked by At

I've recently started exploring Graph databases (in particular Neo4j and OrientDB) and have come across a problem I can't seem to put my finger on.

I'm running a local installation of OrientDB (OrientDB Server v2.0-M3 is active.). I'm using Tinkerpops to connect to, and run queries against, the graph. I'm using Java and Spring on a local Tomcat 7 server. Testing my API I'm using Postman on Chrome.

Here's my faulty GET method:

@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;
}

This generates the following error:

{
"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"
}

I've been trying to figure this out, trying the "fix" that the error suggests. I've also tried to use TransactionalGraph instead of OrientGraph.

Here's the catch... I'm also using a similar method for getting a single resource. This method only works if I'm using the "System.out.println", otherwise it fails with the same error.

@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;
}

Any help appreciated!

1

There are 1 answers

2
Lvca On

You should leave the graph (=connection) open while you're using the result. Can you move the graph.shutdown() after browsing your result set?