Guest User

Untitled

a guest
May 20th, 2018
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.57 KB | None | 0 0
  1. import com.mongodb.ConnectionString;
  2. import com.mongodb.MongoClientSettings;
  3. import com.mongodb.MongoException;
  4. import com.mongodb.client.ClientSession;
  5. import com.mongodb.client.MongoClient;
  6. import com.mongodb.client.MongoClients;
  7. import com.mongodb.client.MongoCollection;
  8. import com.mongodb.client.MongoDatabase;
  9. import org.bson.Document;
  10. import org.bson.types.ObjectId;
  11.  
  12. import java.util.Random;
  13. import java.util.concurrent.ExecutorService;
  14. import java.util.concurrent.Executors;
  15. import java.util.concurrent.TimeUnit;
  16.  
  17. public class WriteConflictTest {
  18. public static void main(String[] args) throws InterruptedException {
  19. ConnectionString connectionString = new ConnectionString("mongodb://localhost,localhost:27018");
  20. MongoClient client = MongoClients.create(MongoClientSettings.builder()
  21. .applyConnectionString(connectionString)
  22. // .addCommandListener(new CommandListener() {
  23. // @Override
  24. // public void commandStarted(final CommandStartedEvent event) {
  25. // System.out.println(event.getCommand());
  26. // }
  27. //
  28. // @Override
  29. // public void commandSucceeded(final CommandSucceededEvent event) {
  30. // System.out.println(event.getResponse());
  31. // System.out.println();
  32. // }
  33. //
  34. // @Override
  35. // public void commandFailed(final CommandFailedEvent event) {
  36. // System.out.println(event.getThrowable().getMessage());
  37. // System.out.println();
  38. // }
  39. // })
  40. .build());
  41.  
  42. MongoDatabase database = client.getDatabase("test");
  43.  
  44. String collectionName = "transactions";
  45.  
  46. MongoCollection<Document> collection = database.getCollection(collectionName);
  47.  
  48. Document document = new Document("_id", new ObjectId());
  49.  
  50. setUp(database, collection, document);
  51.  
  52. ExecutorService executor = Executors.newFixedThreadPool(2);
  53.  
  54. executor.submit(() -> {
  55. replaceDoc(client, collection, document);
  56. });
  57.  
  58. executor.submit(() -> {
  59. replaceDoc(client, collection, document);
  60. });
  61.  
  62. executor.awaitTermination(Long.MAX_VALUE, TimeUnit.SECONDS);
  63. System.out.println("EXITING");
  64. }
  65.  
  66. private static void setUp(final MongoDatabase database, final MongoCollection<Document> collection, final Document document) {
  67. collection.drop();
  68. database.createCollection(collection.getNamespace().getCollectionName());
  69.  
  70. collection.insertOne(document);
  71. }
  72.  
  73. private static void replaceDoc(final MongoClient client, final MongoCollection<Document> collection, final Document filter) {
  74. Document replacement = new Document(filter);
  75. for (int i = 0; i < Integer.MAX_VALUE; i++) {
  76. replacement.put("mod", Thread.currentThread().getName() + ": " + i);
  77. try (ClientSession session = client.startSession()) {
  78. session.startTransaction();
  79. collection.replaceOne(session, filter, replacement);
  80. session.commitTransaction();
  81. System.out.println("Committed on thread" + Thread.currentThread().getName() + " with mod: " + replacement.get("mod"));
  82. } catch (MongoException e) {
  83. System.out.println("Failed to commit on thread" + Thread.currentThread().getName() + ": " + e.getMessage());
  84. } catch (Exception e) {
  85. e.printStackTrace();
  86. }
  87. }
  88. }
  89.  
  90.  
  91. }
Add Comment
Please, Sign In to add comment