简体   繁体   English

在Jersey REST和Neo4j环境中并发POST请求的问题

[英]Issue with concurrent POST requests with Jersey REST and Neo4j environment

I have Jersery Rest service and i am using Neo4j Embedded Database to serve requests with data. 我有Jersery Rest服务,并且我正在使用Neo4j嵌入式数据库来处理数据请求。

Now when i make concurrent GET requests it works fine. 现在,当我发出并发GET请求时,它可以正常工作。

But when i make concurrent POST requests It gives Following Exception : 但是当我发出并发的POST请求时,它给出了以下异常:

Caused by: org.neo4j.kernel.lifecycle.LifecycleException: Component 'org.neo4j.kernel.StoreLockerLifecycleAdapter@62f1ca5e' was successfully initialized, but failed to start. Please see attached cause exception.
    at org.neo4j.kernel.lifecycle.LifeSupport$LifecycleInstance.start(LifeSupport.java:504)
    at org.neo4j.kernel.lifecycle.LifeSupport.start(LifeSupport.java:115)
    at org.neo4j.kernel.InternalAbstractGraphDatabase.run(InternalAbstractGraphDatabase.java:296)
    ... 42 more
Caused by: org.neo4j.kernel.StoreLockException: Could not create lock file
    at org.neo4j.kernel.StoreLocker.checkLock(StoreLocker.java:85)
    at org.neo4j.kernel.StoreLockerLifecycleAdapter.start(StoreLockerLifecycleAdapter.java:40)
    at org.neo4j.kernel.lifecycle.LifeSupport$LifecycleInstance.start(LifeSupport.java:498)

I know the problem is if there is already instance of neo4j running we can't access the same with another thread. 我知道问题在于,如果已经有neo4j实例在运行,则无法通过其他线程访问相同的实例。

Solution would be Neo4j HA 解决方案将是Neo4j HA

But as i dont have enough time to configure Neo4j HA cluster, can anyone please suggest me on How can i make POST request to be single threaded (means no concurrent threads accessing Neo4j at the same time). 但是由于我没有足够的时间来配置Neo4j HA群集,因此有人可以在如何使POST请求成为单线程方面提出建议吗(意味着没有并发线程同时访问Neo4j)。

Any link or tutorial. 任何链接或教程。

Edit 编辑

I am starting neo4j like this: 我是这样开始neo4j的:

GraphDatabaseService graphdb = new GraphDatabaseFactory().newEmbeddedDatabase("D:/GraphDB");'

and in finally block i am doing finally我正在做

graphdb.shutdown();

Not sure how GET requests are working in this case, perhaps someone from Neo4j team could best provide for an explanation. 不确定GET请求在这种情况下如何工作,也许Neo4j团队的某人最好提供一个解释。 I'm assuming that you are initializing and shutting graphdb with each request. 我假设您正在初始化和关闭每个请求的graphdb Also, I haven't been in touch with Java lately, my suggestion may not be accurate. 另外,我最近还没有接触过Java,我的建议可能不准确。

The way I think I would deal with this is by not initializing and shutting graphdb with each request. 我认为我要处理的方式是通过每个请求初始化和关闭graphdb The reason being that Neo4j locks on a graph database directory and won't allow a new thread to re-initialize an embedded instance on the same directory (read thread-safe). 原因是Neo4j锁定在图形数据库目录上,并且不允许新线程重新初始化同一目录上的嵌入式实例(读取线程安全)。 Instead, I'd rather use a singleton class which exposed graphdb and re-use it as a shared object in the JVM. 相反,我宁愿使用单例类,该类公开graphdb并将其作为JVM中的共享对象使用。 graphdb.shutdown() could occur in a shutdown hook for your Jersey service ( graphdb shuts down only when the service goes down). graphdb.shutdown()可能会出现在Jersey服务的关闭挂钩中( graphdb仅在服务关闭时关闭)。 This aligns with what Pangea suggested, ServletContextListener is a good place to do this. 这与Pangea的建议一致, ServletContextListener是执行此操作的好地方。 However, implementation may vary with Jersey. 但是,实施方式可能因泽西岛而异。

Finally, I'd be polite with people who are trying to help out by editing your question, commenting and asking for context around your problem. 最后,我会礼貌地尝试通过编辑您的问题,发表评论并询问有关您问题的背景来寻求帮助的人们。 :) :)

too late to answer this but again if anyone is facing the same issue ,this is how i fixed it. 回答这个问题为时已晚,但是如果有人遇到同样的问题,这就是我如何解决的问题。 Maintain a singleton class. 保持单例课程。 ie,

      GraphDatabaseService graphdb=null;
      if(graphdb.isAvailable()&& graphdb!=null)
       {
            return graphdb;
       }
       else
        {
           graphdb = new GraphDatabaseFactory().newEmbeddedDatabase("D:/GraphDB");
         return graphdb;
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM