/// <summary> /// Disposes the object via a special disposing task that is executed with the exclusive scheduler. /// </summary> public void Dispose() { this.disposed = true; if (this.singleThread == null) { return; } ManualResetEventSlim cleaned = new ManualResetEventSlim(); new Task(() => { if (this.Transaction != null) { this.transaction.Dispose(); this.transaction = null; } if (this.engine != null) { this.engine.Dispose(); this.engine = null; } this.singleThread.Dispose(); this.singleThread = null; cleaned.Set(); }).Start(this.singleThread); cleaned.Wait(); }
/// <summary> /// Initializes a new instance of the object. /// <para>It creates an exclusive scheduler for tasks accessing database and initializes the database engine on disk.</para> /// </summary> /// <param name="threadName">Name of the exclusive thread that will care about tasks accessing the database in exclusive manner.</param> /// <param name="folder">Path to the directory to hold DBreeze database files.</param> public DBreezeSingleThreadSession(string threadName, string folder) { Guard.NotEmpty(threadName, nameof(threadName)); Guard.NotEmpty(folder, nameof(folder)); this.singleThread = new CustomThreadPoolTaskScheduler(1, 100, threadName); new Task(() => { DBreeze.Utils.CustomSerializator.ByteArraySerializator = NBitcoinSerialize; DBreeze.Utils.CustomSerializator.ByteArrayDeSerializator = NBitcoinDeserialize; this.engine = new DBreezeEngine(folder); this.transaction = this.engine.GetTransaction(); }).Start(this.singleThread); }
/// <summary> /// Initializes a new instance of the object. /// <para>It creates an exclusive scheduler for tasks accessing database and initializes the database engine on disk.</para> /// </summary> /// <param name="threadName">Name of the exclusive thread that will care about tasks accessing the database in exclusive manner.</param> /// <param name="folder">Path to the directory to hold DBreeze database files.</param> public DBreezeSingleThreadSession(string threadName, string folder) { Guard.NotEmpty(threadName, nameof(threadName)); Guard.NotEmpty(folder, nameof(folder)); this.singleThread = new CustomThreadPoolTaskScheduler(1, 100, threadName); var task = new Task(() => { DBreeze.Utils.CustomSerializator.ByteArraySerializator = NBitcoinSerialize; DBreeze.Utils.CustomSerializator.ByteArrayDeSerializator = NBitcoinDeserialize; this.engine = new DBreezeEngine(folder); this.transaction = this.engine.GetTransaction(); }); task.Start(this.singleThread); // Features are loaded sequentially, this change will wait (block execution) // after dispatching a new Task (thread) to initialize the dbreeze db. // Node: The 'DBreezeEngine' instance is not thread safe as there are // static initializers happening internally when creating a new instance. task.GetAwaiter().GetResult(); }