Issue
I have a service method mergeAndUpdateAndCompliance()
which uses Spring Data Neo4j OGM version:
@Transactional("neo4jTransactionManager")
@Override
public ComplianceMatrix mergeAndUpdateAndCompliance() {
mergeAndUpdate();
return compliance()
}
@Override
public void mergeAndUpdate() {
//do some update operations
}
@Override
@Transactional(readOnly = true, transactionManager = "neo4jTransactionManager")
public void compliance() {
//do some read operations
}
mergeAndUpdateAndCompliance()
invokes two other service methods - mergeAndUpdate()
and compliance(). compliance() reads the data updated by mergeAndUpdate(). Right now compliance() doesn't see the data updated in mergeAndUpdate() in the same transaction.
It works only if I add session.getTransaction().commit();
between them:
@Autowired
private Session session;
@Transactional("neo4jTransactionManager")
@Override
public ComplianceMatrix mergeAndUpdateAndCompliance() {
mergeAndUpdate();
session.getTransaction().commit();
return compliance()
}
Is it safe to place session.getTransaction().commit(); inside of Spring transaction ? What is the right way to solve this issue? Is it possible to use transaction propagation with SDN in order to configure mergeAndUpdate
with REQUIRES_NEW ?
Solution
You have applied @Transactional
on the mergeAndUpdateAndCompliance
, function, within it is also applied to compliance
method. You should try this way:
@Override
public ComplianceMatrix mergeAndUpdateAndCompliance() {
mergeAndUpdate();
return compliance()
}
@Override
@Transactional("neo4jTransactionManager")
public void mergeAndUpdate() {
//do some update operations
}
@Override
@Transactional(readOnly = true, transactionManager = "neo4jTransactionManager")
public void compliance() {
//do some read operations
}
Instead of applying it on the mergeAndUpdateAndCompliance
, you should apply it on mergeAndUpdate
and compliance
functions separately. So that you don't have to manually commit the transaction.
Answered By - Charchit Kapoor
Answer Checked By - David Goodson (JavaFixing Volunteer)