Supamiu
Supamiu

Reputation: 8731

Best solution for multiple queries in a limited time

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");

Upvotes: 1

Views: 94

Answers (1)

Michael Hunger
Michael Hunger

Reputation: 41676

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).

Upvotes: 2

Related Questions