Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

does Spring @transactional work with MongoDB?

I'm developing a web application with Spring Boot and MongoDB. I want to make the services work with the @transactional spring annotation, but I don't know if that really works. (I didn't work with mongoDB before).

I added the annotation and it seem that everything run fine (The application runs and I can do all operations CRUD), but, I don't know if Spring is ignoring the annotation and it is working as usual, or is really considering the transactionality.

In other post, I have seen that I should add a new bean in the configuration class, in order to enable the transactionlity between Spring and MongoDB. Is it really necessary?, I only use transactions with single Mongo documents.

like image 374
tovarichML Avatar asked Mar 10 '19 11:03

tovarichML


People also ask

Does MongoDB support transactional?

For situations that require atomicity of reads and writes to multiple documents (in a single or multiple collections), MongoDB supports multi-document transactions. With distributed transactions, transactions can be used across multiple operations, collections, databases, documents, and shards.

Can we use @transactional in repository?

The usage of the @Repository annotation or @Transactional . @Repository is not needed at all as the interface you declare will be backed by a proxy the Spring Data infrastructure creates and activates exception translation for anyway.

Can we use MongoDB with Spring?

The Spring framework provides powerful connectors to easily perform database operations with MongoDB. Data is stored as BSON objects in MongoDB making data retrieval easy. For this Spring Boot and MongoDB example tutorial, we are only concerned with the Persistence and Database layers.

On which can the @transactional annotation be applied?

The @Transactional annotation is the metadata that specifies the semantics of the transactions on a method. We have two ways to rollback a transaction: declarative and programmatic. In the declarative approach, we annotate the methods with the @Transactional annotation.


2 Answers

@Transactional works only from spring-data-mongodb version 2.1.0 and higher: https://docs.spring.io/spring-data/mongodb/docs/2.1.0.RELEASE/api/

Indeed you have to add the bean:

@Bean
MongoTransactionManager transactionManager(MongoDatabaseFactory dbFactory) {
    return new MongoTransactionManager(dbFactory);
}

I don't know if Spring is ignoring the annotation and it is working as usual, or is really considering the transactionality

For this, you can throw an exception between 2 DB updates and check if the first update has been rolled back.

But if you use transactions within a single Mongo document, you don't need the @Transactional annotation:

In MongoDB, a write operation is atomic on the level of a single document, even if the operation modifies multiple embedded documents within a single document. MongoDb documentation - Transactions

like image 179
Mykeul Avatar answered Sep 22 '22 12:09

Mykeul


For Reactive style mongoDB & Spring boot integration the answer I provided here can be useful to people

like image 27
kakabali Avatar answered Sep 21 '22 12:09

kakabali