我的图中有以下 2 个现有节点。
A 客户 由唯一客户编号标识的节点。
A 产品 由唯一 ISBN 标识符标识的节点。
我想在一个 之间创建一个关联客户 节点和一个 产品 节点。
但我想将此关联表示为一个名为 的新节点。许可证 将有一个链接到 的节点客户 节点和一个链接到 产品 节点。
此 许可证 节点将有一个新的内部标识符作为随机 GUID 生成。
我的应用程序中的逻辑创建了新的 许可证 节点并将它们链接到其他 2 个节点是在一个事务中执行的。if (Product NOT already associated with the License for that Customer)
create a new License node with a new random GUID
create a relationship from the new License Node to the Product Node
create a relationship from the Customer Node to the new License Node
但是,多个请求可以使用相同的 ISBN 和客户编号同时到达。
发生这种情况时,我有时会收到重复的 许可证 为相同的节点创建 客户 和 产品 节点。
spring data neo4j 中的事务似乎并不能阻止这种情况的发生。
Example of correctly added License
Example of License added twice
如何确保在 Customer 节点和 Product 节点之间只创建一个 License 节点?
最佳答案
The transaction in spring data neo4j does not seem to prevent this from happening.
Neo4j 已读取事务的已提交隔离级别。为了防止这种情况,您需要可序列化。
为了实现您的需要,您可以:
Product NOT already associated with...
之前锁定 Product 和 Customer 节点查看。您可以使用这样的查询来做到这一点(在同一个事务中):MATCH (n:Product) WHERE ID(n) = {id} REMOVE n._lock
和类似的
Customer
. 关于neo4j - 我如何保证在交易中的两个现有节点之间只创建一个新的唯一节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45717128/