Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Transactions In MongoDB

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.

like image 414
user1348855 Avatar asked Feb 19 '13 21:02

user1348855


People also ask

What are transactions in MongoDB?

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.

Is MongoDB good for transactional?

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.

Does MongoDB support ACID transactions?

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.


3 Answers

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

like image 104
Grigori Melnik Avatar answered Oct 16 '22 17:10

Grigori Melnik


MongoDB does support transaction-like semantics using two-phase commits.

There is also another independent effort to support transactions in mongodb using optimistic locking.

like image 28
2020 Avatar answered Oct 16 '22 19:10

2020


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

like image 1
K.C. Avatar answered Oct 16 '22 19:10

K.C.