简体   繁体   中英

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. For 'GET' (read) request able to test 1000 threads with ramp-up of 1second. For 'POST' (write) request, but able to test only with 18 threads with ramp-up of 1second, beyond this thread. We face DeadLock exception:

Caused by: org.neo4j.ogm.exception.CypherException: Error executing Cypher "Neo.TransientError.Transaction.DeadlockDetected"; Code: 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]

I have referred 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. And I use my DataSourceFactory configuration.

@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

  • 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

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