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?
java mongodb exception
add a comment |
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?
java mongodb exception
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
add a comment |
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?
java mongodb exception
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
java mongodb exception
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
add a comment |
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
add a comment |
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.
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
add a comment |
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.
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
add a comment |
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.
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
add a comment |
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.
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.
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
add a comment |
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
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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