Source:
<code>public void shouldExecuteTransactions() { try ( <START> final MongoDBContainer mongoDBContainer = new MongoDBContainer(MONGO_IMAGE) <END> ) { mongoDBContainer.start(); final String mongoRsUrl = mongoDBContainer.getReplicaSetUrl(); assertNotNull(mongoRsUrl); final MongoClient mongoSyncClient = MongoClients.create(mongoRsUrl); mongoSyncClient.getDatabase("mydb1").getCollection("foo") .withWriteConcern(WriteConcern.MAJORITY).insertOne(new Document("abc", 0)); mongoSyncClient.getDatabase("mydb2").getCollection("bar") .withWriteConcern(WriteConcern.MAJORITY).insertOne(new Document("xyz", 0)); final ClientSession clientSession = mongoSyncClient.startSession(); final TransactionOptions txnOptions = TransactionOptions.builder() .readPreference(ReadPreference.primary()) .readConcern(ReadConcern.LOCAL) .writeConcern(WriteConcern.MAJORITY) .build(); final String trxResult = "Inserted into collections in different databases"; TransactionBody<String> txnBody = () -> { final MongoCollection<Document> coll1 = mongoSyncClient.getDatabase("mydb1").getCollection("foo"); final MongoCollection<Document> coll2 = mongoSyncClient.getDatabase("mydb2").getCollection("bar"); coll1.insertOne(clientSession, new Document("abc", 1)); coll2.insertOne(clientSession, new Document("xyz", 999)); return trxResult; }; try { final String trxResultActual = clientSession.withTransaction(txnBody, txnOptions); assertEquals(trxResult, trxResultActual); } catch (RuntimeException re) { throw new IllegalStateException(re.getMessage(), re); } finally { clientSession.close(); mongoSyncClient.close(); } } }</code><technical_language>inline image here, in docs</technical_language>

Target:
public void shouldExecuteTransactions() { try ( final MongoDBContainer mongoDBContainer = new MongoDBContainer(DockerImageName.parse("mongo:4.0.10")) ) { mongoDBContainer.start(); final String mongoRsUrl = mongoDBContainer.getReplicaSetUrl(); assertNotNull(mongoRsUrl); final MongoClient mongoSyncClient = MongoClients.create(mongoRsUrl); mongoSyncClient.getDatabase("mydb1").getCollection("foo") .withWriteConcern(WriteConcern.MAJORITY).insertOne(new Document("abc", 0)); mongoSyncClient.getDatabase("mydb2").getCollection("bar") .withWriteConcern(WriteConcern.MAJORITY).insertOne(new Document("xyz", 0)); final ClientSession clientSession = mongoSyncClient.startSession(); final TransactionOptions txnOptions = TransactionOptions.builder() .readPreference(ReadPreference.primary()) .readConcern(ReadConcern.LOCAL) .writeConcern(WriteConcern.MAJORITY) .build(); final String trxResult = "Inserted into collections in different databases"; TransactionBody<String> txnBody = () -> { final MongoCollection<Document> coll1 = mongoSyncClient.getDatabase("mydb1").getCollection("foo"); final MongoCollection<Document> coll2 = mongoSyncClient.getDatabase("mydb2").getCollection("bar"); coll1.insertOne(clientSession, new Document("abc", 1)); coll2.insertOne(clientSession, new Document("xyz", 999)); return trxResult; }; try { final String trxResultActual = clientSession.withTransaction(txnBody, txnOptions); assertEquals(trxResult, trxResultActual); } catch (RuntimeException re) { throw new IllegalStateException(re.getMessage(), re); } finally { clientSession.close(); mongoSyncClient.close(); } } }