DATAMONGO-2265 - Polishing.
Fix count operation inside transaction and avoid superfluous client session instantiation. Default MongoDatabase emission in case of non active transaction, update documentation, move test to another package. Delay reactive collection re/creation in test to cope with issues in server version 4.1.10. Original Pull Request: #745
This commit is contained in:
@@ -17,6 +17,7 @@
|
||||
* <<mongo.jsonSchema.generated, JSON Schema generation>> from domain types.
|
||||
* SpEL support in for expressions in `@Indexed`.
|
||||
* Annotation-based Collation support through `@Document` and `@Query`.
|
||||
* Declarative reactive transactions using <<mongo.transactions.reactive-tx-manager, @Transactional>>.
|
||||
|
||||
[[new-features.2-1-0]]
|
||||
== What's New in Spring Data MongoDB 2.1
|
||||
|
||||
@@ -138,6 +138,7 @@ template.withSession(session)
|
||||
The preceding example lets you have full control over transactional behavior while using the session scoped `MongoOperations` instance 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, you can use a `TransactionTemplate` to take away some of the noise of manual transaction flow.
|
||||
|
||||
[[mongo.transactions.transaction-template]]
|
||||
== Transactions with `TransactionTemplate`
|
||||
|
||||
Spring Data MongoDB transactions support a `TransactionTemplate`. The following example shows how to create and use a `TransactionTemplate`:
|
||||
@@ -173,6 +174,7 @@ txTemplate.execute(new TransactionCallbackWithoutResult() {
|
||||
|
||||
CAUTION: Changing state of `MongoTemplate` during runtime (as you might think would be possible in item 1 of the preceding listing) can cause threading and visibility issues.
|
||||
|
||||
[[mongo.transactions.tx-manager]]
|
||||
== Transactions with `MongoTransactionManager`
|
||||
|
||||
`MongoTransactionManager` is the gateway to the well known Spring transaction support. It lets applications use https://docs.spring.io/spring/docs/{springVersion}/spring-framework-reference/html/transaction.html[the managed transaction features of Spring].
|
||||
@@ -215,6 +217,7 @@ public class StateService {
|
||||
NOTE: `@Transactional(readOnly = true)` advises `MongoTransactionManager` to also start a transaction that adds the
|
||||
`ClientSession` to outgoing requests.
|
||||
|
||||
[[mongo.transactions.reactive]]
|
||||
== Reactive Transactions
|
||||
|
||||
Same as with the reactive `ClientSession` support, the `ReactiveMongoTemplate` offers dedicated methods for operating
|
||||
@@ -254,6 +257,7 @@ Mono<DeleteResult> result = Mono
|
||||
The culprit of the above operation is in keeping the main flows `DeleteResult` instead of the transaction outcome
|
||||
published via either `commitTransaction()` or `abortTransaction()`, which leads to a rather complicated setup.
|
||||
|
||||
[[mongo.transactions.reactive-operator]]
|
||||
== Transactions with `TransactionalOperator`
|
||||
|
||||
Spring Data MongoDB transactions support a `TransactionalOperator`. The following example shows how to create and use a `TransactionalOperator`:
|
||||
@@ -274,19 +278,20 @@ Step step = // ...;
|
||||
template.insert(step);
|
||||
|
||||
Mono<Void> process(step)
|
||||
.then(template.update(Step.class).apply(Update.set("state", …))
|
||||
.as(rxtx::transactional) <3>
|
||||
.then();
|
||||
.then(template.update(Step.class).apply(Update.set("state", …))
|
||||
.as(rxtx::transactional) <3>
|
||||
.then();
|
||||
----
|
||||
<1> Enable transaction synchronization for Transactional participation.
|
||||
<2> Create the `TransactionalOperator` using the provided `ReactiveTransactionManager`.
|
||||
<3> `TransactionalOperator.transactional(…)` provides transaction management for all upstream operations.
|
||||
====
|
||||
|
||||
[[mongo.transactions.reactive-tx-manager]]
|
||||
== Transactions with `ReactiveMongoTransactionManager`
|
||||
|
||||
`ReactiveMongoTransactionManager` is the gateway to the well known Spring transaction support.
|
||||
It lets applications use https://docs.spring.io/spring/docs/{springVersion}/spring-framework-reference/html/transaction.html[the managed transaction features of Spring].
|
||||
It allows applications to leverage https://docs.spring.io/spring/docs/{springVersion}/spring-framework-reference/html/transaction.html[the managed transaction features of Spring].
|
||||
The `ReactiveMongoTransactionManager` binds a `ClientSession` to the subscriber `Context`.
|
||||
`ReactiveMongoTemplate` detects the session and operates on these resources which are associated with the transaction accordingly.
|
||||
`ReactiveMongoTemplate` can also participate in other, ongoing transactions.
|
||||
@@ -307,15 +312,15 @@ static class Config extends AbstractMongoConfiguration {
|
||||
// ...
|
||||
}
|
||||
|
||||
@Component
|
||||
@Service
|
||||
public class StateService {
|
||||
|
||||
@Transactional
|
||||
Mono<UpdateResult> someBusinessFunction(Step step) { <2>
|
||||
|
||||
return template.insert(step)
|
||||
.then(process(step))
|
||||
.then(template.update(Step.class).apply(Update.set("state", …));
|
||||
.then(process(step))
|
||||
.then(template.update(Step.class).apply(Update.set("state", …));
|
||||
};
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user