public interface QueryRunner extends AutoCloseable
queries run in the same QueryRunner
are guaranteed
to execute in order, meaning changes made by one query will be seen
by all subsequent queries in the same QueryRunner
.
However, to allow handling very large results, and to improve performance,
result streams are retrieved lazily from the network.
This means that when any of run(Query)
methods return a result, the query has only started executing - it may not
have completed yet. Most of the time, you will not notice this, because the
driver automatically waits for queries to complete at specific points to
fulfill its contracts.
Specifically, the driver will ensure all outstanding queries are completed whenever you:
Result.next()
or Result.consume()
Transaction.close()
Session.close()
As noted, most of the time, you will not need to consider this - your writes will
always be durably stored as long as you either use the results, explicitly commit
transactions
or close the session you used using Session.close()
.
While these semantics introduce some complexity, it gives the driver the ability to handle infinite result streams (like subscribing to events), significantly lowers the memory overhead for your application and improves performance.
Session
,
Transaction
Modifier and Type | Method and Description |
---|---|
Result |
run(Query query)
Run a query and return a result stream.
|
Result |
run(String query)
Run a query and return a result stream.
|
Result |
run(String query,
Map<String,Object> parameters)
Run a query and return a result stream.
|
Result |
run(String query,
Record parameters)
Run a query and return a result stream.
|
Result |
run(String query,
Value parameters)
Run a query and return a result stream.
|
close
Result run(String query, Value parameters)
This method takes a set of parameters that will be injected into the query by Neo4j. Using parameters is highly encouraged, it helps avoid dangerous cypher injection attacks and improves database performance as Neo4j can re-use query plans more often.
This particular method takes a Value
as its input. This is useful
if you want to take a map-like value that you've gotten from a prior result
and send it back as parameters.
If you are creating parameters programmatically, run(String, Map)
might be more helpful, it converts your map to a Value
for you.
Result result = session.run( "MATCH (n) WHERE n.name = $myNameParam RETURN (n)",
Values.parameters( "myNameParam", "Bob" ) );
query
- text of a Neo4j queryparameters
- input parameters, should be a map Value, see Values.parameters(Object...)
.Result run(String query, Map<String,Object> parameters)
This method takes a set of parameters that will be injected into the query by Neo4j. Using parameters is highly encouraged, it helps avoid dangerous cypher injection attacks and improves database performance as Neo4j can re-use query plans more often.
This version of run takes a Map
of parameters. The values in the map
must be values that can be converted to Neo4j types. See Values.parameters(Object...)
for
a list of allowed types.
Map<String, Object> parameters = new HashMap<String, Object>();
parameters.put("myNameParam", "Bob");
Result result = session.run( "MATCH (n) WHERE n.name = $myNameParam RETURN (n)",
parameters );
query
- text of a Neo4j queryparameters
- input data for the queryResult run(String query, Record parameters)
This method takes a set of parameters that will be injected into the query by Neo4j. Using parameters is highly encouraged, it helps avoid dangerous cypher injection attacks and improves database performance as Neo4j can re-use query plans more often.
This version of run takes a Record
of parameters, which can be useful
if you want to use the output of one query as input for another.
query
- text of a Neo4j queryparameters
- input data for the queryResult run(String query)
query
- text of a Neo4j queryResult run(Query query)
Query query = new Query( "MATCH (n) WHERE n.name = $myNameParam RETURN n.age" );
Result result = session.run( query.withParameters( Values.parameters( "myNameParam", "Bob" ) ) );
query
- a Neo4j query