生产我们使用java(连接池)与Mongo DB连接。每天我们收到差不多500个错误请求(MongoSocketReadTimeoutException),我们也没有使用任何复杂的查询。是因为陈旧的Mongo连接? 我不明白代码(或)Mongo DB的慢度问题。请查看我在下面提供的代码建议任何需要添加以下代码的参数(或)任何需要更改的内容,如果有任何建议/帮助真的很感激。
at 2018-03-07 19:52:43 ERROR ::Error while connecting the Mongo DB {}
com.mongodb.MongoSocketReadTimeoutException: Timeout while receiving message
at com.mongodb.connection.InternalStreamConnection.translateReadException(InternalStreamConnection.java:474)
at com.mongodb.connection.InternalStreamConnection.receiveMessage(InternalStreamConnection.java:225)
at com.mongodb.connection.UsageTrackingInternalConnection.receiveMessage(UsageTrackingInternalConnection.java:102)
at com.mongodb.connection.DefaultConnectionPool$PooledConnection.receiveMessage(DefaultConnectionPool.java:435)
at com.mongodb.connection.CommandProtocol.execute(CommandProtocol.java:112)
at com.mongodb.connection.DefaultServer$DefaultServerProtocolExecutor.execute(DefaultServer.java:159)
at com.mongodb.connection.DefaultServerConnection.executeProtocol(DefaultServerConnection.java:286)
at com.mongodb.connection.DefaultServerConnection.command(DefaultServerConnection.java:173)
at com.mongodb.operation.CommandOperationHelper.executeWrappedCommandProtocol(CommandOperationHelper.java:215)
at com.mongodb.operation.CommandOperationHelper.executeWrappedCommandProtocol(CommandOperationHelper.java:206)
at com.mongodb.operation.CommandOperationHelper.executeWrappedCommandProtocol(CommandOperationHelper.java:112)
at com.mongodb.operation.FindOperation$1.call(FindOperation.java:487)
at com.mongodb.operation.FindOperation$1.call(FindOperation.java:482)
at com.mongodb.operation.OperationHelper.withConnectionSource(OperationHelper.java:239)
at com.mongodb.operation.OperationHelper.withConnection(OperationHelper.java:212)
at com.mongodb.operation.FindOperation.execute(FindOperation.java:482)
at com.mongodb.operation.FindOperation.execute(FindOperation.java:79)
at com.mongodb.Mongo.execute(Mongo.java:772)
at com.mongodb.Mongo$2.execute(Mongo.java:759)
at com.mongodb.FindIterableImpl$FindOperationIterable.first(FindIterableImpl.java:207)
at com.mongodb.FindIterableImpl.first(FindIterableImpl.java:148)
at com.tecnotree.bom.validation.dao.MongoManager.getJsonObject(MongoManager.java:88)
at com.tecnotree.bom.validation.dao.CustomerMasterDao.getService(CustomerMasterDao.java:48)
at com.tecnotree.bom.validation.service.ValidationService.processValidation(ValidationService.java:214)
at com.tecnotree.bom.validation.service.ValidationService.processRequest(ValidationService.java:125)
at sun.reflect.GeneratedMethodAccessor185.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.sun.jersey.spi.container.JavaMethodInvokerFactory$1.invoke(JavaMethodInvokerFactory.java:60)
at com.sun.jersey.server.impl.model.method.dispatch.AbstractResourceMethodDispatchProvider$ResponseOutInvoker._dispatch(AbstractResourceMethodDispatchProvid
er.java:205)
代码
public class ConnectionManager {
private ConnectionManager() {
}
private static final Logger logger = LoggerFactory.getLogger(ConnectionManager.class);
private static MongoClient mongoClient = null;
private static String connections = null;
private static String connectionTimeOut = null;
private static String socketTimeOut = null;
private static String serverSelectionTimeOut = null;
private static String URL = null;
private static String username = null;
private static String password = null;
private static String hostname = null;
private static String port = null;
private static String maxConnectionIdleTime = null;
private static String minConnectionsPerHost = null;
private static String maxConnectionLifeTime = null;
private static String dbName = null;
static {
dbName = ValidationProperties.getValue("clm.db.dbName");
mongoClient = getCLMSecondaryMongoConnection();
}
private static MongoClient getCLMSecondaryMongoConnection() {
connections = ValidationProperties.getValue("clm.db.connectionsPerHost");
connectionTimeOut = ValidationProperties.getValue("clm.db.connectTimeoutMS");
socketTimeOut = ValidationProperties.getValue("clm.db.socketTimeOut");
serverSelectionTimeOut = ValidationProperties.getValue("clm.db.serverSelectionTimeOut");
username = ValidationProperties.getValue("clm.db.userName");
password = ValidationProperties.getValue("clm.db.password");
hostname = ValidationProperties.getValue("clm.db.hostname");
port = ValidationProperties.getValue("clm.db.portnumber");
maxConnectionIdleTime = ValidationProperties.getValue("clm.db.maxConnectionIdleTime");
minConnectionsPerHost = ValidationProperties.getValue("clm.db.minConnectionsPerHost");
try {
List<MongoCredential> creds = new ArrayList<MongoCredential>();
creds.add(MongoCredential.createCredential(username, dbName, password.toCharArray()));
/*creds.add(MongoCredential.createMongoCRCredential(username, dbName, password.toCharArray()));*/
MongoClientOptions.Builder optionsBuilder = MongoClientOptions.builder();
optionsBuilder.connectTimeout(Integer.parseInt(connectionTimeOut));
optionsBuilder.serverSelectionTimeout(Integer.parseInt(serverSelectionTimeOut));
optionsBuilder.socketTimeout(Integer.parseInt(socketTimeOut));
optionsBuilder.connectionsPerHost(Integer.parseInt(connections));
optionsBuilder.maxConnectionIdleTime(Integer.parseInt(maxConnectionIdleTime));
optionsBuilder.minConnectionsPerHost(Integer.parseInt(minConnectionsPerHost));
if(Boolean.valueOf(ValidationProperties.getValue("clm.db.useSecondaryMongoForRead"))) {
logger.info("Read is going for secondary mongoDB");
optionsBuilder.readPreference(ReadPreference.secondaryPreferred());
}
MongoClientOptions options = optionsBuilder.build();
mongoClient = new MongoClient(new ServerAddress(hostname, Integer.parseInt(port)), creds, options);
} catch (Exception e) {
logger.error("Error while connecting the Mongo DB {}", e);
}
return mongoClient;
}
public static MongoClient getMongoClient() {
return mongoClient;
}
答案 0 :(得分:0)
在您的代码中,您正在设置各种MongoClientOptions,但是您没有说明正在使用什么值。最有可能是其中一种设置引起了问题。我的第一个猜测是socketTimeout太小,我的第二个猜测是connectionsPerHost小于生产中收到的并发请求的最大数目。
我认为不能在生产环境中进行测试,因此,您应该做的第一件事就是尝试在测试环境中重现问题。您可以使用免费的JMeter tool向服务器重现一定的负载。使用它的加速功能,可以查看达到一定负载后是否开始出现问题,还是随机发生。
如果仍然无法找出导致问题的确切原因,则也可以尝试注释掉所有的optionBuilder设置(以便使用所有默认设置)。如果这样可以解决问题,则可以一次取消设置选项,以查看哪个是有问题的。
答案 1 :(得分:0)
您必须从项目中移除休眠依赖项。
我遇到了这个问题,这就是解决方案。