Vish Bansal
Vish Bansal

Reputation: 28

Mongo db timeout exception saying connection refused

Please Help me. I am using mongo DB from a long time for a application.the application is in development mode. Sometimes I see this error

ERROR- Cannot invoke the action, eventually got an error: org.springframework.dao.DataAccessResourceFailureException: Timed out after 10000 ms while waiting to connect. Client view of cluster state is {type=Unknown, servers=[{address=localhost:27017, type=Unknown, state=Connecting, exception={com.mongodb.MongoException$Network: Exception opening the socket}, caused by {java.net.ConnectException: Connection refused}}]; nested exception is com.mongodb.MongoTimeoutException: Timed out after 10000 ms while waiting to connect. Client view of cluster state is {type=Unknown, servers=[{address=localhost:27017, type=Unknown, state=Connecting, exception={com.mongodb.MongoException$Network: Exception opening the socket}, caused by {java.net.ConnectException: Connection refused}}]

But when I restart the server, it does not come again. But for once I have to restart the server. I am using mongodb 2.6.6.

Please help me. I am afraid of future because soon it will be on live mode. my Databaseconnection class:

static final String mongoServer = Play.application().configuration().getString("application.mongo.db.server");
static final String mongoDBname = Play.application().configuration().getString("application.mongo.db.dbname");
static final String mongoUsername = Play.application().configuration().getString("application.mongo.db.username");
static final String mongoPassword = Play.application().configuration().getString("application.mongo.db.password");
static final int connectionPerHost = Play.application().configuration().getInt("application.mongo.db.connections");
static final int connectionIdleTime = Play.application().configuration().getInt("application.mongo.db.idletime");

private MongoTemplate _mongoTemplate;
private static MongoClient   _mongo;

public MongoTemplate getContext() {
    if(_mongoTemplate == null)
        openDbConnection();
    if(_mongo == null || _mongoTemplate == null)
        Logger.error("DatabaseConnection::openDbConnection - Unable to get context. How is this possible?");
    return _mongoTemplate;
}

private static synchronized void createMongo() {
    if(_mongo == null) {
        Logger.debug("DatabaseConnection::openDbConnection - Opening a new connection");
        MongoClientOptions options = new MongoClientOptions.Builder().connectionsPerHost(connectionPerHost)
                .cursorFinalizerEnabled(true).maxConnectionIdleTime(connectionIdleTime).build();
        MongoCredential credential = MongoCredential.createMongoCRCredential(mongoUsername, mongoDBname, mongoPassword.toCharArray());
        ServerAddress addr = null;
        try {
            addr = new ServerAddress(mongoServer);
        } catch (UnknownHostException e) {
            Logger.error("Error Connecting to Mongo: Wrong Server??", e);
            e.printStackTrace();
        }
        _mongo = new MongoClient(addr, Arrays.asList(credential), options);
    }
}

private boolean openDbConnection() {
    try {
        if(_mongo == null) createMongo();
        // TODO: Connection Pooling
        _mongoTemplate = new MongoTemplate(_mongo, mongoDBname); //new MongoTemplate(dbFactory, converter);
        return true;
    } catch (Exception e) {
        Logger.error("Error Opening Connection:", e);
        e.printStackTrace();
    }
    return false;
}

private boolean closeDbConnection() {
    try {
        _mongoTemplate = null;
        return true;
    } catch (Exception ex) {
        Logger.error("Error Closing", ex);
    }
    return false;
}

@Override
protected void finalize() throws Throwable {
    closeDbConnection();
    super.finalize();
}

Upvotes: 0

Views: 6248

Answers (1)

AVINASH SHRIMALI
AVINASH SHRIMALI

Reputation: 580

Usually it appears when the monogdb connection is not established, verify it.

Upvotes: 1

Related Questions