Skip to content

Commit f586494

Browse files
committed
Add java txn examples
1 parent 9cd900e commit f586494

File tree

4 files changed

+125
-0
lines changed

4 files changed

+125
-0
lines changed

source/includes/driver-examples/driver-example-transactions-intro-1.rst

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,20 @@
1111
:start-after: Start Transactions Intro Example 1
1212
:end-before: End Transactions Intro Example 1
1313

14+
15+
- id: java
16+
content: |
17+
18+
.. code-block:: java
19+
20+
try (ClientSession clientSession = client.startSession()) {
21+
clientSession.startTransaction();
22+
23+
employeesCollection.updateOne(clientSession,
24+
Filters.eq("employee", 3),
25+
Updates.set("status", "Inactive"));
26+
eventsCollection.insertOne(clientSession,
27+
new Document("employee", 3).append("status", new Document("new", "Inactive").append("old", "Active")));
28+
29+
commitWithRetry(clientSession);
30+
}

source/includes/driver-examples/driver-example-transactions-retry-1.rst

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,25 @@
3636
:start-after: Start Transactions Retry Example 1
3737
:end-before: End Transactions Retry Example 1
3838

39+
- id: java
40+
content: |
41+
.. code-block:: javascript
42+
43+
44+
void runTransactionWithRetry(Runnable transactional) {
45+
while (true) {
46+
try {
47+
transactional.run();
48+
break;
49+
} catch (MongoException e) {
50+
System.out.println("Transaction aborted. Caught exception during transaction.");
51+
52+
if (e.hasErrorLabel(MongoException.TRANSIENT_TRANSACTION_ERROR_LABEL)) {
53+
System.out.println("TransientTransactionError, aborting transaction and retrying ...");
54+
continue;
55+
} else {
56+
throw e;
57+
}
58+
}
59+
}
60+
}

source/includes/driver-examples/driver-example-transactions-retry-2.rst

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,25 @@
3636
:start-after: Start Transactions Retry Example 2
3737
:end-before: End Transactions Retry Example 2
3838

39+
- id: java
40+
content: |
41+
.. code-block:: java
42+
43+
void commitWithRetry(ClientSession clientSession) {
44+
while (true) {
45+
try {
46+
clientSession.commitTransaction();
47+
System.out.println("Transaction committed");
48+
break;
49+
} catch (MongoException e) {
50+
// can retry commit
51+
if (e.hasErrorLabel(MongoException.UNKNOWN_TRANSACTION_COMMIT_RESULT_LABEL)) {
52+
System.out.println("UnknownTransactionCommitResult, retrying commit operation ...");
53+
continue;
54+
} else {
55+
System.out.println("Exception during commit ...");
56+
throw e;
57+
}
58+
}
59+
}
60+
}

source/includes/driver-examples/driver-example-transactions-retry-3.rst

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,67 @@
8686
:start-after: Start Transactions Retry Example 3
8787
:end-before: End Transactions Retry Example 3
8888

89+
- id: java
90+
content: |
91+
92+
.. code-block:: java
93+
94+
void runTransactionWithRetry(Runnable transactional) {
95+
while (true) {
96+
try {
97+
transactional.run();
98+
break;
99+
} catch (MongoException e) {
100+
System.out.println("Transaction aborted. Caught exception during transaction.");
101+
102+
if (e.hasErrorLabel(MongoException.TRANSIENT_TRANSACTION_ERROR_LABEL)) {
103+
System.out.println("TransientTransactionError, aborting transaction and retrying ...");
104+
continue;
105+
} else {
106+
throw e;
107+
}
108+
}
109+
}
110+
}
111+
112+
void commitWithRetry(ClientSession clientSession) {
113+
while (true) {
114+
try {
115+
clientSession.commitTransaction();
116+
System.out.println("Transaction committed");
117+
break;
118+
} catch (MongoException e) {
119+
// can retry commit
120+
if (e.hasErrorLabel(MongoException.UNKNOWN_TRANSACTION_COMMIT_RESULT_LABEL)) {
121+
System.out.println("UnknownTransactionCommitResult, retrying commit operation ...");
122+
continue;
123+
} else {
124+
System.out.println("Exception during commit ...");
125+
throw e;
126+
}
127+
}
128+
}
129+
}
130+
131+
void updateEmployeeInfo() {
132+
MongoCollection<Document> employeesCollection = client.getDatabase("hr").getCollection("employees");
133+
MongoCollection<Document> eventsCollection = client.getDatabase("hr").getCollection("events");
134+
135+
try (ClientSession clientSession = client.startSession()) {
136+
clientSession.startTransaction();
137+
138+
employeesCollection.updateOne(clientSession,
139+
Filters.eq("employee", 3),
140+
Updates.set("status", "Inactive"));
141+
eventsCollection.insertOne(clientSession,
142+
new Document("employee", 3).append("status", new Document("new", "Inactive").append("old", "Active")));
143+
144+
commitWithRetry(clientSession);
145+
}
146+
}
147+
148+
149+
void updateEmployeeInfoWithRetry() {
150+
runTransactionWithRetry(this::updateEmployeeInfo);
151+
}
152+

0 commit comments

Comments
 (0)