2

I'm very new to Java and MongoDB (or any databases) and I have been building this Java program to test the connection. It's supposed to just simply establish the connection and list all existing database names.

import com.mongodb.MongoClient;

import com.mongodb.client.MongoCursor;

public class test {
    public static void main(String[] args) {
        try{
            MongoClient mongoClient = new MongoClient( "localhost" , 27017 );

            MongoCursor<String> dbsCursor = mongoClient.listDatabaseNames().iterator();
            while(dbsCursor.hasNext()) {
                System.out.println(dbsCursor.next());
            }

        }catch(Exception e){
            System.err.println( e.getClass().getName() + ": " + e.getMessage() );
        }
    }
}

The MongoDB server is already started. However, when I run this app, it shows errors like these

Jun 25, 2016 3:35:06 PM com.mongodb.diagnostics.logging.JULLogger log
INFO: Cluster created with settings {hosts=[localhost:27017], mode=SINGLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms', maxWaitQueueSize=500}
Jun 25, 2016 3:35:06 PM com.mongodb.diagnostics.logging.JULLogger log
INFO: No server chosen by ReadPreferenceServerSelector{readPreference=primary} from cluster description ClusterDescription{type=UNKNOWN, connectionMode=SINGLE, all=[ServerDescription{address=localhost:27017, type=UNKNOWN, state=CONNECTING}]}. Waiting for 30000 ms before timing out
Jun 25, 2016 3:35:06 PM com.mongodb.diagnostics.logging.JULLogger log
INFO: Exception in monitor thread while connecting to server localhost:27017
com.mongodb.MongoSocketWriteException: Exception sending message
    at com.mongodb.connection.InternalStreamConnection.translateWriteException(InternalStreamConnection.java:462)
    at com.mongodb.connection.InternalStreamConnection.sendMessage(InternalStreamConnection.java:205)
    at com.mongodb.connection.CommandHelper.sendMessage(CommandHelper.java:89)
    at com.mongodb.connection.CommandHelper.executeCommand(CommandHelper.java:32)
    at com.mongodb.connection.InternalStreamConnectionInitializer.initializeConnectionDescription(InternalStreamConnectionInitializer.java:83)
    at com.mongodb.connection.InternalStreamConnectionInitializer.initialize(InternalStreamConnectionInitializer.java:43)
    at com.mongodb.connection.InternalStreamConnection.open(InternalStreamConnection.java:115)
    at com.mongodb.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:128)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.net.SocketException: Software caused connection abort: socket write error
    at java.net.SocketOutputStream.socketWrite0(Native Method)
    at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:109)
    at java.net.SocketOutputStream.write(SocketOutputStream.java:153)
    at com.mongodb.connection.SocketStream.write(SocketStream.java:75)
    at        com.mongodb.connection.InternalStreamConnection.sendMessage(InternalStreamConnection.java:201)
... 7 more

com.mongodb.MongoTimeoutException: Timed out after 30000 ms while waiting for a server that matches ReadPreferenceServerSelector{readPreference=primary}. Client view of cluster state is {type=UNKNOWN, servers=[{address=localhost:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketWriteException: Exception sending message}, caused by {java.net.SocketException: Software caused connection abort: socket write error}}]

Process finished with exit code 0

What could these mean? Am I suppose to set ReadPreferenceServerSelector? But I can't find any related documents online.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Jonathan Qu
  • 21
  • 1
  • 1
  • 2
  • Your code is working well. I think it is a problem with mongodb. Try to stop and start it again. Is there any error? – LEQADA Jun 26 '16 at 01:30
  • This error keeps repeating... So before executing I first started the Mongodb in command line. Is this the only thing to do? Or it might possibly be a installation problem since I basically dragged every Mongodb driver file into the project directory because I can't get the maven to work. – Jonathan Qu Jun 26 '16 at 03:08
  • I changed the installation method: imported .jar files into project structure/modules. Yet this bug is still happening – Jonathan Qu Jun 26 '16 at 04:29
  • Can you describe step-by-step how you installed MongoDB, how you included mongodb java libraries to your project and how you compiled it? – LEQADA Jun 26 '16 at 19:33

1 Answers1

0

The ReadPreferenceServerSelector is used internally when firing requests to Mongo. In your scenario, you do not need to go into that detail.

The error of importance to me is this. The linked answer describes why this error might be coming. But it should not be repeating on multiple attempts.

Software caused connection abort: socket write error

  1. Can you check if mongo is running on the port 27017 and is accessible via command line ?
  2. Simply using the mongo-java-driver should be sufficient for your to run a test like this. Just make sure the jar version matches the installed mongodb version. A better idea would be to use something like maven as described in this tutorial.

Please post if the error persists after trying these things.

Community
  • 1
  • 1
Aditya
  • 2,148
  • 3
  • 21
  • 34