///<summary> /// Ensures that the database exists, creating it if needed ///</summary> /// <remarks> /// This operation happens _outside_ of any transaction /// </remarks> public static void EnsureDatabaseExists(this IDatabaseCommands self, string name, bool ignoreFailures = false) { var serverClient = self.ForDefaultDatabase() as ServerClient; if (serverClient == null) { throw new InvalidOperationException("Ensuring database existence requires a Server Client but got: " + self); } var doc = MultiDatabase.CreateDatabaseDocument(name); var docId = "Raven/Databases/" + name; try { if (serverClient.Get(docId) != null) { return; } var req = serverClient.CreateRequest("PUT", "/admin/databases/" + Uri.EscapeDataString(name)); req.Write(doc.ToString(Formatting.Indented)); req.ExecuteRequest(); } catch (Exception) { if (ignoreFailures == false) { throw; } } }
///<summary> /// Ensures that the database exists, creating it if needed ///</summary> /// <remarks> /// This operation happens _outside_ of any transaction /// </remarks> public static void EnsureDatabaseExists(this IDatabaseCommands self, string name, bool ignoreFailures = false) { self = self.ForDefaultDatabase(); AssertValidName(name); var doc = RavenJObject.FromObject(new DatabaseDocument { Settings = { { "Raven/DataDir", Path.Combine("~", Path.Combine("Tenants", name)) } } }); doc.Remove("Id"); var docId = "Raven/Databases/" + name; using (new TransactionScope(TransactionScopeOption.Suppress)) { try { if (self.Get(docId) != null) { return; } self.Put(docId, null, doc, new RavenJObject()); } catch (Exception) { if (ignoreFailures == false) { throw; } } } }
///<summary> /// Ensures that the database exists, creating it if needed ///</summary> /// <remarks> /// This operation happens _outside_ of any transaction /// </remarks> public static void EnsureDatabaseExists(this IDatabaseCommands self, string name, bool ignoreFailures = false) { self = self.ForDefaultDatabase(); var doc = MultiDatabase.CreateDatabaseDocument(name); var docId = "Raven/Databases/" + name; using (new TransactionScope(TransactionScopeOption.Suppress)) { try { if (self.Get(docId) != null) { return; } self.Put(docId, null, doc, new RavenJObject()); } catch (Exception) { if (ignoreFailures == false) { throw; } } } }
public static void CreateDatabase(this IDatabaseCommands self, DatabaseDocument databaseDocument) { var serverClient = self.ForDefaultDatabase() as ServerClient; if (serverClient == null) { throw new InvalidOperationException("Ensuring database existence requires a Server Client but got: " + self); } if (databaseDocument.Settings.ContainsKey("Raven/DataDir") == false) { throw new InvalidOperationException("The Raven/DataDir setting is mandatory"); } var doc = RavenJObject.FromObject(databaseDocument); doc.Remove("Id"); var req = serverClient.CreateRequest("PUT", "/admin/databases/" + Uri.EscapeDataString(databaseDocument.Id)); req.Write(doc.ToString(Formatting.Indented)); req.ExecuteRequest(); }
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.ForDefaultDatabase(); 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); } }
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); } }
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.ForDefaultDatabase(); 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); } } }