Пример #1
0
        public SetupDefaultDatabase()
        {
            #region default_database_1
            // without specifying `DefaultDatabase`
            // created `DatabaseCommands` or opened `Sessions`
            // will work on `<system>` database by default
            // if no database is passed explicitly
            using (IDocumentStore store = new DocumentStore
            {
                Url = "http://localhost:8080/"
            }.Initialize())
            {
                IDatabaseCommands commands = store.DatabaseCommands;
                using (IDocumentSession session = store.OpenSession())
                {
                    // ...
                }

                IDatabaseCommands northwindCommands = commands.ForDatabase("Northwind");
                using (IDocumentSession northwindSession = store.OpenSession("Northwind"))
                {
                    // ...
                }
            }
            #endregion

            #region default_database_2
            // when `DefaultDatabase` is set to `Northwind`
            // created `DatabaseCommands` or opened `Sessions`
            // will work on `Northwind` database by default
            // if no database is passed explicitly
            using (IDocumentStore store = new DocumentStore
            {
                Url = "http://localhost:8080/",
                DefaultDatabase = "Northwind"
            }.Initialize())
            {
                IDatabaseCommands northwindCommands = store.DatabaseCommands;
                using (IDocumentSession northwindSession = store.OpenSession())
                {
                    // ...
                }

                IDatabaseCommands adventureWorksCommands = northwindCommands.ForDatabase("AdventureWorks");
                using (IDocumentSession adventureWorksSession = store.OpenSession("AdventureWorks"))
                {
                    // ...
                }

                IDatabaseCommands systemCommands = northwindCommands.ForSystemDatabase();
                using (IDocumentSession systemSession = store.OpenSession(Constants.SystemDatabase))
                {
                    // ..
                }
            }
            #endregion
        }
Пример #2
0
 private static IDatabaseCommands SetupCommands(IDatabaseCommands databaseCommands, string database, ICredentials credentialsForSession, OpenSessionOptions options)
 {
     if (database != null)
     {
         databaseCommands = databaseCommands.ForDatabase(database);
     }
     if (credentialsForSession != null)
     {
         databaseCommands = databaseCommands.With(credentialsForSession);
     }
     if (options.ForceReadFromMaster)
     {
         databaseCommands.ForceReadFromMaster();
     }
     return(databaseCommands);
 }
Пример #3
0
        public void Execute(Guid myResourceManagerId, IDatabaseCommands commands)
        {
            var resourceManagersRequiringRecovery = new HashSet <Guid>();
            var filesToDelete = new List <string>();

            using (var ctx = documentStore.TransactionRecoveryStorage.Create())
            {
                foreach (var file in ctx.GetFileNames("*.recovery-information"))
                {
                    var txId = string.Empty;
                    try
                    {
                        Stream stream;
                        try
                        {
                            stream = ctx.OpenRead(file);
                        }
                        catch (Exception e)
                        {
                            logger.WarnException(
                                "Could not open recovery information: " + file +
                                ", this is expected if it is an active transaction / held by another server", e);
                            continue;
                        }
                        using (stream)
                            using (var reader = new BinaryReader(stream))
                            {
                                var resourceManagerId = new Guid(reader.ReadString());

                                if (myResourceManagerId != resourceManagerId)
                                {
                                    continue; // it doesn't belong to us, ignore
                                }
                                filesToDelete.Add(file);
                                txId = reader.ReadString();

                                var db = reader.ReadString();

                                var dbCmds = string.IsNullOrEmpty(db) == false
                                             ? commands.ForDatabase(db)
                                             : commands.ForSystemDatabase();

                                TransactionManager.Reenlist(resourceManagerId, stream.ReadData(), new InternalEnlistment(dbCmds, txId));
                                resourceManagersRequiringRecovery.Add(resourceManagerId);
                                logger.Info("Recovered transaction {0}", txId);
                            }
                    }
                    catch (Exception e)
                    {
                        logger.WarnException("Could not re-enlist in DTC transaction for tx: " + txId, e);
                    }
                }

                foreach (var rm in resourceManagersRequiringRecovery)
                {
                    try
                    {
                        TransactionManager.RecoveryComplete(rm);
                    }
                    catch (Exception e)
                    {
                        logger.WarnException("Could not properly complete recovery of resource manager: " + rm, e);
                    }
                }

                var errors = new List <Exception>();
                foreach (var file in filesToDelete)
                {
                    try
                    {
                        ctx.DeleteFile(file);
                    }
                    catch (Exception e)
                    {
                        errors.Add(e);
                    }
                }
                if (errors.Count > 0)
                {
                    throw new AggregateException(errors);
                }
            }
        }
Пример #4
0
		public void Execute(Guid myResourceManagerId, IDatabaseCommands commands)
		{
			var resourceManagersRequiringRecovery = new HashSet<Guid>();
			using (var store = IsolatedStorageFile.GetMachineStoreForDomain())
			{
				var filesToDelete = new List<string>();
				foreach (var file in store.GetFileNames("*.recovery-information"))
				{
					var txId = Guid.Empty;
					try
					{
						IsolatedStorageFileStream stream;
						try
						{
							stream = store.OpenFile(file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
						}
						catch (Exception e)
						{
							logger.WarnException("Could not open recovery information: " + file +", this is expected if it is an active transaction / held by another server", e);
							continue;
						}
						using (stream)
						using(var reader = new BinaryReader(stream))
						{
							var resourceManagerId = new Guid(reader.ReadString());

							if(myResourceManagerId != resourceManagerId)
								continue; // it doesn't belong to us, ignore
							filesToDelete.Add(file);
							txId = new Guid(reader.ReadString());

							var db = reader.ReadString();

							var dbCmds = string.IsNullOrEmpty(db) == false ? 
								commands.ForDatabase(db) : 
								commands.ForSystemDatabase();

							TransactionManager.Reenlist(resourceManagerId, stream.ReadData(), new InternalEnlistment(dbCmds, txId));
							resourceManagersRequiringRecovery.Add(resourceManagerId);
							logger.Info("Recovered transaction {0}", txId);
						}
					}
					catch (Exception e)
					{
						logger.WarnException("Could not re-enlist in DTC transaction for tx: " + txId, e);
					}
				}

				foreach (var rm in resourceManagersRequiringRecovery)
				{
					try
					{
						TransactionManager.RecoveryComplete(rm);
					}
					catch (Exception e)
					{
						logger.WarnException("Could not properly complete recovery of resource manager: " + rm, e);
					}
				}

				var errors = new List<Exception>();
				foreach (var file in filesToDelete)
				{
					try
					{
						if (store.FileExists(file))
							store.DeleteFile(file);
					}
					catch (Exception e)
					{
						errors.Add(e);
					}
				}
				if (errors.Count > 0)
					throw new AggregateException(errors);
			}
		}
Пример #5
0
 public IAsyncDatabaseCommands ForDatabase(string database)
 {
     return(new EmbeddedAsyncServerClient(databaseCommands.ForDatabase(database)));
 }
Пример #6
0
 private static IDatabaseCommands SetupCommands(IDatabaseCommands databaseCommands, string database, ICredentials credentialsForSession, OpenSessionOptions options)
 {
     if (database != null)
         databaseCommands = databaseCommands.ForDatabase(database);
     if (credentialsForSession != null)
         databaseCommands = databaseCommands.With(credentialsForSession);
     if (options.ForceReadFromMaster)
         databaseCommands.ForceReadFromMaster();
     return databaseCommands;
 }
Пример #7
0
        public void Execute(IDatabaseCommands commands)
        {
            var resourceManagersRequiringRecovery = new HashSet <Guid>();

            using (var store = IsolatedStorageFile.GetMachineStoreForDomain())
            {
                foreach (var file in store.GetFileNames("*.recovery-information"))
                {
                    var txId = Guid.Empty;
                    try
                    {
                        using (var fileStream = store.OpenFile(file, FileMode.Open, FileAccess.Read))
                            using (var reader = new BinaryReader(fileStream))
                            {
                                var resourceManagerId = new Guid(reader.ReadString());
                                txId = new Guid(reader.ReadString());

                                var db = reader.ReadString();

                                var dbCmds = string.IsNullOrEmpty(db) == false?
                                             commands.ForDatabase(db) :
                                                 commands.ForDefaultDatabase();

                                TransactionManager.Reenlist(resourceManagerId, fileStream.ReadData(), new InternalEnlistment(dbCmds, txId));
                                resourceManagersRequiringRecovery.Add(resourceManagerId);
                                logger.Info("Recovered transaction {0}", txId);
                            }
                    }
                    catch (Exception e)
                    {
                        logger.WarnException("Could not re-enlist in DTC transaction for tx: " + txId, e);
                    }
                }

                foreach (var rm in resourceManagersRequiringRecovery)
                {
                    try
                    {
                        TransactionManager.RecoveryComplete(rm);
                    }
                    catch (Exception e)
                    {
                        logger.WarnException("Could not properly complete recovery of resource manager: " + rm, e);
                    }
                }

                var errors = new List <Exception>();
                foreach (var file in store.GetFileNames("*.recovery-information"))
                {
                    try
                    {
                        if (store.FileExists(file))
                        {
                            store.DeleteFile(file);
                        }
                    }
                    catch (Exception e)
                    {
                        errors.Add(e);
                    }
                }
                if (errors.Count > 0)
                {
                    throw new AggregateException(errors);
                }
            }
        }
		public void Execute(IDatabaseCommands commands)
		{
			var resourceManagersRequiringRecovery = new HashSet<Guid>();
			using (var store = IsolatedStorageFile.GetMachineStoreForDomain())
			{
				foreach (var file in store.GetFileNames("*.recovery-information"))
				{
					var txId = Guid.Empty;
					try
					{
						using (var fileStream = store.OpenFile(file, FileMode.Open, FileAccess.Read))
						using(var reader = new BinaryReader(fileStream))
						{
							var resourceManagerId = new Guid(reader.ReadString());
							txId = new Guid(reader.ReadString());

							var db = reader.ReadString();

							var dbCmds = string.IsNullOrEmpty(db) == false ? 
								commands.ForDatabase(db) : 
								commands.ForDefaultDatabase();

							TransactionManager.Reenlist(resourceManagerId, fileStream.ReadData(), new InternalEnlistment(dbCmds, txId));
							resourceManagersRequiringRecovery.Add(resourceManagerId);
							logger.Info("Recovered transaction {0}", txId);
						}
					}
					catch (Exception e)
					{
						logger.WarnException("Could not re-enlist in DTC transaction for tx: " + txId, e);
					}
				}

				foreach (var rm in resourceManagersRequiringRecovery)
				{
					try
					{
						TransactionManager.RecoveryComplete(rm);
					}
					catch (Exception e)
					{
						logger.WarnException("Could not properly complete recovery of resource manager: " + rm, e);
					}
				}

				var errors = new List<Exception>();
				foreach (var file in store.GetFileNames("*.recovery-information"))
				{
					try
					{
						if (store.FileExists(file))
							store.DeleteFile(file);
					}
					catch (Exception e)
					{
						errors.Add(e);
					}
				}
				if (errors.Count > 0)
					throw new AggregateException(errors);
			}
		}