Skip to content

Commit

Permalink
update code examples and references
Browse files Browse the repository at this point in the history
  • Loading branch information
stephmarie17 committed Jul 31, 2024
1 parent 07d902b commit cf34824
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 38 deletions.
86 changes: 86 additions & 0 deletions examples/src/test/kotlin/TransactionsTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import com.mongodb.kotlin.client.coroutine.MongoClient
import com.mongodb.client.model.Filters.eq
import com.mongodb.client.model.Updates.inc
import config.getConfig
import kotlinx.coroutines.runBlocking
import org.junit.jupiter.api.AfterAll
import org.junit.jupiter.api.Assertions.assertNotNull
import org.junit.jupiter.api.BeforeAll
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.TestInstance

@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class TransactionsTest {

// :snippet-start: data-class
data class Account(
val accountId: String,
val amount: Int
)
// :snippet-end:

companion object {
val config = getConfig()
val client = MongoClient.create(config.connectionUri)
val database = client.getDatabase("bank")

@BeforeAll
@JvmStatic
fun beforeAll() {
runBlocking {
val savAcct = Account("9876", 900)
database.getCollection<Account>("savings_accounts").insertOne(savAcct)

val chkAcct = Account("9876", 50)
database.getCollection<Account>("checking_accounts").insertOne(chkAcct)
}
}

@AfterAll
@JvmStatic
fun afterAll() {
runBlocking {
database.drop()
client.close()
}
}
}

@Test
fun transactionTest() = runBlocking {
// :snippet-start: transaction-function
// Set up the session
val session = client.startSession()

try {
session.startTransaction()

val savingsColl = database
.getCollection<Account>("savings_accounts")
val checkingColl = database
.getCollection<Account>("checking_accounts")

savingsColl.findOneAndUpdate(
session,
eq(Account::accountId.name, "9876"),
inc(Account::amount.name, -100),
)

checkingColl.findOneAndUpdate(
session,
eq(Account::accountId.name, "9876"),
inc(Account::amount.name, 100)
)

// Commit the transaction
val result = session.commitTransaction()
println("Transaction committed.")
assertNotNull(result) // :remove:
} catch (error: Exception) {
println("An error occurred during the transaction: ${error.message}")
// Abort the transaction
session.abortTransaction()
}
// :snippet-end:
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
data class SavingsAccount(val accountId: String, val amount: Int)
data class CheckingAccount(val accountId: String, val amount: Int)

data class Account(
val accountId: String,
val amount: Int
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Set up the session
val session = client.startSession()

try {
session.startTransaction()

val savingsColl = database
.getCollection<Account>("savings_accounts")
val checkingColl = database
.getCollection<Account>("checking_accounts")

savingsColl.findOneAndUpdate(
session,
eq(Account::accountId.name, "9876"),
inc(Account::amount.name, -100),
)

checkingColl.findOneAndUpdate(
session,
eq(Account::accountId.name, "9876"),
inc(Account::amount.name, 100)
)

// Commit the transaction
val result = session.commitTransaction()
println("Transaction committed.")
} catch (error: Exception) {
println("An error occurred during the transaction: ${error.message}")
// Abort the transaction
session.abortTransaction()
}
32 changes: 0 additions & 32 deletions source/examples/generated/TransactionsTest.snippet.transaction.kt

This file was deleted.

4 changes: 1 addition & 3 deletions source/fundamentals/transactions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,9 @@ This example uses the following {+language+} data classes to model its documents

.. literalinclude:: /examples/generated/TransactionsTest.snippet.data-class.kt
:language: kotlin
:copyable:

.. literalinclude:: /examples/generated/TransactionsTest.snippet.transaction.kt
.. literalinclude:: /examples/generated/TransactionsTest.snippet.transaction-function.kt
:language: kotlin
:copyable:

Additional Information
----------------------
Expand Down

0 comments on commit cf34824

Please sign in to comment.