File tree Expand file tree Collapse file tree 2 files changed +10
-10
lines changed Expand file tree Collapse file tree 2 files changed +10
-10
lines changed Original file line number Diff line number Diff line change 1
1
// Set up the MongoDB client and get the collection
2
2
suspend fun performTransaction (client : MongoClient ) {
3
3
client.startSession().use { session ->
4
- session.startTransaction() // Start the transaction
4
+ // Start the transaction
5
+ session.startTransaction()
5
6
try {
6
7
val database = client.getDatabase(" bank" )
7
8
@@ -18,6 +19,7 @@ suspend fun performTransaction(client: MongoClient) {
18
19
CheckingAccount ::accountId eq " 9876" ,
19
20
inc(CheckingAccount ::amount, 100 )
20
21
)
22
+
21
23
// Commit the transaction
22
24
session.commitTransaction()
23
25
println (" Transaction committed." )
@@ -28,12 +30,3 @@ suspend fun performTransaction(client: MongoClient) {
28
30
}
29
31
}
30
32
}
31
-
32
- data class SavingsAccount (val accountId : String , val amount : Int )
33
- data class CheckingAccount (val accountId : String , val amount : Int )
34
-
35
- fun main () = runBlocking {
36
- val uri = " <connection string uri>"
37
- val client = MongoClient .create(uri)
38
- performTransaction(client)
39
- }
Original file line number Diff line number Diff line change @@ -96,6 +96,13 @@ and commit a changes to existing documents:
96
96
#. Update the specified documents, then use the ``commitTransaction()`` method if all
97
97
operations succeed, or ``abortTransaction()`` if any operations fail.
98
98
99
+ This example uses the following {+language+} data classes to model its documents:
100
+
101
+ .. code-block:: kotlin
102
+
103
+ data class SavingsAccount(val accountId: String, val amount: Int)
104
+ data class CheckingAccount(val accountId: String, val amount: Int)
105
+
99
106
.. literalinclude:: /examples/generated/TransactionsTest.snippet.transaction.kt
100
107
:language: kotlin
101
108
:copyable:
You can’t perform that action at this time.
0 commit comments