Interface Transaction
- All Superinterfaces:
AutoCloseable
All database operations that access the graph, indexes, or the schema must be performed in a transaction.
If you attempt to access the graph outside of a transaction, those operations will throw
NotInTransactionException
.
Here's the idiomatic use of programmatic transactions in Neo4j:
try ( Transaction tx = graphDb.beginTx() )
{
// operations on the graph
// ...
tx.commit();
}
Let's walk through this example line by line. First we retrieve a Transaction
object by invoking the GraphDatabaseService.beginTx()
factory method.
This creates a new transaction which has internal state to keep
track of whether the current transaction is successful. Then we wrap all
operations that modify the graph in a try-finally block with the transaction
as resource. At the end of the block, we invoke the tx.commit()
method to commit that the transaction.
If an exception is raised in the try-block, commit()
will never be
invoked and the transaction will be roll backed. This is very important:
unless commit()
is invoked, the transaction will fail upon
close()
. A transaction can be explicitly rolled back by
invoking the rollback()
method.
Read operations inside of a transaction will also read uncommitted data from the same transaction.
All ResourceIterables
that were returned from operations executed inside a transaction
will be automatically closed when the transaction is committed or rolled back.
Note however, that the ResourceIterator
should be closed
as soon as
possible if you don't intend to exhaust the iterator.
Note that transactions should be used by a single thread only. It is generally not safe to use a transaction from multiple threads. Doing so will lead to undefined behavior.
-
Method Summary
Modifier and TypeMethodDescriptionacquireReadLock
(Entity entity) Acquires a read lock forentity
for this transaction.acquireWriteLock
(Entity entity) Acquires a write lock forentity
for this transaction.Factory method for bidirectional traversal descriptions.void
close()
Close transaction.void
commit()
Commit and close current transaction.Creates a new node.createNode
(Label... labels) Creates a new node and adds the provided labels to it.Executes a query and returns an iterable that contains the result set.Executes a query and returns an iterable that contains the result set.Equivalent tofindNodes(Label, String, Object)
, however it must find no more than onenode
or it will throw an exception.Returns all nodes having the label, and the wanted property value.Returns all nodes having the label, and the wanted property values.findNodes
(Label label, String key1, Object value1, String key2, Object value2, String key3, Object value3) Returns all nodes having the label, and the wanted property values.findNodes
(Label label, String key, String template, StringSearchMode searchMode) Returns all nodes having a given label, and a property value of type String or Character matching the given value template and search mode.Returns all nodes having the label, and the wanted property values.findRelationship
(RelationshipType relationshipType, String key, Object value) Equivalent tofindRelationships(RelationshipType, String, Object)
, however it must find no more than onerelationship
or it will throw an exception.findRelationships
(RelationshipType relationshipType) Returns allrelationships
of a specifictype
.findRelationships
(RelationshipType relationshipType, String key, Object value) Returns allrelationships
having thetype
, and the wanted property value.findRelationships
(RelationshipType relationshipType, String key1, Object value1, String key2, Object value2) Returns allrelationships
having thetype
, and the wanted property values.findRelationships
(RelationshipType relationshipType, String key1, Object value1, String key2, Object value2, String key3, Object value3) Returns allrelationships
having thetype
, and the wanted property values.findRelationships
(RelationshipType relationshipType, String key, String template, StringSearchMode searchMode) Returns allrelationships
having thetype
, and a property value of type String or Character matching the given value template and search mode.findRelationships
(RelationshipType relationshipType, Map<String, Object> propertyValues) Returns allrelationships
having thetype
, and the wanted property values.Returns all labels currently in the underlying store.Returns all labels currently in the underlying store.Returns all nodes in the graph.Returns all property keys currently in the underlying store.Returns all relationships in the graph.Returns all relationship types currently in the underlying store.Returns all relationship types currently in the underlying store.getNodeByElementId
(String elementId) Looks up a node by element id.getNodeById
(long id) Deprecated, for removal: This API element is subject to removal in a future version.getRelationshipByElementId
(String elementId) Looks up a relationship by element id.getRelationshipById
(long id) Deprecated, for removal: This API element is subject to removal in a future version.in favor ofgetRelationshipByElementId(String)
.void
rollback()
Roll back and close current transaction.schema()
Returns theschema manager
where all things related to schema, for example constraints and indexing onlabels
.void
Marks this transaction as terminated, which means that it will be, much like in the case of failure, unconditionally rolled back whenclose()
is called.Factory method for unidirectional traversal descriptions.
-
Method Details
-
createNode
Node createNode()Creates a new node.- Returns:
- the created node.
-
createNode
Creates a new node and adds the provided labels to it.- Parameters:
labels
-labels
to add to the created node.- Returns:
- the created node.
-
getNodeById
Deprecated, for removal: This API element is subject to removal in a future version.in favor ofgetNodeByElementId(String)
.Looks up a node by id. Please note: Neo4j reuses its internal ids when nodes and relationships are deleted, which means it's bad practice to refer to them this way. Instead, use application generated ids.- Parameters:
id
- the id of the node- Returns:
- the node with id
id
if found - Throws:
NotFoundException
- if not found
-
getNodeByElementId
Looks up a node by element id. Please note: Neo4j reuses its internal ids when nodes and relationships are deleted, which means it's bad practice to refer to them this way. Instead, use application generated ids.- Parameters:
elementId
- the id of the node- Returns:
- the node with id
id
if found - Throws:
NotFoundException
- if not found
-
getRelationshipById
Deprecated, for removal: This API element is subject to removal in a future version.in favor ofgetRelationshipByElementId(String)
.Looks up a relationship by id. Please note: Neo4j reuses its internal ids when nodes and relationships are deleted, which means it's bad practice to refer to them this way. Instead, use application generated ids.- Parameters:
id
- the id of the relationship- Returns:
- the relationship with id
id
if found - Throws:
NotFoundException
- if not found
-
getRelationshipByElementId
Looks up a relationship by element id. Please note: Neo4j reuses its internal ids when nodes and relationships are deleted, which means it's bad practice to refer to them this way. Instead, use application generated ids.- Parameters:
elementId
- the id of the relationship- Returns:
- the relationship with id
id
if found - Throws:
NotFoundException
- if not found
-
bidirectionalTraversalDescription
BidirectionalTraversalDescription bidirectionalTraversalDescription()Factory method for bidirectional traversal descriptions.- Returns:
- a new
BidirectionalTraversalDescription
-
traversalDescription
TraversalDescription traversalDescription()Factory method for unidirectional traversal descriptions.- Returns:
- a new
TraversalDescription
-
execute
Executes a query and returns an iterable that contains the result set. This method is the same asexecute(String, java.util.Map)
with an empty parameters-map.- Parameters:
query
- The query to execute- Returns:
- A
Result
that contains the result set. - Throws:
QueryExecutionException
- If the Query contains errors
-
execute
Executes a query and returns an iterable that contains the result set.- Parameters:
query
- The query to executeparameters
- Parameters for the query- Returns:
- A
Result
that contains the result set - Throws:
QueryExecutionException
- If the Query contains errors
-
getAllLabelsInUse
Returns all labels currently in the underlying store. Labels are added to the store the first time they are used. This method guarantees that it will return all labels currently in use. Please take care that the returnedResourceIterable
is closed correctly and as soon as possible inside your transaction to avoid potential blocking of write operations.- Returns:
- all labels in the underlying store.
-
getAllRelationshipTypesInUse
Iterable<RelationshipType> getAllRelationshipTypesInUse()Returns all relationship types currently in the underlying store. Relationship types are added to the underlying store the first time they are used in a successfully committednode.createRelationshipTo(...)
. This method guarantees that it will return all relationship types currently in use.- Returns:
- all relationship types in the underlying store
-
getAllLabels
Returns all labels currently in the underlying store. Labels are added to the store the first time they are used. This method guarantees that it will return all labels currently in use. However, it may also return more than that (e.g. it can return "historic" labels that are no longer used). Please take care that the returnedResourceIterable
is closed correctly and as soon as possible inside your transaction to avoid potential blocking of write operations.- Returns:
- all labels in the underlying store.
-
getAllRelationshipTypes
Iterable<RelationshipType> getAllRelationshipTypes()Returns all relationship types currently in the underlying store. Relationship types are added to the underlying store the first time they are used in a successfully committednode.createRelationshipTo(...)
. Note that this method is guaranteed to return all known relationship types, but it does not guarantee that it won't return more than that (e.g. it can return "historic" relationship types that no longer have any relationships in the node space).- Returns:
- all relationship types in the underlying store
-
getAllPropertyKeys
Returns all property keys currently in the underlying store. This method guarantees that it will return all property keys currently in use. However, it may also return more than that (e.g. it can return "historic" labels that are no longer used). Please take care that the returnedResourceIterable
is closed correctly and as soon as possible inside your transaction to avoid potential blocking of write operations.- Returns:
- all property keys in the underlying store.
-
findNodes
ResourceIterator<Node> findNodes(Label label, String key, String template, StringSearchMode searchMode) Returns all nodes having a given label, and a property value of type String or Character matching the given value template and search mode.If an online index is found, it will be used to look up the requested nodes. If no indexes exist for the label/property combination, the database will scan all labeled nodes looking for matching property values.
The search mode and value template are used to select nodes of interest. The search mode can be one of
- EXACT: The value has to match the template exactly. This is the same behavior as
findNode(Label, String, Object)
. - PREFIX: The value must have a prefix matching the template.
- SUFFIX: The value must have a suffix matching the template.
- CONTAINS: The value must contain the template. Only exact matches are supported.
Please ensure that the returned
ResourceIterator
is closed correctly and as soon as possible inside your transaction to avoid potential blocking of write operations.- Parameters:
label
- consider nodes with this labelkey
- required property keytemplate
- required property value templatesearchMode
- search mode to use for finding matches- Returns:
- an iterator containing all matching nodes. See
ResourceIterator
for responsibilities.
- EXACT: The value has to match the template exactly. This is the same behavior as
-
findNodes
Returns all nodes having the label, and the wanted property values. If an online index is found, it will be used to look up the requested nodes.If no indexes exist for the label with all provided properties, the database will scan all labeled nodes looking for matching nodes.
Note that equality for values do not follow the rules of Java. This means that the number 42 is equals to all other 42 numbers, regardless of whether they are encoded as Integer, Long, Float, Short, Byte or Double.
Same rules follow Character and String - the Character 'A' is equal to the String 'A'.
Finally - arrays also follow these rules. An int[] {1,2,3} is equal to a double[] {1.0, 2.0, 3.0}
Please ensure that the returned
ResourceIterator
is closed correctly and as soon as possible inside your transaction to avoid potential blocking of write operations.- Parameters:
label
- consider nodes with this labelpropertyValues
- required property key-value combinations- Returns:
- an iterator containing all matching nodes. See
ResourceIterator
for responsibilities.
-
findNodes
ResourceIterator<Node> findNodes(Label label, String key1, Object value1, String key2, Object value2, String key3, Object value3) Returns all nodes having the label, and the wanted property values. If an online index is found, it will be used to look up the requested nodes.If no indexes exist for the label with all provided properties, the database will scan all labeled nodes looking for matching nodes.
Note that equality for values do not follow the rules of Java. This means that the number 42 is equals to all other 42 numbers, regardless of whether they are encoded as Integer, Long, Float, Short, Byte or Double.
Same rules follow Character and String - the Character 'A' is equal to the String 'A'.
Finally - arrays also follow these rules. An int[] {1,2,3} is equal to a double[] {1.0, 2.0, 3.0}
Please ensure that the returned
ResourceIterator
is closed correctly and as soon as possible inside your transaction to avoid potential blocking of write operations.- Parameters:
label
- consider nodes with this labelkey1
- required property key1value1
- required property value of key1key2
- required property key2value2
- required property value of key2key3
- required property key3value3
- required property value of key3- Returns:
- an iterator containing all matching nodes. See
ResourceIterator
for responsibilities.
-
findNodes
ResourceIterator<Node> findNodes(Label label, String key1, Object value1, String key2, Object value2) Returns all nodes having the label, and the wanted property values. If an online index is found, it will be used to look up the requested nodes.If no indexes exist for the label with all provided properties, the database will scan all labeled nodes looking for matching nodes.
Note that equality for values do not follow the rules of Java. This means that the number 42 is equals to all other 42 numbers, regardless of whether they are encoded as Integer, Long, Float, Short, Byte or Double.
Same rules follow Character and String - the Character 'A' is equal to the String 'A'.
Finally - arrays also follow these rules. An int[] {1,2,3} is equal to a double[] {1.0, 2.0, 3.0}
Please ensure that the returned
ResourceIterator
is closed correctly and as soon as possible inside your transaction to avoid potential blocking of write operations.- Parameters:
label
- consider nodes with this labelkey1
- required property key1value1
- required property value of key1key2
- required property key2value2
- required property value of key2- Returns:
- an iterator containing all matching nodes. See
ResourceIterator
for responsibilities.
-
findNode
Equivalent tofindNodes(Label, String, Object)
, however it must find no more than onenode
or it will throw an exception.- Parameters:
label
- consider nodes with this labelkey
- required property keyvalue
- required property value- Returns:
- the matching node or
null
if none could be found - Throws:
MultipleFoundException
- if more than one matchingnode
is found
-
findNodes
Returns all nodes having the label, and the wanted property value. If an online index is found, it will be used to look up the requested nodes.If no indexes exist for the label/property combination, the database will scan all labeled nodes looking for the property value.
Note that equality for values do not follow the rules of Java. This means that the number 42 is equals to all other 42 numbers, regardless of whether they are encoded as Integer, Long, Float, Short, Byte or Double.
Same rules follow Character and String - the Character 'A' is equal to the String 'A'.
Finally - arrays also follow these rules. An int[] {1,2,3} is equal to a double[] {1.0, 2.0, 3.0}
Please ensure that the returned
ResourceIterator
is closed correctly and as soon as possible inside your transaction to avoid potential blocking of write operations.- Parameters:
label
- consider nodes with this labelkey
- required property keyvalue
- required property value- Returns:
- an iterator containing all matching nodes. See
ResourceIterator
for responsibilities.
-
findNodes
Returns allnodes
with a specificlabel
. Please take care that the returnedResourceIterator
is closed correctly and as soon as possible inside your transaction to avoid potential blocking of write operations.- Parameters:
label
- theLabel
to return nodes for.- Returns:
- an iterator containing all nodes matching the label. See
ResourceIterator
for responsibilities.
-
findRelationships
ResourceIterator<Relationship> findRelationships(RelationshipType relationshipType, String key, String template, StringSearchMode searchMode) Returns allrelationships
having thetype
, and a property value of type String or Character matching the given value template and search mode.If an online index is found, it will be used to look up the requested relationships. If no indexes exist for the type/property combination, the database will scan all relationships of a specific type looking for matching property values.
The search mode and value template are used to select relationships of interest. The search mode can be one of
- EXACT: The value has to match the template exactly. This is the same behavior
as
findRelationships(RelationshipType, String, Object)
. - PREFIX: The value must have a prefix matching the template.
- SUFFIX: The value must have a suffix matching the template.
- CONTAINS: The value must contain the template. Only exact matches are supported.
Please ensure that the returned
ResourceIterator
is closed correctly and as soon as possible inside your transaction to avoid potential blocking of write operations.- Parameters:
relationshipType
- consider relationships with this typekey
- required property keytemplate
- required property value templatesearchMode
- search mode to use for finding matches- Returns:
- an iterator containing all matching relationships. See
ResourceIterator
for responsibilities.
- EXACT: The value has to match the template exactly. This is the same behavior
as
-
findRelationships
ResourceIterator<Relationship> findRelationships(RelationshipType relationshipType, Map<String, Object> propertyValues) Returns allrelationships
having thetype
, and the wanted property values. If an online index is found, it will be used to look up the requested relationships.If no indexes exist for the type with all provided properties, the database will scan all relationships of a specific type looking for matching values.
Note that equality for values do not follow the rules of Java. This means that the number 42 is equals to all other 42 numbers, regardless of whether they are encoded as Integer, Long, Float, Short, Byte or Double.
Same rules follow Character and String - the Character 'A' is equal to the String 'A'.
Finally - arrays also follow these rules. An int[] {1,2,3} is equal to a double[] {1.0, 2.0, 3.0}
Please ensure that the returned
ResourceIterator
is closed correctly and as soon as possible inside your transaction to avoid potential blocking of write operations.- Parameters:
relationshipType
- consider relationships with this typepropertyValues
- required property key-value combinations- Returns:
- an iterator containing all matching relationships. See
ResourceIterator
for responsibilities.
-
findRelationships
ResourceIterator<Relationship> findRelationships(RelationshipType relationshipType, String key1, Object value1, String key2, Object value2, String key3, Object value3) Returns allrelationships
having thetype
, and the wanted property values. If an online index is found, it will be used to look up the requested relationships.If no indexes exist for the type with all provided properties, the database will scan all relationships of a specific type looking for matching values.
Note that equality for values do not follow the rules of Java. This means that the number 42 is equals to all other 42 numbers, regardless of whether they are encoded as Integer, Long, Float, Short, Byte or Double.
Same rules follow Character and String - the Character 'A' is equal to the String 'A'.
Finally - arrays also follow these rules. An int[] {1,2,3} is equal to a double[] {1.0, 2.0, 3.0}
Please ensure that the returned
ResourceIterator
is closed correctly and as soon as possible inside your transaction to avoid potential blocking of write operations.- Parameters:
relationshipType
- consider relationships with this typekey1
- required property key1value1
- required property value of key1key2
- required property key2value2
- required property value of key2key3
- required property key3value3
- required property value of key3- Returns:
- an iterator containing all matching relationships. See
ResourceIterator
for responsibilities.
-
findRelationships
ResourceIterator<Relationship> findRelationships(RelationshipType relationshipType, String key1, Object value1, String key2, Object value2) Returns allrelationships
having thetype
, and the wanted property values. If an online index is found, it will be used to look up the requested relationships.If no indexes exist for the type with all provided properties, the database will scan all relationships of a specific type looking for matching values.
Note that equality for values do not follow the rules of Java. This means that the number 42 is equals to all other 42 numbers, regardless of whether they are encoded as Integer, Long, Float, Short, Byte or Double.
Same rules follow Character and String - the Character 'A' is equal to the String 'A'.
Finally - arrays also follow these rules. An int[] {1,2,3} is equal to a double[] {1.0, 2.0, 3.0}
Please ensure that the returned
ResourceIterator
is closed correctly and as soon as possible inside your transaction to avoid potential blocking of write operations.- Parameters:
relationshipType
- consider relationships with this typekey1
- required property key1value1
- required property value of key1key2
- required property key2value2
- required property value of key2- Returns:
- an iterator containing all matching relationships. See
ResourceIterator
for responsibilities.
-
findRelationship
Equivalent tofindRelationships(RelationshipType, String, Object)
, however it must find no more than onerelationship
or it will throw an exception.- Parameters:
relationshipType
- consider relationships with this typekey
- required property keyvalue
- required property value- Returns:
- the matching relationship or
null
if none could be found - Throws:
MultipleFoundException
- if more than one matchingrelationship
is found
-
findRelationships
ResourceIterator<Relationship> findRelationships(RelationshipType relationshipType, String key, Object value) Returns allrelationships
having thetype
, and the wanted property value. If an online index is found, it will be used to look up the requested relationships.If no indexes exist for the type/property combination, the database will scan all relationships of a specific type looking for the property value.
Note that equality for values do not follow the rules of Java. This means that the number 42 is equals to all other 42 numbers, regardless of whether they are encoded as Integer, Long, Float, Short, Byte or Double.
Same rules follow Character and String - the Character 'A' is equal to the String 'A'.
Finally - arrays also follow these rules. An int[] {1,2,3} is equal to a double[] {1.0, 2.0, 3.0}
Please ensure that the returned
ResourceIterator
is closed correctly and as soon as possible inside your transaction to avoid potential blocking of write operations.- Parameters:
relationshipType
- consider relationships with this typekey
- required property keyvalue
- required property value- Returns:
- an iterator containing all matching relationships. See
ResourceIterator
for responsibilities.
-
findRelationships
Returns allrelationships
of a specifictype
. Please take care that the returnedResourceIterator
is closed correctly and as soon as possible inside your transaction to avoid potential blocking of write operations.- Parameters:
relationshipType
- theRelationshipType
to return relationships for.- Returns:
- an iterator containing all relationships with matching type. See
ResourceIterator
for responsibilities. - Throws:
IllegalStateException
- if relationship index feature not enabled
-
terminate
void terminate()Marks this transaction as terminated, which means that it will be, much like in the case of failure, unconditionally rolled back whenclose()
is called. Once this method has been invoked, it doesn't matter ifcommit()
()} is invoked afterwards -- the transaction will still be rolled back. Additionally, terminating a transaction causes all subsequent operations carried out within that transaction to throw aTransactionTerminatedException
. Note that, unlike the other transaction operations, this method can be called from different thread. When this method is called, it signals to terminate the transaction and returns immediately. Calling this method on an already closed transaction has no effect. -
getAllNodes
ResourceIterable<Node> getAllNodes()Returns all nodes in the graph.- Returns:
- all nodes in the graph.
-
getAllRelationships
ResourceIterable<Relationship> getAllRelationships()Returns all relationships in the graph.- Returns:
- all relationships in the graph.
-
acquireWriteLock
Acquires a write lock forentity
for this transaction. The lock (returned from this method) can be released manually, but if not it's released automatically when the transaction finishes.- Parameters:
entity
- the entity to acquire a lock for. If another transaction currently holds a write lock to that entity this call will wait until it's released.- Returns:
- a
Lock
which optionally can be used to release this lock earlier than when the transaction finishes. If not released (withLock.release()
it's going to be released when the transaction finishes.
-
acquireReadLock
Acquires a read lock forentity
for this transaction. The lock (returned from this method) can be released manually, but if not it's released automatically when the transaction finishes.- Parameters:
entity
- the entity to acquire a lock for. If another transaction currently hold a write lock to that entity this call will wait until it's released.- Returns:
- a
Lock
which optionally can be used to release this lock earlier than when the transaction finishes. If not released (withLock.release()
it's going to be released with the transaction finishes.
-
schema
Schema schema()Returns theschema manager
where all things related to schema, for example constraints and indexing onlabels
.- Returns:
- the
schema manager
for this database.
-
commit
void commit()Commit and close current transaction.When
commit()
is completed, all resources are released and no more changes are possible in this transaction. -
rollback
void rollback()Roll back and close current transaction. Whenrollback()
is completed, all resources are released and no more changes are possible in this transaction -
close
void close()Close transaction. Ifcommit()
orrollback()
have been called this does nothing. If none of them are called, the transaction will be rolled back.All
ResourceIterables
that where returned from operations executed inside this transaction will be automatically closed by this method in they were not closed before.This method comes from
AutoCloseable
so that aTransaction
can participate in try-with-resource statements.
-
getNodeByElementId(String)
.