Dear MongoDB Community,
I’m encountering an issue with transaction management in a Spring Data MongoDB application when using the @Transactional
annotation across multiple MongoDB repositories.
Problem Statement:
In my Spring Data MongoDB application, I have multiple MongoDB repositories (RepoA
and RepoB
) handling different entities (EntityA
and EntityB
). These entities are mapped to MongoDB documents.
Example Scenario:
javaCopy code
@Service
public class MyService {
@Autowired
private RepoA repoA;
@Autowired
private RepoB repoB;
@Transactional("mongotrans")
public void saveEntities() {
EntityA entityA = new EntityA();
EntityB entityB = new EntityB();
repoA.save(entityA); // This saves successfully -> should not get saved until transaction completes ideally
repoB.save(entityB); // This should be part of the same transaction, but it's not working
}
}
Issue Details:
Despite using @Transactional
, the saves across multiple repositories (repoA
and repoB
) do not behave as expected. If one of the save operations initiated, it should get saved after the block completes
Expected Behavior:
I expect both save operations to be part of the same transaction, ensuring atomicity across multiple repositories. If any save operation fails, the entire transaction should be rolled back.
=
Request for Assistance:
Could you please provide guidance on how to properly configure transaction management for MongoDB repositories in a Spring Data MongoDB application? Are there any specific considerations or best practices to ensure transactions work seamlessly across multiple repositories?
Any insights or suggestions would be greatly appreciated.
also FYI
if i am using @transactionl for a single collection multiple save it is working as expected.
but finding iussues with multi document save alone
config added:
@Configuration
class MongoConfig(private val dbFactory: MongoDatabaseFactory) {
@Bean("mongotrans")
fun transactionManagerMongo(): MongoTransactionManager {
return MongoTransactionManager(dbFactory)
}
}