MongoDB isRunning

23 views
Skip to first unread message

Tom Bocklisch

unread,
Nov 8, 2011, 6:21:25 AM11/8/11
to Lift
Hey,

I try to check if a mongoDB is running with the following snippet:

def running_? : Boolean = {
try {
MongoDB.use(DefaultMongoIdentifier)(db => {
db.getLastError
})
true
} catch {
case e: Exception => false
}
}

The function is working and returning the correct boolean. But
although I am catching all exception im getting a

java.io.IOException: couldn't connect to [/127.0.0.1:27017]
bc:java.net.ConnectException: Connection refused
at com.mongodb.DBPort._open(DBPort.java:206)
at com.mongodb.DBPort.go(DBPort.java:94)
at com.mongodb.DBPort.go(DBPort.java:75)
.....

on the console. How can I catch this one?

Best regards Tom

Tim Nelson

unread,
Nov 8, 2011, 8:26:04 AM11/8/11
to lif...@googlegroups.com
Hi Tom,

I use similar code for testing and get these as well. Up until now I hadn't bothered to look into it, so thanks for giving me a nudge.

Your exceptions are being caught. What you're seeing is the logging output of the mongo-java-driver. The mongo-java-driver uses java util logging. So, you can change the log level for com.mongodb to SEVERE if you'd rather not see those.

For more info on java util logging:

Tim

Tom Bocklisch

unread,
Nov 9, 2011, 4:11:01 AM11/9/11
to Lift
Hi Tim,

thanks for your reply. Your hint helped me to stop javas error logging
while checking if the db is running and turing it on again afterwards.

Here is my approach of silencing javas error logging:

def setLogLevel(level:String){
val loggingProperties = new Properties()
loggingProperties.put( ".level", level)
val pos = new PipedOutputStream()
val pis = new PipedInputStream( pos)

loggingProperties.store( pos, "")
pos.close()
LogManager.getLogManager().readConfiguration(pis)
pis.close()
}

Now it's possible to turn logging of with setLogLevel("OFF") and turn
it on again with setLogLevel("ALL").

Best regards Tom
Reply all
Reply to author
Forward
0 new messages