Skip to content

Commit d57a4cf

Browse files
committed
implement RR feedback
1 parent ba73ae2 commit d57a4cf

File tree

2 files changed

+24
-11
lines changed

2 files changed

+24
-11
lines changed

source/includes/write/transaction.kt

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,36 @@
11
import com.mongodb.*
22
import com.mongodb.client.ClientSession
33
import com.mongodb.client.MongoClients
4-
import com.mongodb.client.MongoCollection
54
import org.bson.Document
65

6+
// start-data-class
7+
data class Restaurant(val name: String, val cuisine: String)
8+
// end-data-class
9+
710
fun main() {
811
// start-transaction
912
// Creates a new MongoClient with a connection string
1013
val client = MongoClients.create("<connection string>")
1114

1215
// Gets the database and collection
13-
val restaurantsDb = client.getDatabase("sample_restaurants")
14-
val restaurantsCollection: MongoCollection<Document> = restaurantsDb.getCollection("restaurants")
16+
val database = client.getDatabase("sample_restaurants")
17+
val collection = database.getCollection<Restaurant>("restaurants")
1518

16-
fun insertDocuments(session: ClientSession) {
19+
// Defines options and inserts restaurants into the collection
20+
fun insertRestaurantsInTransaction(session: ClientSession) {
1721
// Sets options for the collection operation within the transaction
18-
val restaurantsCollectionWithOptions = restaurantsCollection
22+
val restaurantsCollectionWithOptions = collection
1923
.withWriteConcern(WriteConcern("majority"))
2024
.withReadConcern(ReadConcern.LOCAL)
2125

22-
// Inserts documents within the transaction
26+
// Inserts restaurants within the transaction
2327
restaurantsCollectionWithOptions.insertOne(
2428
session,
25-
Document("name", "Kotlin Sync Pizza").append("cuisine", "Pizza")
29+
Restaurant("name", "Kotlin Sync Pizza").append("cuisine", "Pizza")
2630
)
2731
restaurantsCollectionWithOptions.insertOne(
2832
session,
29-
Document("name", "Kotlin Sync Burger").append("cuisine", "Burger")
33+
Restaurant("name", "Kotlin Sync Burger").append("cuisine", "Burger")
3034
)
3135
}
3236

@@ -40,7 +44,7 @@ fun main() {
4044

4145
// Uses the withTransaction method to start a transaction and run the given function
4246
session.withTransaction({
43-
insertDocuments(session)
47+
insertRestaurantsInTransaction(session)
4448
println("Transaction succeeded")
4549
}, txnOptions)
4650
} catch (e: Exception) {

source/write/transactions.txt

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,15 @@ from the :atlas:`Atlas sample datasets </sample-data>`. To learn how to create a
5555
free MongoDB Atlas cluster and load the sample datasets, see the
5656
:atlas:`Get Started with Atlas </getting-started>` guide.
5757

58+
The documents in this collection are modeled by the following {+language+} data class:
59+
60+
.. literalinclude:: /includes/write/transaction.kt
61+
:start-after: start-data-class
62+
:end-before: end-data-class
63+
:language: kotlin
64+
:copyable:
65+
:dedent:
66+
5867
Methods
5968
-------
6069

@@ -77,7 +86,7 @@ manage your transaction:
7786
this method, see the :manual:`startTransaction() page
7887
</reference/method/Session.startTransaction/>` in the Server manual.
7988
|
80-
| **Parameter**: ``transactionOptions``
89+
| **Parameter**: ``TransactionOptions``
8190

8291
* - ``abortTransaction()``
8392
- | Ends the active transaction for this session. Returns an
@@ -98,7 +107,7 @@ manage your transaction:
98107
- | Starts a transaction on this session and runs the given function within
99108
a transaction.
100109
|
101-
| **Parameters**: ``transactionBody, options``
110+
| **Parameters**: transaction body function, ``TransactionOptions``
102111

103112
Example
104113
-------

0 commit comments

Comments
 (0)