如何在 Resteasy 上的每个请求之前连接到 Neo4j?
1) 我使用 Resteasy 提供 RESTful Web 服务,该服务允许访问特定的 Neo4j 图形数据库。
使用 Resteasy,此 Web 资源 (GraphResource.java) 如下所示:
@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
}
}
我想在 getUserInfos 方法中访问 DB。我知道我必须实例化一个 Graph 对象(使用 Gremlin):
Graph graph = new Neo4jGraph("/tmp/neo4j");
...但我不知道在哪里是最好的地方。
您认为 PreProcessInterceptor有用吗?我从未见过任何带有数据库连接的示例。
2)Graph对象是否必须静态定义?它应该在所有请求之间共享吗?如何让两个请求不纠缠在一起?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我正在使用 Jersey,但我们正在做同样的事情 - 公开在幕后使用 neo4j 的 REST 服务。我所做的是使用 getDb() 方法创建一个单例,该方法基本上获取 neo4j 数据库的句柄。数据库可以由多个线程共享 - 您只需要确保多次实例化同一个数据库即可。
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.