Q:

How to connect to Neo4j before each request on Resteasy?

1) I'm using Resteasy to provide a RESTful Web service which gives access to a specific Neo4j graph database.
With Resteasy, this Web resource (GraphResource.java) looks like:
@Path("graph")
public class GraphResource {

    @GET
    @Path("users/{id}")
    @Produces(MediaType.APPLICATION_JSON)
    public String getUserInfos(@PathParam("id") String id) {
        // Search the database, get a string representation and return it
    }
}

I would like to get access to DB in the getUserInfos method. I know that I have to instantiate a Graph object (with Gremlin):
Graph graph = new Neo4jGraph("/tmp/neo4j");

... but I don't know where is the best place.
Do you think that PreProcessInterceptor could be useful? I never saw any example with DB connection in it.
2) Does the Graph object has to be statically defined? Should it be shared between all the requests? How to make two requests do not become entangled?

A:

I'm using Jersey but we are doing the same thing- exposing a REST service that uses neo4j under the covers. What I do is create a singleton with a getDb() method that basically gets a handle to the neo4j db. The db can be shared by multiple threads- you just need to make sure that you instantiate the same db multiple times.

