grails / gorm-mongodb

GORM for MongoDB

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Nested transactions doesn't work

rdisabato opened this issue · comments

I'm using Grails 4.0.2 and Gorm 7.0.3. Mongo DB is configured with codec engine.
I work in a start-up that intensively use Grails with MongoDB. We are now building an architecture based on plugins and each of our APIs often requires many database writes on multiple collections. We want to achieve code consolidation by using transactions, in order to support rollback of all writes in the case of unpredictable exception throws.

We found that methods marked with @transactional annotation works as expected: each save() inside the method is committed at the method end, and if an exception occurs nothing is effectively written to db.

Problems starts when using nested @transactional methods: the child method is always executed in a new transaction, and all the save() calls in the child method are committed at its method end. If an exception is thrown in the parent method after calling the child method, all the data inside the child method are already committed to database.

I've done a lot of debugging and i found that Gorm for MongoDB is using a transaction manager called "DatastoreTransactionManager" that extends "AbstractPlatformTransactionManager", and it has a property "nestedTransactionAllowed" that is set by default to "false". I've also set this property to "true" in my "doWithApplicationContext" method, but the behaviour remains the same.

I found that the engine that is intended to check if a transaction already exist, in order to join in it, make use of the method "isExistingTransaction" that is declared in "AbstractPlatformTransactionManager" and return always "false". This method is not overrided in "DatastoreTransactionManager. After seeing that i thought that Gorm for MongoDB version 7.0.2 simply does not support nested transactions. I think this demonstrates the problem i found.

I've also checked the library "spring-data-mongodb" that has the class "MongoTransactionManager" that extends "AbstractPlatformTransactionManager" and correctly implements implemented "isExistingTransaction" method. I've tried to replace the Gorm "DatastoreTransactionManager" bean with the "MongoTransactionManager" bean in "spring-data-mongodb" library, but after spending a lot of time and facing out a lot of errors and incompatibilities i'm enough sure this is not a practicable way.

Please help us to solve this very big problem, as we are building an enterprise class architecture and we are very confortable with Grails up to this.

Thanks a lot

commented

+1 have same problem.
@rdisabato Did you resolved this problem ?