I am persisting a Node entity that contains relationships to few other entities. This persist works fine when I am doing it one at a time, however if I run this concurrently, I get NPE at org.neo4j.ogm.mapper.EntityGraphMapper
java.lang.NullPointerException: null
at org.neo4j.ogm.mapper.EntityGraphMapper.updateRelationship(EntityGraphMapper.java:618) ~[neo4j-ogm-1.1.2.jar:na]
at org.neo4j.ogm.mapper.EntityGraphMapper.mapRelationshipEntity(EntityGraphMapper.java:481) ~[neo4j-ogm-1.1.2.jar:na]
at org.neo4j.ogm.mapper.EntityGraphMapper.link(EntityGraphMapper.java:330) ~[neo4j-ogm-1.1.2.jar:na]
at org.neo4j.ogm.mapper.EntityGraphMapper.mapEntityReferences(EntityGraphMapper.java:265) ~[neo4j-ogm-1.1.2.jar:na]
at org.neo4j.ogm.mapper.EntityGraphMapper.mapEntity(EntityGraphMapper.java:158) ~[neo4j-ogm-1.1.2.jar:na]
Here is my Node entity's crux:
@NodeEntity
public class Post {
@Relationship(type = "POST_BY_USER", direction = Relationship.INCOMING)
User poster;
@Relationship(type = "POST_TO_STORE", direction = Relationship.OUTGOING)
Store toStore;
@Relationship(type = "POST_PRODUCT", direction = Relationship.INCOMING)
Product product;
@Relationship(type = "POST_INSPIRATION", direction = Relationship.INCOMING)
Product_Inspiration productInspiration;
}
I tried persisting with both using a GraphRepository with (@Transactional)as well as using a session with the transaction, in both scenarios, i hit the same issue with concurrency.
Could someone please let me know how I could possibly fix this issue? Since I am getting this issue when I bombard concurrent requests, I am not sure if this could be something with the code flow as such.
As requested by Vince about code how to send concurrent requests, I am using axon cqrs framework with Play and I send multiple concurrent requests to my Play application's rest API which via axon invokes concurrent writes to Neo4j via postProduct method. here is how my write code looks like.
def postProduct(productId: ProductId, product2Post: Product2Post): Post = workNLogOnFail(
doPost(productId, product2Post),
"postProduct"
)
private def doPost(productId: ProductId, product2Post: Product2Post): Post = {
val product: Product = configureDomainEntity(productId, product2Post)
val storeId: StoreId = storeService1.findOrCreateStore(product2Post.host)
val post: Post = commonService.configurePost(product2Post.commonData2Post.relationInfo, isProduct = true) { (x: Post) =>
x.setToStore(storeService1.updateStoreForProductPost(x, storeId))
x.setProduct(product)
}
val persistedPost = xtPostProduct(product, post)
commonService.addUrlMapping(product2Post.commonData2Post, productId.identifier)
persistedPost
}
private def xtPostProduct(product: Product, post: Post): Post = {
Try {
val tx: Transaction = session.beginTransaction()
logger.debug(s"saving product")
// session.save(product)
logger.debug(s"updating user")
// session.save(post.getPoster)
logger.debug(s"posting product")
session.save(post)
tx.commit()
post
}.get
}
i tried @Transanctional with GraphRepository as well as session in the method xtPostProduct.