Aziz Mirzayev
931 subscribers
735 photos
81 videos
20 files
919 links
Assalomu alaykum.

Men Mirzayev Aziz .Net va AI bo'yicha dasturchiman. Xozirda EPAM korxonasida faoliyat olib boraman.

Bu kanalda hayotimda bo'lib o'tayotgan yangiliklar, fikrlar va hulosalar bn baham ko'raman.

Me --> http://bit.ly/3Ek47E8
Download Telegram
Aziz Mirzayev
Xozirch Integration testlarini yozib bo'ldim. Bugun bitta muammo chiqdi. Mongodbda auto transaction by default ishlamasakan. Misol db contextga 1 dan ko'p entity change qilinsa xato beryapti. Xozircha Database.AutoTransactionBehavior = AutoTransactionBehavior.Never;…
This setting (Database.AutoTransactionBehavior = AutoTransactionBehavior.Never) in EF Core when working with MongoDB (or any non-transactional provider) is not just a toggle — it has important caveats. Let’s go deep into what this means, why it exists, and the potential issues you should watch out for.

⸻

🔎 What AutoTransactionBehavior Does

In EF Core (SQL providers like SQL Server, PostgreSQL, etc.):
• By default, SaveChanges wraps your operations inside a transaction if one isn’t already active.
• This ensures atomicity: either all changes succeed, or none are committed.

MongoDB is different:
• MongoDB doesn’t use EF’s transaction system natively.
• Transactions exist in MongoDB only for replica sets and sharded clusters (introduced in MongoDB 4.0), and even then EF Core providers may not fully integrate with them.
• So the EF Core MongoDB provider often disables transaction wrapping by default or allows you to set this explicitly.

⸻

⚠️ Potential Issues When Using AutoTransactionBehavior.Never

1. Loss of Atomicity
• SQL DB: if you call SaveChanges on 5 entities and the 3rd insert fails → all inserts roll back.
• MongoDB + Never: each document is sent individually; if the 3rd fails → the first 2 stay written.
👉 This leaves your DB in a partially saved state, inconsistent with your domain model.

⸻

2. Concurrency Issues
• Without a transaction, if multiple processes/threads update related documents at the same time, you can get:
• Lost updates (last writer wins).
• Race conditions (e.g., two services decrementing stock, both reading the same old value, then writing incorrect new values).
• Normally EF Core’s transaction would serialize this, but Never disables that safeguard.

⸻

3. Cross-Collection Inconsistencies
• Example: You create an Order and related OrderItems.
• With Never, if Order inserts succeed but OrderItems fail → orphaned data.
• MongoDB doesn’t enforce foreign keys, so cleanup is manual.

⸻

4. No Retry on Failure
• SQL providers + EF Core transactions can be retried as a unit if transient errors occur.
• With Never, MongoDB inserts/updates might fail in the middle, requiring you to implement your own retry + compensation logic.

⸻

5. Eventual Consistency Surprises
• If your app logic assumes that after SaveChanges the DB is in a valid, fully persisted state, that assumption is broken.
• For example:
• Service A: Saves Product and Inventory documents.
• Service B: Immediately queries both.
If Inventory failed to persist, Service B sees half-saved state.

⸻

6. Harder Unit of Work Semantics
• EF Core’s DbContext is supposed to represent a unit of work.
• With Never, SaveChanges no longer guarantees atomic unit-of-work behavior.
• That undermines the DDD principle of aggregate consistency.

⸻

7. Provider-Specific Transaction Support Gaps
• MongoDB does support multi-document transactions on replica sets/shards, but:
• Not all EF Core MongoDB providers implement them.
• If they do, you must use AutoTransactionBehavior.WhenNeeded or explicit BeginTransaction().
👉 By forcing Never, you guarantee transactions are never used, even where they are possible.

⸻

8. Debugging & Error Recovery Complexity
• Example: A batch update fails mid-way.
• With transactions, you know nothing changed → just retry.
• With Never, you must:
1. Detect which documents succeeded.
2. Roll back manually or compensate forward.
3. Handle side effects (like external events fired for already-saved docs).

⸻

✅ When is Never Safe/Useful?

You might want this if:
• Your operations are idempotent (safe to repeat).
• Each document is independent (no strong relationships).
• You rely on MongoDB’s natural eventual consistency model.
• Performance matters more than strict atomicity (skipping transactions can be faster).

⸻

🧩 Best Practices if You Must Use Never
1. Design for Idempotency
E.g., if inserting PaymentHistory, use natural/document IDs that prevent duplicates if retried.
2. Apply Outbox Pattern for event publishing so half-saved states don’t fire inconsistent domain events.
3.
Aziz Mirzayev
Xozirch Integration testlarini yozib bo'ldim. Bugun bitta muammo chiqdi. Mongodbda auto transaction by default ishlamasakan. Misol db contextga 1 dan ko'p entity change qilinsa xato beryapti. Xozircha Database.AutoTransactionBehavior = AutoTransactionBehavior.Never;…
Use Compensating Actions: If Order fails, delete any saved OrderItems, or mark Order as “incomplete.”
4. Limit SaveChanges Scope: Save only one aggregate root per SaveChanges call to reduce risk.
5. Enable Application-Level Transactions: Wrap SaveChanges in custom logic that ensures consistency across documents.

⸻

🔑 Bottom Line

Setting Database.AutoTransactionBehavior = AutoTransactionBehavior.Never in EF Core MongoDB:
• Removes EF’s guarantee of atomicity.
• Exposes you to partial saves, race conditions, and inconsistencies.
• Makes you responsible for implementing consistency, rollback, and retry logic manually.
• Only safe if your model and operations are naturally idempotent or single-document focused.
EPAMning Maxtumqulidagi ofisiga anchadan beri kelmaganakanman. Joylashgunimcha 15 minut ketti ))))
👍1
Kun boshlanish ritualini boshladik )))
👍2
EPAMchilar ucun party 🎉🎉🎉
👍1
5 oʻrin oldi mani komandam. Lekin zoʻr boʻldi. 1 martta AWS da ishlashim edi.
https://www.mongodb.com/company/blog/building-with-patterns-the-extended-reference-pattern
Mana shu yaramasni o'qiganimda o'zim bn o'zim 2 hafta olishmasdim MongoDBni tanlashda =)
👍1
Forwarded from Programmer Jokes
Bugun rosa qiziq kun bo'ldi. Face detection va face analysis bo'yicha eksperimentlar o'tqazdim. Aaancha bo'lganakan bunaqa ilmiy yondashuv qilmaganimga. Mazza qvordim lekin =)
👍1
Bjr bjr =)
🆒1
Вокруг сплошной обман...
🤣1