Best solution for multiple queries in a limited time

匆匆过客 提交于 2019-12-24 17:19:10

问题


For a MORPG Hack'n'Slash game i am currently using Neo4j with a pattern like this :

I have a Neo4J connector class, creating my connection and implementing Singleton and this instance is used by every xxxMapper classes, calling Neo4jConnetor.getInstance().query(String query) which returns the iterator of the queryresult.

Atm I'm asking myself a question, the game will have a ton of queries per second (like 5 per player per second). So I don't know, in terms of perfs, which pattern to use, if I should keep using my Singleton system or using another one like a pool of Neo4jConnector or anything else i don't know yet.

Here is the connector class :

public class Neo4jConnector{

    private String urlRest;
    private String url = "http://localhost:7474";
    protected QueryEngine<?> engine;
    protected static Neo4jConnector INSTANCE = new Neo4jConnector();

    private Neo4jConnector(){
        urlRest = url+"/db/data";
        final RestAPI graphDb = new RestAPIFacade(urlRest);
        engine = new RestCypherQueryEngine(graphDb);
    }

    public static Neo4jConnector getInstance(){
        if (INSTANCE == null)
        { 
            INSTANCE = new Neo4jConnector();
        }
        return INSTANCE;
    }

    @SuppressWarnings("unchecked")
    public Iterator<Map<String, Object>> query(String query){
        QueryResult<Map<String, Object>> row = (QueryResult<Map<String, Object>>) engine.query(query, Collections.EMPTY_MAP);
        return row.iterator();
    }
}

and an example call of this class :

Iterator<Map<String, Object>> iterator = Neo4jConnector.getInstance().query("optional Match(u:User{username:'"+username+"'}) return u.password as password, u.id as id");

回答1:


Neo4j's embedded GraphDatabaseService is not pooled and threadsafe.

I would not recommend RestGraphDatabase and friends, because it is slow and outdated.

Just use parameters instead of literal strings and don't use optional match to start a query.

If you look for faster access look into the JDBC driver (which will be updated soonish).



来源:https://stackoverflow.com/questions/29622464/best-solution-for-multiple-queries-in-a-limited-time

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!