Kai Schlegel
Kai Schlegel

Reputation: 183

How does Spring Data Neo4j begin transaction work?

I have a comprehension question. I cannot understand how the database actions from the Neo4jTemplate like "getOrCreateNode()" belong to the surrounding transction. How is it implemented? The Neo4jTemplate would be shared in a multi-threaded environment? I cannot see a distinct membership of the transaction. I would understand if the actions are directly in the transaction object (e.g. tx.getOrCreateNode()).

@Service
public class TestService {

 @Autowired
 private Neo4jTemplate template;

  public void save(IndexedTriple triple) {
    GraphDatabase gdb = template.getGraphDatabase();
    Transaction tx = gdb.beginTx();

    Node subject = gdb.getOrCreateNode()
    ...

    tx.success();
    tx.finish();
  }
}

Thanks in advance.

Upvotes: 2

Views: 2833

Answers (1)

Aravind Yarram
Aravind Yarram

Reputation: 80194

The below extract from the reference documentation pretty much sums it up. Use the spring transaction manager instead of using the Neo4j transactions and let spring take care of demarcation. Also, the transaction management is completely thread-safe. For you, I suggest using @Transactional annotation. If there is an existing transaction already, then spring joins that existing transaction as well.

Transactions

The Neo4jTemplate provides implicit transactions for some of its methods. For instance save uses them. For other modifying operations please provide Spring Transaction management using @Transactional or the TransactionTemplate.

Upvotes: 4

Related Questions