private static MongoDatabase DoGetDatabase(MongoServer mongo, string databaseName, MongoCredentials credentials,
                                                   WriteConcern writeConcern, bool allowCreate)
        {
            var dbHolder = (DatabaseHolder)TransactionSynchronizationManager.GetResource(mongo);

            // Do we have a populated holder and TX sync active?
            if (dbHolder != null && !dbHolder.IsEmpty && TransactionSynchronizationManager.SynchronizationActive)
            {
                var holderDatabase = dbHolder.GetDatabase(databaseName);

                // DB found but not yet synchronized
                if (holderDatabase != null && !dbHolder.SynchronizedWithTransaction)
                {
                    Log.Debug(
                        m => m("Registering Spring transaction synchronization for existing MongoDB {0}.", databaseName));

                    TransactionSynchronizationManager.RegisterSynchronization(new MongoSynchronization(dbHolder, mongo));
                    dbHolder.SynchronizedWithTransaction = true;
                }

                if (holderDatabase != null)
                {
                    return(holderDatabase);
                }
            }

            // Lookup fresh database instance
            Log.Debug(m => m("Getting Mongo Database name=[{0}]", databaseName));

            if (writeConcern == null)
            {
                writeConcern = WriteConcern.Acknowledged;
            }

            var newDatabase = credentials != null
                                  ? mongo.GetDatabase(databaseName, credentials, writeConcern)
                                  : mongo.GetDatabase(databaseName, writeConcern);

            // TX sync active, bind new database to thread
            if (TransactionSynchronizationManager.SynchronizationActive)
            {
                Log.Debug(
                    m => m("Registering Spring transaction synchronization for MongoDB instance {0}.", databaseName));

                DatabaseHolder holderToUse = dbHolder;

                if (holderToUse == null)
                {
                    holderToUse = new DatabaseHolder(databaseName, newDatabase);
                }
                else
                {
                    holderToUse.AddDatabase(databaseName, newDatabase);
                }

                TransactionSynchronizationManager.RegisterSynchronization(new MongoSynchronization(holderToUse, mongo));
                holderToUse.SynchronizedWithTransaction = true;

                if (holderToUse != dbHolder)
                {
                    TransactionSynchronizationManager.BindResource(mongo, holderToUse);
                }
            }

            // Check whether we are allowed to return the DB.
            if (!allowCreate && !IsDbTransactional(newDatabase, mongo))
            {
                throw new InvalidOperationException("No Mongo DB bound to thread, "
                                                    +
                                                    "and configuration does not allow creation of non-transactional one here");
            }

            return(newDatabase);
        }
        private static MongoDatabase DoGetDatabase(MongoServer mongo, string databaseName, MongoCredentials credentials,
                                                   WriteConcern writeConcern, bool allowCreate)
        {
            var dbHolder = (DatabaseHolder)TransactionSynchronizationManager.GetResource(mongo);

            // Do we have a populated holder and TX sync active?
            if (dbHolder != null && !dbHolder.IsEmpty && TransactionSynchronizationManager.SynchronizationActive)
            {
                var holderDatabase = dbHolder.GetDatabase(databaseName);

                // DB found but not yet synchronized
                if (holderDatabase != null && !dbHolder.SynchronizedWithTransaction)
                {

                    Log.Debug(
                        m => m("Registering Spring transaction synchronization for existing MongoDB {0}.", databaseName));

                    TransactionSynchronizationManager.RegisterSynchronization(new MongoSynchronization(dbHolder, mongo));
                    dbHolder.SynchronizedWithTransaction = true;
                }

                if (holderDatabase != null)
                {
                    return holderDatabase;
                }
            }

            // Lookup fresh database instance
            Log.Debug(m => m("Getting Mongo Database name=[{0}]", databaseName));

            if (writeConcern == null)
                writeConcern = WriteConcern.Acknowledged;

            var newDatabase = credentials != null
                                  ? mongo.GetDatabase(databaseName, credentials, writeConcern)
                                  : mongo.GetDatabase(databaseName, writeConcern);

            // TX sync active, bind new database to thread
            if (TransactionSynchronizationManager.SynchronizationActive)
            {

                Log.Debug(
                    m => m("Registering Spring transaction synchronization for MongoDB instance {0}.", databaseName));

                DatabaseHolder holderToUse = dbHolder;

                if (holderToUse == null)
                {
                    holderToUse = new DatabaseHolder(databaseName, newDatabase);
                }
                else
                {
                    holderToUse.AddDatabase(databaseName, newDatabase);
                }

                TransactionSynchronizationManager.RegisterSynchronization(new MongoSynchronization(holderToUse, mongo));
                holderToUse.SynchronizedWithTransaction = true;

                if (holderToUse != dbHolder)
                {
                    TransactionSynchronizationManager.BindResource(mongo, holderToUse);
                }
            }

            // Check whether we are allowed to return the DB.
            if (!allowCreate && !IsDbTransactional(newDatabase, mongo))
            {
                throw new InvalidOperationException("No Mongo DB bound to thread, "
                                                    +
                                                    "and configuration does not allow creation of non-transactional one here");
            }

            return newDatabase;
        }