// Copyright (C) 2025, The Duplicati Team // https://duplicati.com, hello@duplicati.com // // Permission is hereby granted, free of charge, to any person obtaining a // copy of this software and associated documentation files (the "Software"), // to deal in the Software without restriction, including without limitation // the rights to use, copy, modify, merge, publish, distribute, sublicense, // and/or sell copies of the Software, and to permit persons to whom the // Software is furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // DEALINGS IN THE SOFTWARE. using System; using System.Data; #nullable enable namespace Duplicati.Library.Main.Database; /// /// Wraps a transaction so it can be comitted and restarted /// internal class ReusableTransaction : IDisposable { /// /// The tag used for logging /// private static readonly string LOGTAG = Logging.Log.LogTagFromType(typeof(ReusableTransaction)); /// /// The database to use /// private readonly LocalDatabase m_db; /// /// The current transaction /// private IDbTransaction? m_transaction; /// /// Creates a new reusable transaction /// /// The database to use public ReusableTransaction(LocalDatabase db, IDbTransaction? transaction = null) { m_db = db; m_transaction = transaction ?? db.BeginTransaction(); } /// /// The current transaction /// public IDbTransaction Transaction => m_transaction ?? throw new InvalidOperationException("Transaction is disposed"); /// /// Commits the current transaction and optionally restarts it /// /// The log message to use /// True if the transaction should be restarted public void Commit(string? message, bool restart = true) { if (m_transaction == null) throw new InvalidOperationException("Transaction is already disposed"); if (m_transaction != null) { using (var timer = string.IsNullOrWhiteSpace(message) ? null : new Logging.Timer(LOGTAG, message, $"CommitTransaction: {message}")) m_transaction.Commit(); m_transaction.Dispose(); m_transaction = null; } if (restart) m_transaction = m_db.BeginTransaction(); } /// public void Dispose() { m_transaction?.Dispose(); m_transaction = null; } }