简体   繁体   English

如何在同时保存数据的同时避免spring数据neo4j ogm中的死锁

[英]how to avoid deadlock in spring data neo4j ogm while saving data concurrently

I have been trying to load test concurrently using SpringDataNeo4j(SDN) with 1 CPU AND 1GB RAM. 我一直在尝试使用带有1个CPU和1GB RAM的SpringDataNeo4j(SDN)并发加载测试。 For 'GET' (read) request able to test 1000 threads with ramp-up of 1second. 对于“ GET”(读取)请求,能够以1秒的加速测试1000个线程。 For 'POST' (write) request, but able to test only with 18 threads with ramp-up of 1second, beyond this thread. 对于“ POST”(写)请求,但仅能测试18个线程,超出该线程的时间为1秒。 We face DeadLock exception: 我们面临DeadLock异常:

Caused by: org.neo4j.ogm.exception.CypherException: Error executing Cypher "Neo.TransientError.Transaction.DeadlockDetected"; 由以下原因引起:org.neo4j.ogm.exception.CypherException:执行Cypher“ Neo.TransientError.Transaction.DeadlockDetected”时出错。 Code: Neo.TransientError.Transaction.DeadlockDetected; 代码:Neo.TransientError.Transaction.DeadlockDetected; Description: LockClient[1081] can't wait on resource RWLock[NODE(97), hash=108078135] since => LockClient[1081] <-[:HELD_BY]- RWLock[NODE(98), hash=1267379687] <-[:WAITING_FOR]- LockClient[1076] <-[:HELD_BY]- RWLock[NODE(97), hash=108078135] 说明:LockClient [1081]无法等待资源RWLock [NODE(97),hash = 108078135],因为=> LockClient [1081] <-[:HELD_BY]-RWLock [NODE(98),hash = 1267379687] <- [:WAITING_FOR]-LockClient [1076] <-[:HELD_BY]-RWLock [NODE(97),hash = 108078135]

I have referred http://neo4j.com/docs/java-reference/current/#transactions-deadlocks 我已经提到了http://neo4j.com/docs/java-reference/current/#transactions-deadlocks

TransactionTemplate template = new TransactionTemplate(  ).retries( 5 ).backoff( 3, TimeUnit.SECONDS );

For saveService, I use default @Transactional though I couldn't able to replicate the TransactionTemplate in my testing code. 对于saveService,尽管无法在测试代码中复制TransactionTemplate ,但我使用默认的@Transactional And I use my DataSourceFactory configuration. 我使用DataSourceFactory配置。

@Configuration 
@PropertySource(value = { "classpath:ogm.properties" }
@EnableNeo4jRepositories(basePackages = "com.my.graph.repository")
@EnableTransactionManagement

Any suggestions! 有什么建议么!

Thanks in advance! 提前致谢!

You can reduce the possibility of a deadlock happening by 您可以通过以下方式减少发生死锁的可能性

  • making the transaction smaller - eg instead of saving 1000 nodes and relationships, save only 100 使事务更小-例如,不保存1000个节点和关系,而仅保存100个

  • using the structure of your domain ensure that you don't concurrently update same nodes 使用域的结构确保您不会同时更新相同的节点

But sometimes it is not possible to avoid. 但是有时无法避免。 In those situations you can either 在这种情况下,您可以

  • catch the exception and rerun the transaction (essentially do yourself retry) 捕获异常并重新运行事务(本质上是自己重试)

     int retries = 5; while (retries > 0) { try { fooService.foo(bar); } catch (CypherException ex) { retries--; Thread.sleep(delay); } } 
  • use some library that already implements retry logic, like spring-retry or guava-retrying 使用一些已经实现重试逻辑的库,例如spring-retryguava-retrying

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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