简体   繁体   中英

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 (eg 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.

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM