Last active
April 20, 2018 18:16
-
-
Save jyemin/7031cc235759e4619ab0ab342f22db37 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright 2018 MongoDB, Inc. | |
* | |
* Licensed under the Apache License, Version 2.0 (the "License"); | |
* you may not use this file except in compliance with the License. | |
* You may obtain a copy of the License at | |
* | |
* http://www.apache.org/licenses/LICENSE-2.0 | |
* | |
* Unless required by applicable law or agreed to in writing, software | |
* distributed under the License is distributed on an "AS IS" BASIS, | |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
* See the License for the specific language governing permissions and | |
* limitations under the License. | |
* | |
*/ | |
import com.mongodb.MongoClient; | |
import com.mongodb.MongoClientURI; | |
import com.mongodb.MongoServerException; | |
import com.mongodb.MongoSocketException; | |
import com.mongodb.TransactionOptions; | |
import com.mongodb.WriteConcern; | |
import com.mongodb.client.ClientSession; | |
import com.mongodb.client.MongoCollection; | |
import com.mongodb.client.MongoDatabase; | |
import org.bson.Document; | |
import java.util.Arrays; | |
import static com.mongodb.client.model.Filters.eq; | |
import static com.mongodb.client.model.Updates.inc; | |
public class BetaExamples { | |
static MongoClientURI uri; | |
static MongoClient client; | |
static MongoDatabase database; | |
static MongoCollection<Document> inventory; | |
static MongoCollection<Document> shipment; | |
public static void main(String[] args) throws InterruptedException { | |
// setup code | |
uri = new MongoClientURI("mongodb://localhost,localhost:27018"); | |
client = new MongoClient(uri); | |
database = client.getDatabase("test"); | |
// drop the database so that we can run this repeatedly | |
database.drop(); | |
// create the collections in advance so there is no attempt to implicitly create them within the transaction | |
database.createCollection("inventory"); | |
database.createCollection("shipment"); | |
inventory = database.getCollection("inventory"); | |
shipment = database.getCollection("shipment"); | |
inventory.insertOne(new Document("sku", "abc123").append("qty", 500)); | |
// Start Beta Transaction Example 1 | |
try (ClientSession clientSession = client.startSession()) { | |
clientSession.startTransaction(TransactionOptions.builder().writeConcern(WriteConcern.MAJORITY).build()); | |
inventory.updateOne(clientSession, eq("sku", "abc123"), inc("qty", -100)); | |
shipment.insertOne(clientSession, new Document("sku", "abc123").append("qty", 100)); | |
clientSession.commitTransaction(); | |
} | |
// End Beta Transaction Example 1 | |
// Start Beta Transaction Example 2 | |
// In the following block, the following write concerns are used: | |
// the updateOne and insertOne operations uses w = 1, the transaction commit/abort uses w = "majority". | |
try (ClientSession clientSession = client.startSession()) { | |
clientSession.startTransaction(TransactionOptions.builder().writeConcern(WriteConcern.MAJORITY).build()); | |
inventory.updateOne(clientSession, eq("sku", "abc123"), inc("qty", -100)); | |
// The write concern specified here will be ignored | |
shipment.withWriteConcern(WriteConcern.MAJORITY) | |
.insertOne(clientSession, new Document("sku", "abc123").append("qty", 100)); | |
clientSession.commitTransaction(); | |
} | |
// End Beta Transaction Example 2 | |
// Start Beta Transaction Example 3 | |
try { | |
runShipmentTransaction(); | |
} catch (MongoSocketException e) { | |
runShipmentTransaction(); | |
} catch (MongoServerException e) { | |
if (Arrays.asList(112, 244, 251).contains(e.getCode())) { | |
runShipmentTransaction(); | |
} else { | |
throw e; | |
} | |
} | |
// End Beta Transaction Example 3 | |
client.close(); | |
} | |
static void runShipmentTransaction() { | |
try (ClientSession clientSession = client.startSession()) { | |
clientSession.startTransaction(); | |
inventory.updateOne(clientSession, eq("sku", "abc123"), inc("qty", -100)); | |
shipment.insertOne(clientSession, new Document("sku", "abc123").append("qty", 100)); | |
clientSession.commitTransaction(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add a comment explaining the error codes?
// Retry the transaction after WriteConflict, TransactionAborted, orNoSuchTransaction.