Database Reference
In-Depth Information
7.1.2. Finishing what you start and not trying to do too much in one go
Creating a transaction in a try block, as we said previously, guarantees that transactions
are finished appropriately in a timely manner. The stateful nature of transactions requires
thatyougiveconsiderationtotheamountofworkyouattempttodoinasingletransaction.
This only really becomes an issue for very large transactions, including large inserts or up-
dates, such as the one shown in the following listing. When executed, this code will likely
fail with an out-of-memory exception, depending on the amount of memory available for
the JVM.
Listing 7.3. A really big transaction can run out of memory
try(Transaction tx = graphDatabaseService.beginTx()) {
for (int i = 0; i < 100000000; i++) {
Node n = this.graphDatabaseService.createNode();
n.setProperty("number", i);
}
tx.success();
}
java.lang.OutOfMemoryError: Java heap space
at java.util.HashMap.addEntry(HashMap.java:766)
at java.util.HashMap.put(HashMap.java:402)
at java.util.HashSet.add(HashSet.java:217)
at org.neo4j.kernel.impl.api.DiffSets.add(DiffSets.java:98)
at ....
In order to avoid out-of-memory exceptions in very large transactions, the work should be
broken down into a number of smaller transactions. It's not, however, generally advisable
to create a large number of nodes using a separate transaction for each new node because
the computational cost of the transaction will have a significant impact on performance. A
better strategy would be to do some hundreds or perhaps thousands of creation operations
in a batch with one transaction. The exact number will depend on the amount of data in-
volved in creating each node.
The previous example conflates the server's and the client's behavior, because of its use of
the embedded mode. In a more traditional client/server scenario, the pressure of handling
large transactions mainly affects the server, because it's the server that needs to keep track
of all uncommitted transactions started by a potentially large number of clients.
Batch inserter and transactions
Search WWH ::




Custom Search