Files

65 lines
2.0 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Data.Sqlite;
using XP.Common.Database.Interfaces;
using XP.Common.Logging.Interfaces;
namespace XP.Common.Database.Implementations
{
/// <summary>
/// SQLite 事务实现
/// </summary>
public class SqliteTransaction : IDbTransaction
{
private readonly Microsoft.Data.Sqlite.SqliteTransaction _innerTransaction;
private readonly ILoggerService _logger;
private bool _isDisposed = false;
public SqliteTransaction(Microsoft.Data.Sqlite.SqliteTransaction innerTrans, ILoggerService logger)
{
_innerTransaction = innerTrans ?? throw new ArgumentNullException(nameof(innerTrans));
_logger = logger.ForModule("Sqlite.Transaction");
}
public IDbExecuteResult Commit()
{
try
{
_innerTransaction.Commit();
_logger.Debug("SQLite事务提交成功");
return DbExecuteResult.Success("事务提交成功");
}
catch (Exception ex)
{
_logger.Error(ex, "SQLite事务提交失败");
return DbExecuteResult.Fail("事务提交失败", ex);
}
}
public IDbExecuteResult Rollback()
{
try
{
_innerTransaction.Rollback();
_logger.Debug("SQLite事务回滚成功");
return DbExecuteResult.Success("事务回滚成功");
}
catch (Exception ex)
{
_logger.Error(ex, "SQLite事务回滚失败");
return DbExecuteResult.Fail("事务回滚失败", ex);
}
}
public void Dispose()
{
if (_isDisposed) return;
_innerTransaction.Dispose();
_isDisposed = true;
_logger.Debug("SQLite事务资源已释放");
}
}
}