Files
duplicati/Duplicati/Library/Main/Database/ReusableTransaction.cs
T

141 lines
5.0 KiB
C#
Raw Normal View History

// Copyright (C) 2025, The Duplicati Team
// https://duplicati.com, hello@duplicati.com
2025-05-13 11:28:23 +02:00
//
// 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:
2025-05-13 11:28:23 +02:00
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
2025-05-13 11:28:23 +02:00
//
// 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.
2025-05-13 11:28:23 +02:00
2025-03-14 10:41:14 +01:00
using System;
using System.Threading.Tasks;
2025-05-13 16:15:53 +02:00
using Duplicati.Library.Utility;
using Microsoft.Data.Sqlite;
2025-03-14 10:41:14 +01:00
#nullable enable
namespace Duplicati.Library.Main.Database;
/// <summary>
2025-05-13 16:16:06 +02:00
/// Wraps a transaction so it can be comitted and restarted.
2025-03-14 10:41:14 +01:00
/// </summary>
2025-05-13 16:16:06 +02:00
/// <remarks>
/// Creates a new reusable transaction.
/// </remarks>
/// <param name="db">The database to use.</param>
/// <param name="transaction">The transaction to use. If null, a new transaction is created.</param>
2025-05-13 16:15:53 +02:00
internal class ReusableTransaction(LocalDatabase db, SqliteTransaction? transaction = null) : IDisposable
2025-03-14 10:41:14 +01:00
{
/// <summary>
2025-05-13 16:16:06 +02:00
/// The tag used for logging.
2025-03-14 10:41:14 +01:00
/// </summary>
private static readonly string LOGTAG = Logging.Log.LogTagFromType(typeof(ReusableTransaction));
2025-03-14 10:41:14 +01:00
/// <summary>
2025-05-13 16:16:06 +02:00
/// The database to use.
2025-03-14 10:41:14 +01:00
/// </summary>
2025-05-13 16:15:53 +02:00
private readonly LocalDatabase m_db = db;
2025-03-14 10:41:14 +01:00
/// <summary>
2025-05-13 16:16:06 +02:00
/// The current transaction.
2025-03-14 10:41:14 +01:00
/// </summary>
2025-05-13 16:15:53 +02:00
private SqliteTransaction m_transaction = transaction ?? db.Connection.BeginTransaction();
2025-03-14 10:41:14 +01:00
/// <summary>
2025-05-13 16:16:06 +02:00
/// True if the transaction is disposed.
2025-03-14 10:41:14 +01:00
/// </summary>
2025-05-13 16:15:53 +02:00
private bool m_disposed = false;
2025-03-14 10:41:14 +01:00
/// <summary>
2025-05-13 16:16:06 +02:00
/// The current transaction.
2025-03-14 10:41:14 +01:00
/// </summary>
2025-05-13 16:15:53 +02:00
public SqliteTransaction Transaction => m_disposed ? m_transaction : throw new InvalidOperationException("Transaction is disposed");
2025-03-14 10:41:14 +01:00
/// <summary>
2025-05-13 16:16:06 +02:00
/// Commits the current transaction and optionally restarts it.
2025-03-14 10:41:14 +01:00
/// </summary>
2025-05-13 16:16:06 +02:00
/// <remarks>
/// Calls the Async version of this method and awaits it.
/// </remarks>
/// <param name="message">The log message to use.</param>
/// <param name="restart">True if the transaction should be restarted.</param>
/// <exception cref="InvalidOperationException">If the transaction is already Disposed.</exception>
public void Commit(string? message = null, bool restart = true)
2025-03-14 10:41:14 +01:00
{
2025-05-13 16:15:53 +02:00
CommitAsync(message, restart).Await();
}
2025-05-13 16:16:06 +02:00
/// <summary>
/// Async version of Commit: <inheritdoc cref="Commit(string?, bool)"/>
/// </summary>
/// <param name="message">The log message to use.</param>
/// <param name="restart">True if the transaction should be restarted.</param>
/// <returns>An awaitable task.</returns>
/// <exception cref="InvalidOperationException">If the transaction is already Disposed.</exception>
public async Task CommitAsync(string? message = null, bool restart = true)
{
2025-05-13 16:15:53 +02:00
if (m_disposed)
throw new InvalidOperationException("Transaction is already disposed");
2025-05-13 16:15:53 +02:00
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();
2025-05-13 16:15:53 +02:00
else
m_disposed = true;
2025-03-14 10:41:14 +01:00
}
/// <inheritdoc/>
2025-05-13 16:16:06 +02:00
/// <remarks>
/// Calls the Async version of this method and awaits it.
/// </remarks>
2025-03-14 10:41:14 +01:00
public void Dispose()
{
2025-05-13 16:15:53 +02:00
DisposeAsync().Await();
}
2025-05-13 16:16:06 +02:00
/// <summary>
/// Async version of Dispose: <inheritdoc cref="Dispose()"/>
/// </summary>
/// <returns>An awaitable task.</returns>
2025-05-13 16:15:53 +02:00
public async Task DisposeAsync()
{
if (!m_disposed)
{
m_disposed = true;
await m_transaction.DisposeAsync();
}
2025-03-14 10:41:14 +01:00
}
2025-05-20 07:34:14 +02:00
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;
}
}
2025-03-14 10:41:14 +01:00
}