Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > c++ check if service is running

Reply
Thread Tools

c++ check if service is running

 
 
Mick
Guest
Posts: n/a
 
      10-10-2012
Hi there,

I posted this in mongodb-user, but I think it's more of a C++ question, so sorry for the double-post...

I'm using Visual Studio 2008 C++, connecting to Mongo 2.0.6 database.

If I try to get a ScopedDbConnection, but mongod is not running for some reason, it throws

"First-chance exception at 0x750bb9bc in myApp.exe: Microsoft C++ exception: mongo::SocketException at memory location 0x0057ead8"

Is there any way to check if either of those 2 are running (mongod or MongoDB) before trying to get a ScopedDbConnection?

Thank you in advance.
 
Reply With Quote
 
 
 
 
Melzzzzz
Guest
Posts: n/a
 
      10-10-2012
On Wed, 10 Oct 2012 15:51:14 -0700 (PDT)
Mick <> wrote:

> Hi there,
>
> I posted this in mongodb-user, but I think it's more of a C++
> question, so sorry for the double-post...
>
> I'm using Visual Studio 2008 C++, connecting to Mongo 2.0.6
> database.
>
> If I try to get a ScopedDbConnection, but mongod is not running for
> some reason, it throws
>
> "First-chance exception at 0x750bb9bc in myApp.exe: Microsoft C++
> exception: mongo::SocketException at memory location 0x0057ead8"
>
> Is there any way to check if either of those 2 are running (mongod or
> MongoDB) before trying to get a ScopedDbConnection?


You already have it. With exception you know it's not running.
So catch exception, report and exit.



 
Reply With Quote
 
 
 
 
goran.pusic@gmail.com
Guest
Posts: n/a
 
      10-11-2012
On Thursday, October 11, 2012 12:51:14 AM UTC+2, Mick wrote:
> Hi there,
>
>
>
> I posted this in mongodb-user, but I think it's more of a C++ question, so sorry for the double-post...
>
>
>
> I'm using Visual Studio 2008 C++, connecting to Mongo 2.0.6 database.
>
>
>
> If I try to get a ScopedDbConnection, but mongod is not running for some reason, it throws
>
>
>
> "First-chance exception at 0x750bb9bc in myApp.exe: Microsoft C++ exception: mongo::SocketException at memory location 0x0057ead8"
>
>
>
> Is there any way to check if either of those 2 are running (mongod or MongoDB) before trying to get a ScopedDbConnection?


Maybe, but it's a bad idea. It's akin to going to a supermarket to see whether there's milk, going back home to get the money, then going to the supermarket again to buy it.

What you should do instead is to catch the exception, inspect it, and report the error. E.g. (I don't know anything about mongo, so I might be way offbase here).

try
{
ScopedDbConnection db(...);
use(db); // lots of code here
}
catch(const mongo::SocketException& e)
{
// inspect e._type here to see what to do next.
}

Another reason why trying to see whether a "service is running" (not mongo,ANYTHING) is that it is surprisingly hard to do well. It's hard because there's literally thousands of reasons why you can't connect. If that is so, it is by far the best to actually try to connect, and report to the user anything you have in the line of info (in this case e.g. toString() and the address you tried to reach if it isn't in exception object already).

Note that the approach like this: (don't know if possible here, but have seen people doing this):

auto_ptr<ScopedConnection> P;
try
{
P = auto_ptr<ScopedConnection>(new ScopedConnection(...));
}
catch(whatever)
{
reporterror;
}

if (P)
{ // mongo alive, yay!
use(*P);
}

is an abomination. It's ugly, and you still need another try/catch to report errors that will eventually come from use.

So... Don't detect anything. Fail eagerly, loudly and clearly instead.

HTH,

Goran.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to selecet check box in the data grid ?? only one check box mit ASP .Net 1 01-25-2006 06:47 PM
InvocationTargetException when calling "new Service()" in Axis web service to call another web service Michael Averstegge Java 0 01-10-2006 11:05 PM
How can check a window service is running or not in java sherazi Java 1 01-02-2006 08:07 PM
Calling a Web Service using Axis, from within an Axis Web Service running under Tomcat hocho888 Java 1 04-29-2005 08:26 PM
.Net Pro include Visual Source Safe or other Code Check in Check out software? Davisro ASP .Net 1 06-14-2004 03:23 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57