MongoDB java驱动程序 - com.mongodb.MongoSocketReadTimeoutException:接收消息时超时

时间:2018-02-07 04:47:54

标签: java mongodb exception driver synchronous

尝试连接到MongoDB的本地实例时遇到一些问题。下面的堆栈跟踪:

    Feb 07, 2018 5:29:06 PM com.mongodb.diagnostics.logging.JULLogger log
INFO: Cluster created with settings {hosts=[127.0.0.1:27017], mode=SINGLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms', maxWaitQueueSize=500}
Feb 07, 2018 5:29:07 PM com.mongodb.diagnostics.logging.JULLogger log
INFO: Cluster description not yet available. Waiting for 30000 ms before timing out
Feb 07, 2018 5:29:27 PM com.mongodb.diagnostics.logging.JULLogger log
INFO: Exception in monitor thread while connecting to server 127.0.0.1:27017
com.mongodb.MongoSocketReadTimeoutException: Timeout while receiving message
    at com.mongodb.connection.InternalStreamConnection.translateReadException(InternalStreamConnection.java:530)
    at com.mongodb.connection.InternalStreamConnection.receiveMessage(InternalStreamConnection.java:421)
    at com.mongodb.connection.InternalStreamConnection.receiveCommandMessageResponse(InternalStreamConnection.java:290)
    at com.mongodb.connection.InternalStreamConnection.sendAndReceive(InternalStreamConnection.java:255)
    at com.mongodb.connection.CommandHelper.sendAndReceive(CommandHelper.java:84)
    at com.mongodb.connection.CommandHelper.executeCommand(CommandHelper.java:34)
    at com.mongodb.connection.InternalStreamConnectionInitializer.initializeConnectionDescription(InternalStreamConnectionInitializer.java:91)
    at com.mongodb.connection.InternalStreamConnectionInitializer.initialize(InternalStreamConnectionInitializer.java:51)
    at com.mongodb.connection.InternalStreamConnection.open(InternalStreamConnection.java:127)
    at com.mongodb.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:114)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.net.SocketTimeoutException: Read timed out
    at java.net.SocketInputStream.socketRead0(Native Method)
    at java.net.SocketInputStream.socketRead(SocketInputStream.java:116)
    at java.net.SocketInputStream.read(SocketInputStream.java:170)
    at java.net.SocketInputStream.read(SocketInputStream.java:141)
    at com.mongodb.connection.SocketStream.read(SocketStream.java:84)
    at com.mongodb.connection.InternalStreamConnection.receiveResponseBuffers(InternalStreamConnection.java:547)
    at com.mongodb.connection.InternalStreamConnection.receiveMessage(InternalStreamConnection.java:418)
    ... 9 more

com.mongodb.MongoTimeoutException: Timed out after 30000 ms while waiting to connect. Client view of cluster state is {type=UNKNOWN, servers=[{address=127.0.0.1:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketReadTimeoutException: Timeout while receiving message}, caused by {java.net.SocketTimeoutException: Read timed out}}]
    at com.mongodb.connection.BaseCluster.getDescription(BaseCluster.java:167)
    at com.mongodb.Mongo.getConnectedClusterDescription(Mongo.java:881)
    at com.mongodb.Mongo.createClientSession(Mongo.java:873)
    at com.mongodb.Mongo$3.getClientSession(Mongo.java:862)
    at com.mongodb.Mongo$3.execute(Mongo.java:830)
    at com.mongodb.MongoCollectionImpl.executeSingleWriteRequest(MongoCollectionImpl.java:1025)
    at com.mongodb.MongoCollectionImpl.executeInsertOne(MongoCollectionImpl.java:513)
    at com.mongodb.MongoCollectionImpl.insertOne(MongoCollectionImpl.java:493)
    at com.mongodb.MongoCollectionImpl.insertOne(MongoCollectionImpl.java:487)
    at database.Setup.insertAll(Setup.java:46)
    at database.Setup.main(Setup.java:64)

我正在使用Windows 10,下载并安装了MongoDB,创建了数据目录并从命令行启动了mongo。

我可以使用mongo shell连接并列出详细信息:

 db.hostInfo();
{
        "system" : {
                "currentTime" : ISODate("2018-02-07T04:32:08.428Z"),
                "hostname" : "DESKTOP-KH7KS0F",
                "cpuAddrSize" : 64,
                "memSizeMB" : 16294,
                "numCores" : 12,
                "cpuArch" : "x86_64",
                "numaEnabled" : false
        },
        "os" : {
                "type" : "Windows",
                "name" : "Microsoft Windows 10",
                "version" : "10.0 (build 16299)"
        },
        "extra" : {
                "pageSize" : NumberLong(4096)
        },
        "ok" : 1
}
> db.stats()
{
        "db" : "test",
        "collections" : 0,
        "views" : 0,
        "objects" : 0,
        "avgObjSize" : 0,
        "dataSize" : 0,
        "storageSize" : 0,
        "numExtents" : 0,
        "indexes" : 0,
        "indexSize" : 0,
        "fileSize" : 0,
        "fsUsedSize" : 0,
        "fsTotalSize" : 0,
        "ok" : 1
}
> db.version()
3.6.2

我正在使用同步驱动程序并将其添加为maven依赖项:

    <dependency>
        <groupId>org.mongodb</groupId>
        <artifactId>mongodb-driver</artifactId>
        <version>3.6.2</version>
    </dependency>

这是从命令行运行Mongo的输出:

2018-02-06T19:59:53.808-0700 I CONTROL  [initandlisten] MongoDB starting : pid=14308 port=27017 dbpath=C:\data\db\ 64-bit host=DESKTOP-KH7KS0F
2018-02-06T19:59:53.808-0700 I CONTROL  [initandlisten] targetMinOS: Windows 7/Windows Server 2008 R2
2018-02-06T19:59:53.808-0700 I CONTROL  [initandlisten] db version v3.6.2
2018-02-06T19:59:53.808-0700 I CONTROL  [initandlisten] git version: 489d177dbd0f0420a8ca04d39fd78d0a2c539420
2018-02-06T19:59:53.809-0700 I CONTROL  [initandlisten] OpenSSL version: OpenSSL 1.0.1u-fips  22 Sep 2016
2018-02-06T19:59:53.809-0700 I CONTROL  [initandlisten] allocator: tcmalloc
2018-02-06T19:59:53.810-0700 I CONTROL  [initandlisten] modules: none
2018-02-06T19:59:53.810-0700 I CONTROL  [initandlisten] build environment:
2018-02-06T19:59:53.810-0700 I CONTROL  [initandlisten]     distmod: 2008plus-ssl
2018-02-06T19:59:53.810-0700 I CONTROL  [initandlisten]     distarch: x86_64
2018-02-06T19:59:53.811-0700 I CONTROL  [initandlisten]     target_arch: x86_64
2018-02-06T19:59:53.811-0700 I CONTROL  [initandlisten] options: {}
2018-02-06T19:59:53.814-0700 I STORAGE  [initandlisten] wiredtiger_open config: create,cache_size=7635M,session_max=20000,eviction=(threads_min=4,threads_max=4),config_base=false,statistics=(fast),log=(enabled=true,archive=true,path=journal,compressor=snappy),file_manager=(close_idle_time=100000),statistics_log=(wait=0),verbose=(recovery_progress),
2018-02-06T19:59:53.875-0700 I CONTROL  [initandlisten]
2018-02-06T19:59:53.875-0700 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2018-02-06T19:59:53.877-0700 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2018-02-06T19:59:53.878-0700 I CONTROL  [initandlisten]
2018-02-06T19:59:53.878-0700 I CONTROL  [initandlisten] ** WARNING: This server is bound to localhost.
2018-02-06T19:59:53.878-0700 I CONTROL  [initandlisten] **          Remote systems will be unable to connect to this server.
2018-02-06T19:59:53.878-0700 I CONTROL  [initandlisten] **          Start the server with --bind_ip <address> to specify which IP
2018-02-06T19:59:53.879-0700 I CONTROL  [initandlisten] **          addresses it should serve responses from, or with --bind_ip_all to
2018-02-06T19:59:53.879-0700 I CONTROL  [initandlisten] **          bind to all interfaces. If this behavior is desired, start the
2018-02-06T19:59:53.879-0700 I CONTROL  [initandlisten] **          server with --bind_ip 127.0.0.1 to disable this warning.
2018-02-06T19:59:53.880-0700 I CONTROL  [initandlisten]
2018-02-06T19:59:53.880-0700 I CONTROL  [initandlisten]
2018-02-06T19:59:53.880-0700 I CONTROL  [initandlisten] ** WARNING: The file system cache of this machine is configured to be greater than 40% of the total memory. This can lead to increased memory pressure and poor performance.
2018-02-06T19:59:53.880-0700 I CONTROL  [initandlisten] See http://dochub.mongodb.org/core/wt-windows-system-file-cache
2018-02-06T19:59:53.881-0700 I CONTROL  [initandlisten]
2018-02-07T15:59:53.882+1300 I STORAGE  [initandlisten] createCollection: admin.system.version with provided UUID: c91b385a-579d-400f-92f6-1442d6ef5887
2018-02-07T15:59:53.892+1300 I COMMAND  [initandlisten] setting featureCompatibilityVersion to 3.6
2018-02-07T15:59:53.895+1300 I STORAGE  [initandlisten] createCollection: local.startup_log with generated UUID: ccbcb30a-7460-463b-9334-c5d7d531e473
2018-02-07T15:59:54.081+1300 I FTDC     [initandlisten] Initializing full-time diagnostic data capture with directory 'C:/data/db/diagnostic.data'
2018-02-07T15:59:54.083+1300 I NETWORK  [initandlisten] waiting for connections on port 27017
2018-02-07T16:01:11.179+1300 I NETWORK  [listener] connection accepted from 127.0.0.1:61738 #1 (1 connection now open)

最后是java代码:

   public static void init() {
        try {
            Database.mongoClient = new MongoClient(DatabaseConfig.DB_HOST, DatabaseConfig.DB_PORT);
        } catch (Exception e) {
            Database.logger.log(Level.INFO, String.format("Unable to connect to database using connection : %s %d", DatabaseConfig.DB_HOST, DatabaseConfig.DB_PORT));
            e.printStackTrace();
            Database.mongoClient.close();
            throw e;
        }
    }

0 个答案:

没有答案