public interface Driver extends AutoCloseable
sessions
to run statements against the database.
An example:
// Create a driver with default configuration
Driver driver = GraphDatabase.driver( "bolt://localhost:7687" );
// Establish a session
Session session = driver.session();
// Running a simple statement can be done like this
session.run( "CREATE (n {name:'Bob'})" );
// Or, run multiple statements together in an atomic transaction:
try( Transaction tx = session.beginTransaction() )
{
tx.run( "CREATE (n {name:'Alice'})" );
tx.run( "CREATE (n {name:'Tina'})" );
tx.success();
}
// Retrieve results
StatementResult result = session.run( "MATCH (n) RETURN n.name" );
List<String> names = new LinkedList<>();
while( result.hasNext() )
{
names.add( result.next().get("n.name").asString() );
}
// Sessions are pooled, to avoid the overhead of creating new connections - this means
// it is very important to close your session when you are done with it, otherwise you will
// run out of sessions.
session.close();
// And, to clean up resources, always close the driver when your application is done
driver.close();
A driver maintains a connection pool for each Neo4j instance. For resource efficiency reasons you are encouraged
to use the same driver instance across your application. You can control the connection pooling behavior when you
create the driver using the Config
you pass into GraphDatabase.driver(URI, Config)
.
Session session()
a statement
or
a transaction
.void close() throws Neo4jException
close
in interface AutoCloseable
Neo4jException
- any error that might happen when releasing all resourcesCopyright © 2016. All rights reserved.