Saya menjalankan kode Anda terhadap server MongoDB saya sendiri (berjalan), dan saya dapat melihat kesalahan yang sama. Apa yang mengejutkan saya adalah bahwa kesalahan mengatakan "Menunggu 30000 ms sebelum waktu habis", tetapi kode selesai dalam waktu kurang dari 30 detik. Ini memberikan petunjuk tentang apa masalahnya.
Ingat ini async - oleh karena itu Anda tidak dapat mengharapkan semua operasi berjalan secara berurutan di utas yang sama. Apa yang sebenarnya terjadi adalah main
metode selesai sebelum panggilan Anda ke database selesai.
Jika Anda mengubah kode untuk menunggu hasil kembali sebelum diakhiri, Anda mendapatkan hasil yang jauh lebih masuk akal.
Kode:
public static void main(String[] args) throws InterruptedException {
CountDownLatch latch = new CountDownLatch(1);
// connect to the local database server,default:127.0.0.1:27017
MongoClient mongoClient = MongoClients.create();
// get handle to "testDB" database
MongoDatabase database = (MongoDatabase) mongoClient.getDatabase("testDB");
SingleResultCallback<Void> callbackWhenFinished = new SingleResultCallback<Void>() {
@Override
public void onResult(final Void result, final Throwable t) {
System.out.println("Operation Finished!");
latch.countDown();
}
};
// get a handle to the "test" collection
MongoCollection<Document> collection = database.getCollection("test");
collection.insertOne(new Document("lala", "hehe"), callbackWhenFinished);
latch.await();
}
Hasil:
Aug 11, 2015 9:31:34 AM com.mongodb.diagnostics.logging.JULLogger log
INFO: Cluster created with settings {hosts=[localhost:27017], mode=SINGLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms', maxWaitQueueSize=500}
Aug 11, 2015 9:31:35 AM com.mongodb.diagnostics.logging.JULLogger log
INFO: No server chosen by PrimaryServerSelector from cluster description ClusterDescription{type=UNKNOWN, connectionMode=SINGLE, all=[ServerDescription{address=localhost:27017, type=UNKNOWN, state=CONNECTING}]}. Waiting for 30000 ms before timing out
Aug 11, 2015 9:31:35 AM com.mongodb.diagnostics.logging.JULLogger log
INFO: Opened connection [connectionId{localValue:1, serverValue:4}] to localhost:27017
Aug 11, 2015 9:31:35 AM com.mongodb.diagnostics.logging.JULLogger log
INFO: Monitor thread successfully connected to server with description ServerDescription{address=localhost:27017, type=STANDALONE, state=CONNECTED, ok=true, version=ServerVersion{versionList=[3, 0, 2]}, minWireVersion=0, maxWireVersion=3, electionId=null, maxDocumentSize=16777216, roundTripTimeNanos=1281647}
Aug 11, 2015 9:31:35 AM com.mongodb.diagnostics.logging.JULLogger log
INFO: Opened connection [connectionId{localValue:2, serverValue:5}] to localhost:27017
Operation Finished!
Omong-omong, kode dapat disederhanakan lebih jauh, terutama karena Anda mengatakan Anda menggunakan Java 8:
public static void main(String[] args) throws InterruptedException {
CountDownLatch latch = new CountDownLatch(1);
// connect to the local database server,default:127.0.0.1:27017
MongoClient mongoClient = MongoClients.create();
// get handle to "testDB" database
MongoDatabase database = mongoClient.getDatabase("testDB");
// get a handle to the "test" collection
MongoCollection<Document> collection = database.getCollection("test");
collection.insertOne(new Document("lala", "hehe"),
(result, t) -> {
System.out.println("Operation Finished!");
latch.countDown();
});
latch.await();
}