- Notifications
You must be signed in to change notification settings - Fork 1.1k
DATAMONGO-1920 - Add support for MongoDB 4.0 transactions (snyc) #554
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
Conversation
mp911de left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder whether we should also provide a TransactionAwareMongoDbFactoryProxy. This could allow in-flight MongoTemplate creation that can participate in transactions if one is active.
| // check for native MongoDB transaction | ||
| if (resourceHolder != null && (resourceHolder.hasSession() || resourceHolder.isSynchronizedWithTransaction())) { | ||
| | ||
| resourceHolder.requested(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
RefCount is incremented on each access. I think we should increment this one only on first access. Otherwise, multiple calls will reference the underlying resource multiple times without reducing refcount.
| * @author Christoph Strobl | ||
| * @since 2.1 | ||
| */ | ||
| public enum SessionSynchronization { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
NATIVE and ANY are hard to guess names. How about ALWAYS, ON_ACTUAL_TRANSACTION and NEVER (to omit transactional participation even if a Tx is active)?
| @christophstrobl Which artifact (and repo) is necessary to try you transactional implementation? |
<dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-mongodb</artifactId> <version>2.1.0.DATAMONGO-1920-SNAPSHOT</version> </dependency>via <repositories> <repository> <id>spring-libs-snapshot</id> <url>https://repo.spring.io/libs-snapshot</url> </repository> </repositories> |
MongoTransactionManager is the gateway to the well known Spring transaction support. It allows applications to use managed transaction features of Spring. The MongoTransactionManager binds a ClientSession to the thread. MongoTemplate automatically detects those and operates on them accordingly. static class Config extends AbstractMongoConfiguration { // ... @bean MongoTransactionManager transactionManager(MongoDbFactory dbFactory) { return new MongoTransactionManager(dbFactory); } } @component public class StateService { @transactional void someBusinessFunction(Step step) { template.insert(step); process(step); template.update(Step.class).apply(update.set("state", // ... }; }); 6d72fdd to e71ee57 Compare Slightly tweak method names. Document MongoDatabaseUtils usage in the context of MongoTransactionManager. Rename SessionSynchronization constants to align with AbstractPlatformTransactionManager. Slightly tweak Javadoc and reference docs for typos.
2bfda74 to 1f38afa Compare We now download and unpack MongoDB directly instead of using TravisCI's outdated MongoDB version.
Remove outdated profiles.
e6abe82 to 21919a0 Compare We now download and unpack MongoDB directly instead of using TravisCI's outdated MongoDB version. Original pull request: #554.
Remove outdated profiles. Original pull request: #554.
…s driver). MongoTransactionManager is the gateway to the well known Spring transaction support. It allows applications to use managed transaction features of Spring. The MongoTransactionManager binds a ClientSession to the thread. MongoTemplate automatically detects those and operates on them accordingly. static class Config extends AbstractMongoConfiguration { // ... @bean MongoTransactionManager transactionManager(MongoDbFactory dbFactory) { return new MongoTransactionManager(dbFactory); } } @component public class StateService { @transactional void someBusinessFunction(Step step) { template.insert(step); process(step); template.update(Step.class).apply(update.set("state", // ... }; }); Original pull request: #554. Slightly tweak method names. Document MongoDatabaseUtils usage in the context of MongoTransactionManager. Rename SessionSynchronization constants to align with AbstractPlatformTransactionManager. Slightly tweak Javadoc and reference docs for typos. Original pull request: #554.
We now download and unpack MongoDB directly instead of using TravisCI's outdated MongoDB version. Original pull request: #554.
Remove outdated profiles. Original pull request: #554.
| That's merged and polished now! |
ℹ️ Requires: MongoDB Server 3.7.9 ℹ️
MongoDB Transactions
As of version 4, MongoDB will support Transactions. Transactions are built on top of
Sessions and therefore require an activeClientSession.MongoTransactionManagerwithin your application context, transaction support is DISABLED. You may usesetSessionSynchronization(ANY)to participate in ongoing non-native MongoDB transactions.To get full programmatic control over transactions, you may want to use the session callback on
MongoOperations.An example of programmatic transaction control within a
SessionCallbackis shown below:Programmatic transactions
The above example allows you to have full control over transactional behavior while using the session scoped
MongoOperationsinstance within the callback to ensure the session is passed on to every server call.To avoid some of the overhead that comes with this approach usage of a
TransactionTemplatecan take away some of the noise of manual transaction flow.Transactions with TransactionTemplate
Transactions with MongoTransactionManager
MongoTransactionManageris the gateway to the well known Spring transaction support. It allows applications to use managed transaction features of Spring.The
MongoTransactionManagerbinds aClientSessionto the thread.MongoTemplatedetects those and operates on these resources which are associated with the transaction accordingly.MongoTemplatecan also participate in other, ongoing transactions.@Transactional(readOnly = true)advisesMongoTransactionManageralso to start a transaction adding theClientSessionto outgoing requests.