I am using a NoSQL database MongoDB with Java and Spring Data. I am aware that MongoDB only supports transactions for a single document.
I am using Spring Transactions to carry out MongoDB transcations. I am using TransactionTemplate. What should I set in TransactionManager when using TransactionTemplate?
EDIT
I have something like this:
<bean id=”txtTemplateBean” class=”org.springframework.transaction.support.TransactionTemplate”>
<property name=”transactionManager” ref=”txnManagerBean”></property>
I need to define txnManagerBean to point to something like DataSourceTransactionManager for a MongoDB database.
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.
MongoDB doesn't support transactions, but saving one document is atomic. So, it is better to design you database schema in such a way, that all the data needed to be saved atomically will be placed in one document.
However, not all databases support transactions that work across multiple records, which can be concerning to developers who are accustomed to using them. But, MongoDB supports multi-document MongoDB ACID transactions for the use cases that require them.
Multi-document ACID transactions are now supported in MongoDB 4.0! See https://www.mongodb.com/blog/post/mongodb-multi-document-acid-transactions-general-availability
MongoDB does support transaction-like semantics using two-phase commits.
There is also another independent effort to support transactions in mongodb using optimistic locking.
MongoDB doesn't support transactions, it only supports atomic operations.
http://docs.mongodb.org/manual/tutorial/model-data-for-atomic-operations/
Here is a post from someone who implemented transactions for MongoDB with optimistic locking: https://stackoverflow.com/a/12757751/1173560
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With