RavenDB: Embedded Mode

Let’s take a look at more ‘optional’ and supporting features of db4o. One of these is that you can run RavenDB in embedded mode. I personally like embed the database smaller applications. Another big selling point is that this is very nice for testing reasons.

Embedded Mode

The first thing we need to do is to reference the required assemblies. The easiest way is to grab the ‘RavenDB (embedded)’ package via NuGet. Alternatively you can reference the assemblies in ‘EmbeddedClient’ folder of the RavenDB distribution.

After than we just start up an EmbeddableDocumentStore instance. That’s all, we now can start using the embedded storage.

using (var documentStore = new EmbeddableDocumentStore{DataDirectory = "Data"}.Initialize())
{
	// now we can use the embedded instance
	using (var session = documentStore.OpenSession())
	{
		// do work with the session
	}
}

Raven Inside

Raven Inside

In Memory Instance

For testing purpose it’s often useful to create in memory database instances, which can be created and thrown away really quickly. That is possible with RavenDB. We just need to set the RunInMemory flag: BOOM, now RavenDB runs in memory.

using (var documentStore = new EmbeddableDocumentStore{RunInMemory = true}.Initialize())
{
	using (var session = documentStore.OpenSession())
	{
		// Run complex test scenarious
	}
}

Uh, forgot what this blog is about

Uh, forgot what this blog is about

Embedded HTTP

When we run an embedded database we cannot inspect that database while the application is running. For that we need to provide access for our administration tools. Well, that’s again a flag away. When we enable the ‘UseEmbeddedHttpServer’ flag RavenDB will start up a small HTTP server which exposes the regular RavenDB functionality. Then we just can use Raven Studio (the Silverlight app which comes with RavenDB) to connect to our database.

using (var documentStore = new EmbeddableDocumentStore
           {
               DataDirectory = "Data",
               UseEmbeddedHttpServer = true
           }.Initialize())
{
	using (var session = documentStore.OpenSession())
	{
		// Run your app
	}
}

Alternatively we can manually start the HTTP server any time when required:

// Start the HTTP server manually
var server = new RavenDbHttpServer(documentStore.Configuration, documentStore.DocumentDatabase);
server.Start();

When you want to ensure that the application has the rights to open the port you can use this utility to ask for permission. Basically it checks if the application has enough rights to open the port. In case it doesn’t have the rights it will prompt for it:

NonAdminHttp.EnsureCanListenToWhenInNonAdminContext(8080);

I guess modern heating system do have HTTP interfaces

I guess modern heating system do have HTTP interfaces

Conclusion

Now we know how to run RavenDB as embedded database. A wonderful feature, even when we use it only for testing. After this simple feature I hopefully get back to more advanced features next time. =).

Tagged on: ,

One thought on “RavenDB: Embedded Mode

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>