📄 sqlitetransaction.cs
字号:
namespace Imps.Client.Data
{
using System;
public sealed class SQLiteTransaction : IDisposable
{
internal SQLiteConnection _cnn;
internal long _version;
internal SQLiteTransaction(SQLiteConnection connection, bool deferredLock)
{
this._cnn = connection;
this._version = this._cnn._version;
if (this._cnn._transactionLevel++ == 0)
{
try
{
using (SQLiteCommand command = this._cnn.CreateCommand())
{
if (!deferredLock)
{
command.CommandText = "BEGIN IMMEDIATE";
}
else
{
command.CommandText = "BEGIN";
}
command.ExecuteNonQuery();
}
}
catch (SQLiteException)
{
this._cnn._transactionLevel--;
this._cnn = null;
throw;
}
}
}
public void Commit()
{
this.IsValid(true);
if ((this._cnn._transactionLevel - 1) == 0)
{
using (SQLiteCommand command = this._cnn.CreateCommand())
{
command.CommandText = "COMMIT";
command.ExecuteNonQuery();
}
}
this._cnn._transactionLevel--;
this._cnn = null;
}
public void Dispose()
{
this.Dispose(true);
}
private void Dispose(bool disposing)
{
if (disposing)
{
if (this.IsValid(false))
{
this.Rollback();
}
this._cnn = null;
}
}
internal bool IsValid(bool throwError)
{
if (this._cnn == null)
{
if (throwError)
{
throw new ArgumentNullException("No connection associated with this transaction");
}
return false;
}
if (this._cnn._transactionLevel == 0)
{
if (throwError)
{
throw new SQLiteException(0x15, "No transaction is active on this connection");
}
return false;
}
if (this._cnn._version != this._version)
{
if (throwError)
{
throw new SQLiteException(0x15, "The connection was closed and re-opened, changes were rolled back");
}
return false;
}
if (this._cnn.State == ConnectionState.Open)
{
return true;
}
if (throwError)
{
throw new SQLiteException(0x15, "Connection was closed");
}
return false;
}
public void Rollback()
{
this.IsValid(true);
using (SQLiteCommand command = this._cnn.CreateCommand())
{
command.CommandText = "ROLLBACK";
command.ExecuteNonQuery();
}
this._cnn._transactionLevel = 0;
this._cnn = null;
}
public SQLiteConnection Connection
{
get
{
return this._cnn;
}
}
public Imps.Client.Data.IsolationLevel IsolationLevel
{
get
{
return Imps.Client.Data.IsolationLevel.Serializable;
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -