-
-
Notifications
You must be signed in to change notification settings - Fork 504
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Transaction support #1888
Comments
@alcaeus: FYI, we have a new session API in the specifications pipeline for MongoDB drivers, which is intended to also improve the API around transactions. ETA for those APIs will be for the driver versions supporting MongoDB 4.2. It may be far off, but it's is something we can keep in mind when brainstorming about how best to integrate transactions into ODM. On a related note: https://github.com/p-mongo/tests/blob/master/docs/mongoid-sessions-transactions.md |
If somebody passes around this, it's pretty easy to implement with ODM 2.0 by passing session into
Ideally you can override |
I'll just note here that the above is not suggested: this will lead to the UnitOfWork assuming that all data was written successfully, while the |
Fair point, in my case it's used with |
Hello, I would like to contribute and work on an implementation of this feature. Could you please provide some advice and tell me how the desired implementation should work? |
@DavideTriso That is great news! The first step would be basic transaction handling: have the UnitOfWork start a session (if the feature is enabled), do its usual commit operations, then commit the transaction and handle any errors. From then on, we can see what other features we want to add. Note that we can't make this an "always-on" feature, as transactions require replica sets which may not be available. Thus, I'd add this to the configuration and allow users to turn transactions on or off as necessary. To get started, I recommend forking from the "master" branch, which always points to the upcoming minor release and creating a draft PR as a discussion point. If you need more guidance, also feel free to contact us in the Doctrine Slack via the #mongodb-odm channel and we can go from there. The resources @jcaillot are a good starting point for the non-technical side of things, so go ahead and check those out if you need help. Thanks to both of you for moving this along! |
Tanks for your advice @alcaeus and @jcaillot. I have given a nearest look at the mongodb-odm code before starting to work at the implementation
Please let me know your thoughts! |
Session should be "opened" as late as possible, possibly only before
I believe current approach is to leave An argument can be made that if transactions are enabled in configuration, then it should become the default way of handling Just my two cents ;) |
To elaborate on what @Steveb-p commented:
IMO, this should happen as follows:
From there, we can expand this. For example, we may want to let the user pass their own session into As for testing this, just some notes that might make your life easier:
|
@DavideTriso Hey, I tried implementing this using this blog. Though I get the following error while passing session to I want to develop my new application based on this package but I am just unsure because of its lack of support for multi-document transactions. |
Hello @gitwithravish, the error you have reported is related to how MongoDB works and not to the ODM itself. Implementing multi-document transactions in ODM is currently not possible without BCs. As far as I know, if you need multi-document transactions you have to handle them yourself, for example by wrapping the DocumentManager methods in another class which handles the transaction (start, commit, rollback). Also remember you cannot use the ODM events when doing this, because the ODM is not aware of the transaction and will fire the postUpdate, postRemove, postPersist events as soon as the DB command is executed, but if you are inside a transaction the operations can still be rolled back if an error occurs. |
@DavideTriso I did not pass session in Currently I have my entire application built upon doctrine-odm package. But wherever I am required to use transactions, I am using mongodb php driver functions directly to start a session, pass the session to all transactions and then commit/abort. Issue that I have to use 2 database communication approaches. Some using docrine-odm and some using pure php mongodb driver. Can you suggest me when and where do I pass a session to doctrine-odm package and make use of existing functionalities ? I understand that there is no straightforward way to this but since you have been onto this since a long time, you will be able to help me out with this i believe. Can you brief me about the following statement ?
This approach has also done something similar but it didnt do the job for me. |
@gitwithravish I will reach you on Slack for further details |
@DavideTriso @gitwithravish If you have any conclusions please write it here. Thank you :) |
My conclusion is that it is possible to use multi-document transactions with odm by implementing your additional layer to handle them, but with some limitations. This approach is only possible if you do not rely on the built-in events system of doctrine, because odm is not aware of the transaction and fires the “post-flush” events as soon as the db command is executed. |
Has there been any movement on this in the past 1.5 years? MongoDB 4.2 has been out since August 2019 supporting multi-document transactions. It would be great to be able to strictly use this library as opposed to having to also use the mongo/php library for this use case. |
@cousinjoe no, there has not. @DavideTriso summed up the issue quite well, as in that the UnitOfWork ends up in an inconsistent state if a flush does not succeed. In ORM, an error in a transaction leads to a closed entity manager, which is an irrecoverable state (meaning that you have to reset the EM leading to all entities becoming invalid). Due to the way MongoDB works we are a lot more flexible and the driver is able to recover from such errors. However, the way UnitOfWork is implemented means that once a flush fails, it can't be executed again. This makes transactions only semi-useful and introduces this broken state into UnitOfWork. This is something I'd like to avoid at all costs. Unfortunately, this means that UnitOfWork requires significant changes. To be fair, it would also benefit from these changes even when not using transactions, as it can already end up in a very broken state when writes fail. |
This was implemented in #2586 and will be released in 2.7.0 🎉 |
Feature Request
Introduce transaction support on database level as MongoDB starts to allow since version 4.0.
Summary
I'd like to ask for opinion and discuss future solutions regarding transactions in Doctrine ODM.
MongoDB documentation regarding transactions: https://docs.mongodb.com/manual/core/transactions/
There are a few relevant bits of information regarding this feature. Specifically:
What is Doctrine ODM desired approach on this? Introducing
beginTransaction()
,transactional()
methods intoDocumentManager
? Since MongoDB transactions incur performance penalty, they probably will not be enabled onflush
by default?The text was updated successfully, but these errors were encountered: