Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Managing RavenDB Document Store Startup

DZone's Guide to

Managing RavenDB Document Store Startup

· Database Zone
Free Resource

MongoDB Atlas is a database as a service that makes it easy to deploy, manage, and scale MongoDB. So you can focus on innovation, not operations. Brought to you in partnership with MongoDB.

The RavenDB’s document store is your main access point to the database. It is strongly recommended that you’ll have just a single instance of the document store per each server you are accessing. That usually means that you have to implement a singleton, with all the double checked locking nonsense that is involved in that. I was giving a course in RavenDB last week and I stumbled upon a very nice coding pattern:

    public static class Global
    {
        private static readonly Lazy<IDocumentStore> theDocStore = new Lazy<IDocumentStore>(()=>
            {
                var docStore = new DocumentStore
                    {
                        ConnectionStringName = "RavenDB"
                    };
                docStore.Initialize();

                //OPTIONAL:
                //IndexCreation.CreateIndexes(typeof(Global).Assembly, docStore);

                return docStore;
            });

        public static IDocumentStore DocumentStore
        {
            get { return theDocStore.Value; }
        }
    }

This is a very readable code, and it handles pretty much all of the treading stuff for you, without obscuring what you really want to do.

And what about when you have multiple servers? How do you handle it then? Same idea, taken one step further:

    public static class Global
    {
        private readonly static ConcurrentDictionary<string, Lazy<IDocumentStore>> stores = 
            new ConcurrentDictionary<string, Lazy<IDocumentStore>>();
        
        public static IDocumentStore GetDocumentStoreFor(string url)
        {
            return stores.GetOrAdd(url, CreateDocumentStore).Value;
        }

        private static Lazy<IDocumentStore> CreateDocumentStore(string url)
        {
            return new Lazy<IDocumentStore>(() =>
                {
                    var docStore = new DocumentStore
                        {
                            ConnectionStringName = url
                        };
                    docStore.Initialize();

                    //OPTIONAL:
                    //IndexCreation.CreateIndexes(typeof(Global).Assembly, docStore);

                    return docStore;
                });
        }
    }

This is nice, easy and the correct way to handle things.




MongoDB Atlas is the best way to run MongoDB on AWS — highly secure by default, highly available, and fully elastic. Get started free. Brought to you in partnership with MongoDB.

Topics:

Published at DZone with permission of Oren Eini, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}