// 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.Threading.Tasks;
using Duplicati.Library.Utility;
using Microsoft.Data.Sqlite;
#nullable enable
namespace Duplicati.Library.Main.Database;
///
/// Wraps a transaction so it can be comitted and restarted.
///
///
/// Creates a new reusable transaction.
///
/// The database to use.
/// The transaction to use. If null, a new transaction is created.
internal class ReusableTransaction(LocalDatabase db, SqliteTransaction? transaction = null) : 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 = db;
///
/// The current transaction.
///
private SqliteTransaction m_transaction = transaction ?? db.Connection.BeginTransaction();
///
/// True if the transaction is disposed.
///
private bool m_disposed = false;
///
/// The current transaction.
///
public SqliteTransaction Transaction => m_disposed ? m_transaction : throw new InvalidOperationException("Transaction is disposed");
///
/// Commits the current transaction and optionally restarts it.
///
///
/// Calls the Async version of this method and awaits it.
///
/// The log message to use.
/// True if the transaction should be restarted.
/// If the transaction is already Disposed.
public void Commit(string? message = null, bool restart = true)
{
CommitAsync(message, restart).Await();
}
///
/// Async version of Commit:
///
/// The log message to use.
/// True if the transaction should be restarted.
/// An awaitable task.
/// If the transaction is already Disposed.
public async Task CommitAsync(string? message = null, bool restart = true)
{
if (m_disposed)
throw new InvalidOperationException("Transaction is already disposed");
using (var timer = string.IsNullOrWhiteSpace(message) ? null : new Logging.Timer(LOGTAG, message, $"CommitTransaction: {message}"))
await m_transaction.CommitAsync();
await m_transaction.DisposeAsync();
if (restart)
m_transaction = m_db.Connection.BeginTransaction();
else
m_disposed = true;
}
///
///
/// Calls the Async version of this method and awaits it.
///
public void Dispose()
{
DisposeAsync().Await();
}
///
/// Async version of Dispose:
///
/// An awaitable task.
public async Task DisposeAsync()
{
if (!m_disposed)
{
m_disposed = true;
await m_transaction.DisposeAsync();
}
}
public async Task RollBackAsync(string? message = null, bool restart = true)
{
if (m_disposed)
throw new InvalidOperationException("Transaction is already disposed");
using (var timer = new Logging.Timer(LOGTAG, message, $"RollbackTransaction: {message}"))
await m_transaction.RollbackAsync();
await m_transaction.DisposeAsync();
if (restart)
{
m_transaction = m_db.Connection.BeginTransaction();
}
else
{
m_disposed = true;
}
}
}