MongoDB check Connection without exception











up vote
0
down vote

favorite












I am using MongoDB with java. I need a way to check if MongoDB is running (to prevent exceptoins later). I tried it with this code:



mongoClient = new MongoClient();
try {
mongoClient.getAddress();
mongoRunning = true;
} catch (Throwable e){
log.warn("no db connected");
return;
}


I know throwable is bad but it is not working neither with exception nor with Throwable. I see (the catch block is not entered!):



[INFO ] 2018-11-10 22:33:26.209 [cluster-ClusterId{value='5be74e9f7170312fd4eb1ffe', description='null'}-127.0.0.1:27017] cluster - Exception in monitor thread while connecting to server 127.0.0.1:27017
com.mongodb.MongoSocketOpenException: Exception opening socket
at com.mongodb.internal.connection.SocketStream.open(SocketStream.java:67) ~[mongo-java-driver-3.8.2.jar:?]
at com.mongodb.internal.connection.InternalStreamConnection.open(InternalStreamConnection.java:126) ~[mongo-java-driver-3.8.2.jar:?]
at com.mongodb.internal.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:117) [mongo-java-driver-3.8.2.jar:?]
at java.lang.Thread.run(Thread.java:745) [?:1.8.0_102]
Caused by: java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method) ~[?:1.8.0_102]
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:85) ~[?:1.8.0_102]
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350) ~[?:1.8.0_102]
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206) ~[?:1.8.0_102]
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188) ~[?:1.8.0_102]
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172) ~[?:1.8.0_102]
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) ~[?:1.8.0_102]
at java.net.Socket.connect(Socket.java:589) ~[?:1.8.0_102]
at com.mongodb.internal.connection.SocketStreamHelper.initialize(SocketStreamHelper.java:64) ~[mongo-java-driver-3.8.2.jar:?]
at com.mongodb.internal.connection.SocketStream.open(SocketStream.java:62) ~[mongo-java-driver-3.8.2.jar:?]
... 3 more


And my program terminates. An i thought i know java :-(. But i also tried:



mongoClient.listDatabases();


This behaves also odd: mongoRunning is set to true and an exception is thrown asynchronously.
I need a way to check the connection once. Is this possible without any exception and in a synchronous way?










share|improve this question






















  • Have you tried to put the constructor call in the try block?
    – Vitalii Vitrenko
    Nov 10 at 21:58












  • no, but the constructor never throws exception.
    – dermoritz
    Nov 10 at 22:11















up vote
0
down vote

favorite












I am using MongoDB with java. I need a way to check if MongoDB is running (to prevent exceptoins later). I tried it with this code:



mongoClient = new MongoClient();
try {
mongoClient.getAddress();
mongoRunning = true;
} catch (Throwable e){
log.warn("no db connected");
return;
}


I know throwable is bad but it is not working neither with exception nor with Throwable. I see (the catch block is not entered!):



[INFO ] 2018-11-10 22:33:26.209 [cluster-ClusterId{value='5be74e9f7170312fd4eb1ffe', description='null'}-127.0.0.1:27017] cluster - Exception in monitor thread while connecting to server 127.0.0.1:27017
com.mongodb.MongoSocketOpenException: Exception opening socket
at com.mongodb.internal.connection.SocketStream.open(SocketStream.java:67) ~[mongo-java-driver-3.8.2.jar:?]
at com.mongodb.internal.connection.InternalStreamConnection.open(InternalStreamConnection.java:126) ~[mongo-java-driver-3.8.2.jar:?]
at com.mongodb.internal.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:117) [mongo-java-driver-3.8.2.jar:?]
at java.lang.Thread.run(Thread.java:745) [?:1.8.0_102]
Caused by: java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method) ~[?:1.8.0_102]
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:85) ~[?:1.8.0_102]
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350) ~[?:1.8.0_102]
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206) ~[?:1.8.0_102]
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188) ~[?:1.8.0_102]
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172) ~[?:1.8.0_102]
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) ~[?:1.8.0_102]
at java.net.Socket.connect(Socket.java:589) ~[?:1.8.0_102]
at com.mongodb.internal.connection.SocketStreamHelper.initialize(SocketStreamHelper.java:64) ~[mongo-java-driver-3.8.2.jar:?]
at com.mongodb.internal.connection.SocketStream.open(SocketStream.java:62) ~[mongo-java-driver-3.8.2.jar:?]
... 3 more


And my program terminates. An i thought i know java :-(. But i also tried:



mongoClient.listDatabases();


This behaves also odd: mongoRunning is set to true and an exception is thrown asynchronously.
I need a way to check the connection once. Is this possible without any exception and in a synchronous way?










share|improve this question






















  • Have you tried to put the constructor call in the try block?
    – Vitalii Vitrenko
    Nov 10 at 21:58












  • no, but the constructor never throws exception.
    – dermoritz
    Nov 10 at 22:11













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I am using MongoDB with java. I need a way to check if MongoDB is running (to prevent exceptoins later). I tried it with this code:



mongoClient = new MongoClient();
try {
mongoClient.getAddress();
mongoRunning = true;
} catch (Throwable e){
log.warn("no db connected");
return;
}


I know throwable is bad but it is not working neither with exception nor with Throwable. I see (the catch block is not entered!):



[INFO ] 2018-11-10 22:33:26.209 [cluster-ClusterId{value='5be74e9f7170312fd4eb1ffe', description='null'}-127.0.0.1:27017] cluster - Exception in monitor thread while connecting to server 127.0.0.1:27017
com.mongodb.MongoSocketOpenException: Exception opening socket
at com.mongodb.internal.connection.SocketStream.open(SocketStream.java:67) ~[mongo-java-driver-3.8.2.jar:?]
at com.mongodb.internal.connection.InternalStreamConnection.open(InternalStreamConnection.java:126) ~[mongo-java-driver-3.8.2.jar:?]
at com.mongodb.internal.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:117) [mongo-java-driver-3.8.2.jar:?]
at java.lang.Thread.run(Thread.java:745) [?:1.8.0_102]
Caused by: java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method) ~[?:1.8.0_102]
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:85) ~[?:1.8.0_102]
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350) ~[?:1.8.0_102]
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206) ~[?:1.8.0_102]
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188) ~[?:1.8.0_102]
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172) ~[?:1.8.0_102]
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) ~[?:1.8.0_102]
at java.net.Socket.connect(Socket.java:589) ~[?:1.8.0_102]
at com.mongodb.internal.connection.SocketStreamHelper.initialize(SocketStreamHelper.java:64) ~[mongo-java-driver-3.8.2.jar:?]
at com.mongodb.internal.connection.SocketStream.open(SocketStream.java:62) ~[mongo-java-driver-3.8.2.jar:?]
... 3 more


And my program terminates. An i thought i know java :-(. But i also tried:



mongoClient.listDatabases();


This behaves also odd: mongoRunning is set to true and an exception is thrown asynchronously.
I need a way to check the connection once. Is this possible without any exception and in a synchronous way?










share|improve this question













I am using MongoDB with java. I need a way to check if MongoDB is running (to prevent exceptoins later). I tried it with this code:



mongoClient = new MongoClient();
try {
mongoClient.getAddress();
mongoRunning = true;
} catch (Throwable e){
log.warn("no db connected");
return;
}


I know throwable is bad but it is not working neither with exception nor with Throwable. I see (the catch block is not entered!):



[INFO ] 2018-11-10 22:33:26.209 [cluster-ClusterId{value='5be74e9f7170312fd4eb1ffe', description='null'}-127.0.0.1:27017] cluster - Exception in monitor thread while connecting to server 127.0.0.1:27017
com.mongodb.MongoSocketOpenException: Exception opening socket
at com.mongodb.internal.connection.SocketStream.open(SocketStream.java:67) ~[mongo-java-driver-3.8.2.jar:?]
at com.mongodb.internal.connection.InternalStreamConnection.open(InternalStreamConnection.java:126) ~[mongo-java-driver-3.8.2.jar:?]
at com.mongodb.internal.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:117) [mongo-java-driver-3.8.2.jar:?]
at java.lang.Thread.run(Thread.java:745) [?:1.8.0_102]
Caused by: java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method) ~[?:1.8.0_102]
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:85) ~[?:1.8.0_102]
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350) ~[?:1.8.0_102]
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206) ~[?:1.8.0_102]
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188) ~[?:1.8.0_102]
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172) ~[?:1.8.0_102]
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) ~[?:1.8.0_102]
at java.net.Socket.connect(Socket.java:589) ~[?:1.8.0_102]
at com.mongodb.internal.connection.SocketStreamHelper.initialize(SocketStreamHelper.java:64) ~[mongo-java-driver-3.8.2.jar:?]
at com.mongodb.internal.connection.SocketStream.open(SocketStream.java:62) ~[mongo-java-driver-3.8.2.jar:?]
... 3 more


And my program terminates. An i thought i know java :-(. But i also tried:



mongoClient.listDatabases();


This behaves also odd: mongoRunning is set to true and an exception is thrown asynchronously.
I need a way to check the connection once. Is this possible without any exception and in a synchronous way?







java mongodb exception






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 10 at 21:45









dermoritz

4,6241157112




4,6241157112












  • Have you tried to put the constructor call in the try block?
    – Vitalii Vitrenko
    Nov 10 at 21:58












  • no, but the constructor never throws exception.
    – dermoritz
    Nov 10 at 22:11


















  • Have you tried to put the constructor call in the try block?
    – Vitalii Vitrenko
    Nov 10 at 21:58












  • no, but the constructor never throws exception.
    – dermoritz
    Nov 10 at 22:11
















Have you tried to put the constructor call in the try block?
– Vitalii Vitrenko
Nov 10 at 21:58






Have you tried to put the constructor call in the try block?
– Vitalii Vitrenko
Nov 10 at 21:58














no, but the constructor never throws exception.
– dermoritz
Nov 10 at 22:11




no, but the constructor never throws exception.
– dermoritz
Nov 10 at 22:11












1 Answer
1






active

oldest

votes

















up vote
1
down vote













MongoClient represents a connection pool. It manages the connections, and everything happening in its threads.



I think it is wrong to ask question "if MongoDB is running". Suppose you have a tool or instrument to check if it is running, and you receive no exception at the time of checking, but right after you checked MongoDB is crashed, what would you do. I.e. it is wrong to attempt such check, instead one should focus to write application in reliable way with proper error handling at the time of queries.



Additionally, you may look at ConnectionPoolSettings which has getMaxWaitQueueSize, by default its 500 queries before it starts throwing exceptions. If that is what causes problems to you, you may try to reduce this to lesser value, to start receiving exceptions earlier.






share|improve this answer





















  • my use case is: the mongodb is optional to my application and if db is not available it just should not try to persist stuff/ se the db. i know about the listener i could use to get live signs of mongo. but for the moment i only need one synchronous check
    – dermoritz
    Nov 10 at 22:14










  • If it is optional for application, I would do it on application configuration level. For instance, if MONGODB_URL is provided in environment variable, then do persistence, if not don't do. But having both, configured the connection URL, and sometimes doing and sometimes not doing persistence, it is not optional, it is something else.
    – muradm
    Nov 10 at 22:17











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53243717%2fmongodb-check-connection-without-exception%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
1
down vote













MongoClient represents a connection pool. It manages the connections, and everything happening in its threads.



I think it is wrong to ask question "if MongoDB is running". Suppose you have a tool or instrument to check if it is running, and you receive no exception at the time of checking, but right after you checked MongoDB is crashed, what would you do. I.e. it is wrong to attempt such check, instead one should focus to write application in reliable way with proper error handling at the time of queries.



Additionally, you may look at ConnectionPoolSettings which has getMaxWaitQueueSize, by default its 500 queries before it starts throwing exceptions. If that is what causes problems to you, you may try to reduce this to lesser value, to start receiving exceptions earlier.






share|improve this answer





















  • my use case is: the mongodb is optional to my application and if db is not available it just should not try to persist stuff/ se the db. i know about the listener i could use to get live signs of mongo. but for the moment i only need one synchronous check
    – dermoritz
    Nov 10 at 22:14










  • If it is optional for application, I would do it on application configuration level. For instance, if MONGODB_URL is provided in environment variable, then do persistence, if not don't do. But having both, configured the connection URL, and sometimes doing and sometimes not doing persistence, it is not optional, it is something else.
    – muradm
    Nov 10 at 22:17















up vote
1
down vote













MongoClient represents a connection pool. It manages the connections, and everything happening in its threads.



I think it is wrong to ask question "if MongoDB is running". Suppose you have a tool or instrument to check if it is running, and you receive no exception at the time of checking, but right after you checked MongoDB is crashed, what would you do. I.e. it is wrong to attempt such check, instead one should focus to write application in reliable way with proper error handling at the time of queries.



Additionally, you may look at ConnectionPoolSettings which has getMaxWaitQueueSize, by default its 500 queries before it starts throwing exceptions. If that is what causes problems to you, you may try to reduce this to lesser value, to start receiving exceptions earlier.






share|improve this answer





















  • my use case is: the mongodb is optional to my application and if db is not available it just should not try to persist stuff/ se the db. i know about the listener i could use to get live signs of mongo. but for the moment i only need one synchronous check
    – dermoritz
    Nov 10 at 22:14










  • If it is optional for application, I would do it on application configuration level. For instance, if MONGODB_URL is provided in environment variable, then do persistence, if not don't do. But having both, configured the connection URL, and sometimes doing and sometimes not doing persistence, it is not optional, it is something else.
    – muradm
    Nov 10 at 22:17













up vote
1
down vote










up vote
1
down vote









MongoClient represents a connection pool. It manages the connections, and everything happening in its threads.



I think it is wrong to ask question "if MongoDB is running". Suppose you have a tool or instrument to check if it is running, and you receive no exception at the time of checking, but right after you checked MongoDB is crashed, what would you do. I.e. it is wrong to attempt such check, instead one should focus to write application in reliable way with proper error handling at the time of queries.



Additionally, you may look at ConnectionPoolSettings which has getMaxWaitQueueSize, by default its 500 queries before it starts throwing exceptions. If that is what causes problems to you, you may try to reduce this to lesser value, to start receiving exceptions earlier.






share|improve this answer












MongoClient represents a connection pool. It manages the connections, and everything happening in its threads.



I think it is wrong to ask question "if MongoDB is running". Suppose you have a tool or instrument to check if it is running, and you receive no exception at the time of checking, but right after you checked MongoDB is crashed, what would you do. I.e. it is wrong to attempt such check, instead one should focus to write application in reliable way with proper error handling at the time of queries.



Additionally, you may look at ConnectionPoolSettings which has getMaxWaitQueueSize, by default its 500 queries before it starts throwing exceptions. If that is what causes problems to you, you may try to reduce this to lesser value, to start receiving exceptions earlier.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 10 at 21:58









muradm

722418




722418












  • my use case is: the mongodb is optional to my application and if db is not available it just should not try to persist stuff/ se the db. i know about the listener i could use to get live signs of mongo. but for the moment i only need one synchronous check
    – dermoritz
    Nov 10 at 22:14










  • If it is optional for application, I would do it on application configuration level. For instance, if MONGODB_URL is provided in environment variable, then do persistence, if not don't do. But having both, configured the connection URL, and sometimes doing and sometimes not doing persistence, it is not optional, it is something else.
    – muradm
    Nov 10 at 22:17


















  • my use case is: the mongodb is optional to my application and if db is not available it just should not try to persist stuff/ se the db. i know about the listener i could use to get live signs of mongo. but for the moment i only need one synchronous check
    – dermoritz
    Nov 10 at 22:14










  • If it is optional for application, I would do it on application configuration level. For instance, if MONGODB_URL is provided in environment variable, then do persistence, if not don't do. But having both, configured the connection URL, and sometimes doing and sometimes not doing persistence, it is not optional, it is something else.
    – muradm
    Nov 10 at 22:17
















my use case is: the mongodb is optional to my application and if db is not available it just should not try to persist stuff/ se the db. i know about the listener i could use to get live signs of mongo. but for the moment i only need one synchronous check
– dermoritz
Nov 10 at 22:14




my use case is: the mongodb is optional to my application and if db is not available it just should not try to persist stuff/ se the db. i know about the listener i could use to get live signs of mongo. but for the moment i only need one synchronous check
– dermoritz
Nov 10 at 22:14












If it is optional for application, I would do it on application configuration level. For instance, if MONGODB_URL is provided in environment variable, then do persistence, if not don't do. But having both, configured the connection URL, and sometimes doing and sometimes not doing persistence, it is not optional, it is something else.
– muradm
Nov 10 at 22:17




If it is optional for application, I would do it on application configuration level. For instance, if MONGODB_URL is provided in environment variable, then do persistence, if not don't do. But having both, configured the connection URL, and sometimes doing and sometimes not doing persistence, it is not optional, it is something else.
– muradm
Nov 10 at 22:17


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53243717%2fmongodb-check-connection-without-exception%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Bressuire

Vorschmack

Quarantine