/******************************************************** * ADO.NET 2.0 Data Provider for SQLite Version 3.X * Written by Robert Simpson (robert@blackcastlesoft.com) * * Released to the public domain, use at your own risk! ********************************************************/ namespace System.Data.SQLite { using System; using System.Data; using System.Data.Common; /// /// SQLite implementation of DbTransaction. /// public sealed class SQLiteTransaction : DbTransaction { /// /// The connection to which this transaction is bound /// internal SQLiteConnection _cnn; /// /// Constructs the transaction object, binding it to the supplied connection /// /// The connection to open a transaction on /// TRUE to defer the writelock, or FALSE to lock immediately internal SQLiteTransaction(SQLiteConnection cnn, bool deferredLock) { try { if (!deferredLock) cnn._sql.Execute("BEGIN IMMEDIATE"); else cnn._sql.Execute("BEGIN"); _cnn = cnn; } catch (SQLiteException) { BaseDispose(); throw; } } /// /// Commits the current transaction. /// public override void Commit() { if (_cnn == null) throw new ArgumentNullException(); try { _cnn._sql.Execute("COMMIT"); } catch (SQLiteException) { BaseDispose(); throw; } BaseDispose(); } /// /// Returns the underlying connection to which this transaction applies. /// public new SQLiteConnection Connection { get { return _cnn; } } /// /// Forwards to the local Connection property /// protected override DbConnection DbConnection { get { return Connection; } } /// /// Disposes the transaction. If it is currently active, any changes are rolled back. /// protected override void Dispose(bool disposing) { if (_cnn != null) Rollback(); _cnn = null; base.Dispose(disposing); } /// /// Gets the isolation level of the transaction. SQLite does not support isolation levels, so this always returns Unspecified. /// public override IsolationLevel IsolationLevel { get { return IsolationLevel.Unspecified; } } /// /// Rolls back the active transaction. /// public override void Rollback() { if (_cnn == null) throw new ArgumentNullException(); try { _cnn._sql.Execute("ROLLBACK"); } catch (SQLiteException) { BaseDispose(); throw; } BaseDispose(); } private void BaseDispose() { _cnn._activeTransaction = null; _cnn = null; } } }