Files
spring-data-examples/mongodb/transactions
Spring Operator ccae97890f #491 - URL Cleanup.
This commit updates URLs to prefer the https protocol. Redirects are not followed to avoid accidentally expanding intentionally shortened URLs (i.e. if using a URL shortener).

# Fixed URLs

## Fixed Success
These URLs were switched to an https URL with a 2xx status. While the status was successful, your review is still recommended.

* [ ] http://www.apache.org/licenses/ with 1 occurrences migrated to:
  https://www.apache.org/licenses/ ([https](https://www.apache.org/licenses/) result 200).
* [ ] http://www.apache.org/licenses/LICENSE-2.0 with 426 occurrences migrated to:
  https://www.apache.org/licenses/LICENSE-2.0 ([https](https://www.apache.org/licenses/LICENSE-2.0) result 200).
2019-03-22 08:13:14 +01:00
..
2019-03-22 08:13:14 +01:00
2019-03-20 10:10:59 -05:00

Spring Data MongoDB - Transaction Sample

This project contains samples for upcoming MongoDB 4.0 transactions.

Running the Sample

The sample uses multiple embedded MongoDB processes in a MongoDB replica set. It contains test for both the synchronous and the reactive transaction support in the sync / reactive packages.

You may run the examples directly from your IDE or use maven on the command line.

INFO: The operations to download the required MongoDB binaries and spin up the cluster can take some time. Please be patient.

Sync Transactions

MongoTransactionManager is the gateway to the well known Spring transaction support. It lets applications use the managed transaction features of Spring. The MongoTransactionManager binds a ClientSession to the thread. MongoTemplate detects the session and operates on these resources which are associated with the transaction accordingly. MongoTemplate can also participate in other, ongoing transactions.

@Configuration
static class Config extends AbstractMongoConfiguration {

	@Bean
	MongoTransactionManager transactionManager(MongoDbFactory dbFactory) {
		return new MongoTransactionManager(dbFactory);
	}

	// ...
}

@Component
public class TransitionService {

    @Transactional
    public void run(Integer id) {
    
    	Process process = lookup(id);
    
    	if (!State.CREATED.equals(process.getState())) {
    		return;
    	}
    
    	start(process);
    	verify(process);
    	finish(process);
    }
}

Reactive transactions

ReactiveMongoTemplate offers dedicated methods for operating within a transaction without having to worry about the commit/abort actions depending on the operations outcome. There's currently no session or transaction integration with reactive repositories - we apologize for that!

NOTE: Please note that you cannot preform meta operations, like collection creation within a transaction.

@Component
public class RactiveTransitionService {
	
    public Mono<Integer> run(Integer id) {

		return template.inTransaction().execute(action -> {

			return lookup(id) //
			        .filter(State.CREATED::equals)
			        .flatMap(process -> start(action, process))
			        .flatMap(this::verify)
			        .flatMap(process -> finish(action, process));

		}).next().map(Process::getId);
	}
}