@@ -30,56 +30,59 @@ public void shouldExecuteTransactions() {
3030 // startingMongoDBContainer {
3131 mongoDBContainer .start ();
3232 // }
33+ executeTx (mongoDBContainer );
34+ }
35+ }
3336
34- final String mongoRsUrl = mongoDBContainer .getReplicaSetUrl ();
35- assertThat (mongoRsUrl ).isNotNull ();
36- final String connectionString = mongoDBContainer .getConnectionString ();
37- final MongoClient mongoSyncClientBase = MongoClients .create (connectionString );
38- final MongoClient mongoSyncClient = MongoClients .create (mongoRsUrl );
39- mongoSyncClient
40- .getDatabase ("mydb1" )
41- .getCollection ("foo" )
42- .withWriteConcern (WriteConcern .MAJORITY )
43- .insertOne (new Document ("abc" , 0 ));
44- mongoSyncClient
45- .getDatabase ("mydb2" )
46- .getCollection ("bar" )
47- .withWriteConcern (WriteConcern .MAJORITY )
48- .insertOne (new Document ("xyz" , 0 ));
49- mongoSyncClientBase
50- .getDatabase ("mydb3" )
51- .getCollection ("baz" )
52- .withWriteConcern (WriteConcern .MAJORITY )
53- .insertOne (new Document ("def" , 0 ));
37+ private void executeTx (MongoDBContainer mongoDBContainer ) {
38+ final String mongoRsUrl = mongoDBContainer .getReplicaSetUrl ();
39+ assertThat (mongoRsUrl ).isNotNull ();
40+ final String connectionString = mongoDBContainer .getConnectionString ();
41+ final MongoClient mongoSyncClientBase = MongoClients .create (connectionString );
42+ final MongoClient mongoSyncClient = MongoClients .create (mongoRsUrl );
43+ mongoSyncClient
44+ .getDatabase ("mydb1" )
45+ .getCollection ("foo" )
46+ .withWriteConcern (WriteConcern .MAJORITY )
47+ .insertOne (new Document ("abc" , 0 ));
48+ mongoSyncClient
49+ .getDatabase ("mydb2" )
50+ .getCollection ("bar" )
51+ .withWriteConcern (WriteConcern .MAJORITY )
52+ .insertOne (new Document ("xyz" , 0 ));
53+ mongoSyncClientBase
54+ .getDatabase ("mydb3" )
55+ .getCollection ("baz" )
56+ .withWriteConcern (WriteConcern .MAJORITY )
57+ .insertOne (new Document ("def" , 0 ));
5458
55- final ClientSession clientSession = mongoSyncClient .startSession ();
56- final TransactionOptions txnOptions = TransactionOptions
57- .builder ()
58- .readPreference (ReadPreference .primary ())
59- .readConcern (ReadConcern .LOCAL )
60- .writeConcern (WriteConcern .MAJORITY )
61- .build ();
59+ final ClientSession clientSession = mongoSyncClient .startSession ();
60+ final TransactionOptions txnOptions = TransactionOptions
61+ .builder ()
62+ .readPreference (ReadPreference .primary ())
63+ .readConcern (ReadConcern .LOCAL )
64+ .writeConcern (WriteConcern .MAJORITY )
65+ .build ();
6266
63- final String trxResult = "Inserted into collections in different databases" ;
67+ final String trxResult = "Inserted into collections in different databases" ;
6468
65- TransactionBody <String > txnBody = () -> {
66- final MongoCollection <Document > coll1 = mongoSyncClient .getDatabase ("mydb1" ).getCollection ("foo" );
67- final MongoCollection <Document > coll2 = mongoSyncClient .getDatabase ("mydb2" ).getCollection ("bar" );
69+ TransactionBody <String > txnBody = () -> {
70+ final MongoCollection <Document > coll1 = mongoSyncClient .getDatabase ("mydb1" ).getCollection ("foo" );
71+ final MongoCollection <Document > coll2 = mongoSyncClient .getDatabase ("mydb2" ).getCollection ("bar" );
6872
69- coll1 .insertOne (clientSession , new Document ("abc" , 1 ));
70- coll2 .insertOne (clientSession , new Document ("xyz" , 999 ));
71- return trxResult ;
72- };
73+ coll1 .insertOne (clientSession , new Document ("abc" , 1 ));
74+ coll2 .insertOne (clientSession , new Document ("xyz" , 999 ));
75+ return trxResult ;
76+ };
7377
74- try {
75- final String trxResultActual = clientSession .withTransaction (txnBody , txnOptions );
76- assertThat (trxResultActual ).isEqualTo (trxResult );
77- } catch (RuntimeException re ) {
78- throw new IllegalStateException (re .getMessage (), re );
79- } finally {
80- clientSession .close ();
81- mongoSyncClient .close ();
82- }
78+ try {
79+ final String trxResultActual = clientSession .withTransaction (txnBody , txnOptions );
80+ assertThat (trxResultActual ).isEqualTo (trxResult );
81+ } catch (RuntimeException re ) {
82+ throw new IllegalStateException (re .getMessage (), re );
83+ } finally {
84+ clientSession .close ();
85+ mongoSyncClient .close ();
8386 }
8487 }
8588
@@ -103,6 +106,7 @@ public void shouldTestDatabaseName() {
103106 public void supportsMongoDB_6 () {
104107 try (final MongoDBContainer mongoDBContainer = new MongoDBContainer ("mongo:6.0.1" )) {
105108 mongoDBContainer .start ();
109+ executeTx (mongoDBContainer );
106110 }
107111 }
108112}
0 commit comments