I have 2 repositories, one for mongodb (DocumentRepository) and the other for hibernate entity (EntityRepository)
I have a simple service:
@Transactional
public doSomePersisting() {
try {
this.entityRepository.save(entity);
this.documentRepository.save(document);
}
catch(...) {
//Rollback mongoDB here
}
}
Is it possible to rollback the mongoDB on the "//Rollback mongoDB here" line? I already got a rollback from the entity part (Transactional annotation)
Sorry for reposting my answer.
The earlier code was allowed to insert data into MongoDB even query exceptions throwing at data insertion into PostgreSQL(Using myBatis).
I have resolved the data Transaction issue between MongoDB and Relational database and @Transactional perfectly works by making these changes in the above code.
Solution for @Transactional Management.Mongo Config class
Service class for Data insertion
POM.XML
MongoDB Rollback does not work with @Transactional and MongoTransactionManager. Full code implementation is here.
MongoDB 4.0, mongo-java-driver(version 3.8.2), spring-data-mongodb(version 2.1.0)
MongoConfig class
Service class to insert data in mongodb and postgreSQL(Using mybatis).
Data is not getting rolledback at mongodb even exception throwing at this line userService.save(user); //This line throws invalid syntax at insert query exception.
MongoDB doesn't support transactions (at least not outside the scope of a single document). If you want to roll back changes you will need to handcraft that yourself. There are a few resources out there that describe ways of implementing your own transactions in Mongo if you really need them in certain circumstances. You could take a look at..
http://docs.mongodb.org/manual/tutorial/perform-two-phase-commits/
This is just an explanation of a pattern you could use. If you find that you absolutely need transactions in your application, you should consider whether MongoDB is a good fit for your needs.