mongodb icon indicating copy to clipboard operation
mongodb copied to clipboard

Support Mongodb 4.0 transactions

Open fire opened this issue 7 years ago • 12 comments

Support transactions in mongodb elixir driver. The end goal is to see many to many joins working in mongodb ecto.

EXAMPLE FROM MONGODB DOCUMENTATION

Consider a scenario where for each order shipment, you need to decrease the inventory for the item shipped. If either write fails, you want the undo both operations.

The following Python code shows a sample usage of transactions where either both write operations succeed or both operations fail.

client = pymongo.MongoClient(...)
db = client.test

s = client.start_session()
s.start_transaction()

try:
    db.inventory.update_one({"sku": "abc123"}, {"$inc": {"qty": -100}}, session=s)
    db.shipment.insert_one({"sku": "abc123", "qty": 100}, session=s)
except Exception:
    s.abort_transaction()
else:
    s.commit_transaction()
s.end_session()

As mentioned in: https://github.com/ankhers/mongodb_ecto/issues/113

fire avatar Jun 13 '18 13:06 fire

https://github.com/mongodb/mongo/releases/tag/r4.0.0-rc5 Is the latest release candidate.

fire avatar Jun 13 '18 13:06 fire

Mongdb tests for transactions https://github.com/mongodb/mongo/blob/v4.0/jstests/core/txns/multi_statement_transaction.js

fire avatar Jun 13 '18 15:06 fire

Requires session support. https://docs.mongodb.com/manual/core/read-isolation-consistency-recency/#causal-consistency

fire avatar Jun 13 '18 15:06 fire

Going to take a break but the session and transaction example is https://github.com/mongodb/mongo-c-driver/blob/master/src/libmongoc/examples/example-transaction.c .

fire avatar Jun 13 '18 15:06 fire

Any news on this one?

AlexKovalevych avatar Dec 12 '18 13:12 AlexKovalevych

Nothing yet.

ankhers avatar Dec 13 '18 23:12 ankhers

I want to try to implement this one.

hauleth avatar Jul 23 '19 10:07 hauleth

That would be appreciated. Thank you!

ankhers avatar Jul 25 '19 03:07 ankhers

My idea is to automatically start session for each new connection, always (or maybe make that optional via connection options) and then use regular DBConnection API to handle transactions. The only issue I see there is that when there will be pool then the application will not provide linearity. However that shouldn't be much of issue as this is how the library behaves currently.

hauleth avatar Jul 26 '19 12:07 hauleth

Close as current master has support for sessions and transactions (not implemented as above, but with their own custom API).

hauleth avatar Oct 30 '20 10:10 hauleth

For getting this transaction changes. In my local, i tried with "{:mongodb, "~> 0.5.1"}". But i can't able to get the transaction futures like. Mongo.start_session or Session.start_transaction(session). How can i get transaction future

YasirAsar avatar Dec 19 '20 09:12 YasirAsar

@YasirAsar It's only on master at this point.

scottmessinger avatar Feb 02 '21 20:02 scottmessinger