Database Reference
In-Depth Information
Listing 3.2. Creating single user node in Neo4j (Java 7/Neo4j 2.0.X style)
In both styles, the first step is to instantiate a Neo4j database . You provide the filesys-
tem location where the data is located as the constructor argument. If this represents an
existingNeo4jdatabase,thatdatabasewillbeused;ifthelocationdoesn'texist,Neo4jwill
create an empty database at that location. The database should be created only once and
reused for the application's lifecycle. In the following code examples, we'll assume that
we're reusing an already configured, but empty, Neo4j database instance.
We mentioned before that Neo4j is a fully ACID-compliant graph database, supporting all
standard transactional properties, as is expected from any other database. When using a
Neo4j-embeddeddatabase,thetransactionboundaryismanagedusingtheNeo4jCoreJava
API programmatically. Steps and in both listings (and and in listing 3.1 )
are part of the Neo4j API for transaction management. (Don't worry if you don't under-
standitallyet;we'lltakeanin-depthlookatNeo4jtransactions in chapter 7 .)AsallNeo4j
operations must be executed within a transaction, you'll see these same lines of code in all
code examples that write to Neo4j; without them, the code simply won't work. (In Neo4j
1.9 and below, transactions are only mandatory for operations that update the database, but
from 2.0 onward, this includes read operations as well.) For now, all you need to note is
that every block of code that accesses the Neo4j database is implemented within either a
try / catch / finally block(Neo4j1.9/Java6)ora try (trywithresource)block(Neo4j
2.0/Java 7), and that these patterns are used to handle the transaction boundary of the ex-
ecuted code.
Inbothapproaches,thecorepartoftheexampleisthecodewithinthe try block , .
We'll include the transaction management code in all the examples throughout this chapter
for consistency reasons, but we'll discuss Neo4j's transaction handling and API in more
detail in chapter 7 .
 
Search WWH ::




Custom Search