Skip to content

Commit cf34824

Browse files
committed
update code examples and references
1 parent 07d902b commit cf34824

File tree

5 files changed

+122
-38
lines changed

5 files changed

+122
-38
lines changed
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
import com.mongodb.kotlin.client.coroutine.MongoClient
2+
import com.mongodb.client.model.Filters.eq
3+
import com.mongodb.client.model.Updates.inc
4+
import config.getConfig
5+
import kotlinx.coroutines.runBlocking
6+
import org.junit.jupiter.api.AfterAll
7+
import org.junit.jupiter.api.Assertions.assertNotNull
8+
import org.junit.jupiter.api.BeforeAll
9+
import org.junit.jupiter.api.Test
10+
import org.junit.jupiter.api.TestInstance
11+
12+
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
13+
class TransactionsTest {
14+
15+
// :snippet-start: data-class
16+
data class Account(
17+
val accountId: String,
18+
val amount: Int
19+
)
20+
// :snippet-end:
21+
22+
companion object {
23+
val config = getConfig()
24+
val client = MongoClient.create(config.connectionUri)
25+
val database = client.getDatabase("bank")
26+
27+
@BeforeAll
28+
@JvmStatic
29+
fun beforeAll() {
30+
runBlocking {
31+
val savAcct = Account("9876", 900)
32+
database.getCollection<Account>("savings_accounts").insertOne(savAcct)
33+
34+
val chkAcct = Account("9876", 50)
35+
database.getCollection<Account>("checking_accounts").insertOne(chkAcct)
36+
}
37+
}
38+
39+
@AfterAll
40+
@JvmStatic
41+
fun afterAll() {
42+
runBlocking {
43+
database.drop()
44+
client.close()
45+
}
46+
}
47+
}
48+
49+
@Test
50+
fun transactionTest() = runBlocking {
51+
// :snippet-start: transaction-function
52+
// Set up the session
53+
val session = client.startSession()
54+
55+
try {
56+
session.startTransaction()
57+
58+
val savingsColl = database
59+
.getCollection<Account>("savings_accounts")
60+
val checkingColl = database
61+
.getCollection<Account>("checking_accounts")
62+
63+
savingsColl.findOneAndUpdate(
64+
session,
65+
eq(Account::accountId.name, "9876"),
66+
inc(Account::amount.name, -100),
67+
)
68+
69+
checkingColl.findOneAndUpdate(
70+
session,
71+
eq(Account::accountId.name, "9876"),
72+
inc(Account::amount.name, 100)
73+
)
74+
75+
// Commit the transaction
76+
val result = session.commitTransaction()
77+
println("Transaction committed.")
78+
assertNotNull(result) // :remove:
79+
} catch (error: Exception) {
80+
println("An error occurred during the transaction: ${error.message}")
81+
// Abort the transaction
82+
session.abortTransaction()
83+
}
84+
// :snippet-end:
85+
}
86+
}
Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1-
data class SavingsAccount(val accountId: String, val amount: Int)
2-
data class CheckingAccount(val accountId: String, val amount: Int)
3-
1+
data class Account(
2+
val accountId: String,
3+
val amount: Int
4+
)
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Set up the session
2+
val session = client.startSession()
3+
4+
try {
5+
session.startTransaction()
6+
7+
val savingsColl = database
8+
.getCollection<Account>("savings_accounts")
9+
val checkingColl = database
10+
.getCollection<Account>("checking_accounts")
11+
12+
savingsColl.findOneAndUpdate(
13+
session,
14+
eq(Account::accountId.name, "9876"),
15+
inc(Account::amount.name, -100),
16+
)
17+
18+
checkingColl.findOneAndUpdate(
19+
session,
20+
eq(Account::accountId.name, "9876"),
21+
inc(Account::amount.name, 100)
22+
)
23+
24+
// Commit the transaction
25+
val result = session.commitTransaction()
26+
println("Transaction committed.")
27+
} catch (error: Exception) {
28+
println("An error occurred during the transaction: ${error.message}")
29+
// Abort the transaction
30+
session.abortTransaction()
31+
}

source/examples/generated/TransactionsTest.snippet.transaction.kt

Lines changed: 0 additions & 32 deletions
This file was deleted.

source/fundamentals/transactions.txt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,11 +100,9 @@ This example uses the following {+language+} data classes to model its documents
100100

101101
.. literalinclude:: /examples/generated/TransactionsTest.snippet.data-class.kt
102102
:language: kotlin
103-
:copyable:
104103

105-
.. literalinclude:: /examples/generated/TransactionsTest.snippet.transaction.kt
104+
.. literalinclude:: /examples/generated/TransactionsTest.snippet.transaction-function.kt
106105
:language: kotlin
107-
:copyable:
108106

109107
Additional Information
110108
----------------------

0 commit comments

Comments
 (0)