NoSQL Zone is brought to you in partnership with:

Ayende Rahien is working for Hibernating Rhinos LTD, a Israeli based company producing developer productivity tools for OLTP applications such as NHibernate Profiler (nhprof.com), Linq to SQL Profiler(l2sprof.com), Entity Framework Profiler (efprof.com) and more. Ayende is a DZone MVB and is not an employee of DZone and has posted 484 posts at DZone. You can read more from them at their website. View Full User Profile

Managing RavenDB Document Store Startup

12.24.2012
| 1967 views |
  • submit to reddit

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.




Published at DZone with permission of Ayende Rahien, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)