6

I configure my document store in the following way:

store = new DocumentStore { Url = serverUrl };
store.Initialize();

I like to know how I can make sure prior or post initialization but before opening a session whether the client is connected to the server. I did not startup the server and I could still initialize the store, not sure why or whether it creates by default an embedded db if it cannot find the server under specified url. Any idea how to check that the connection is established between client and server?

Matt Johnson-Pint
  • 230,703
  • 74
  • 448
  • 575
Matt
  • 7,004
  • 11
  • 71
  • 117

1 Answers1

8

Initialization does not actually open a connection. The RavenDB client opens and closes connections as it needs to.

It will not revert to an embedded database. You have to explicitly use an EmbeddableDocumentStore if you want an embedded database instance.

If you want to check yourself if the server is up, you can just do something and see if it fails. Probably the easiest thing you could do is to try to get the build number of the RavenDB server. This can be done using documentStore.AsyncDatabaseCommands.GetBuildNumberAsync().

Here are some extension methods that will help make it even easier. Put these in a static class:

public static bool TryGetServerVersion(this IDocumentStore documentStore, out BuildNumber buildNumber, int timeoutMilliseconds = 5000)
{
    try
    {
        var task = documentStore.AsyncDatabaseCommands.GetBuildNumberAsync();
        var success = task.Wait(timeoutMilliseconds);
        buildNumber = task.Result;
        return success;
    }
    catch
    {
        buildNumber = null;
        return false;
    }
}

public static bool IsServerOnline(this IDocumentStore documentStore, int timeoutMilliseconds = 5000)
{
    BuildNumber buildNumber;
    return documentStore.TryGetServerVersion(out buildNumber, timeoutMilliseconds);
}

Then you can use them like this:

var online = documentStore.IsServerOnline();

Or like this:

BuildNumber buildNumber;
var online = documentStore.TryGetServerVersion(out buildNumber);
Matt Johnson-Pint
  • 230,703
  • 74
  • 448
  • 575
  • This helps, will try and report back. Thank you. – Matt Feb 16 '13 at 02:22
  • It appears that my AsyncDatabaseCOmmands object is NULL? Are you doing any other initialisation on the document store before you get to this point?? – tigerswithguitars Jul 31 '13 at 09:34
  • We tried this and found that it began failing after the most recent RavenDB client upgrade. [This discussion](https://groups.google.com/forum/#!topic/ravendb/-bU7OJXdG4Q) on the Google group came to the conclusion that you should use a synchronous call. – Rob Scott Aug 02 '13 at 18:03
  • The problem is that `GetBuildNumber` only exists in the async commands. I will raise an issue to request it for sync also. But then you'd have to handle a timeout exception. – Matt Johnson-Pint Aug 02 '13 at 18:10
  • 1
    @MattJohnson, seems the GetBuildNumber is missing in the AsyncDatabaseCommands in the new version (I just updated to Client.Lightweight 3.0.0.0). Is there a better way to check whether the store is connected? – Matt Jan 23 '15 at 11:13
  • 1
    @MattWolf AsyncDatabaseCommands.GlobalAdmin.GetBuildNumberAsync() – dusky Sep 11 '15 at 16:29