namespace Zeta.EnterpriseLibrary.Data.Base { using System; using System.Data; using System.Data.Common; public abstract class AdoNetBaseTransactionScope<TConnection> : IAdoNetTransactionScope where TConnection : DbConnection, new() { protected AdoNetBaseTransactionScope( IAdoNetQuerier associatedQuerier) { _querier = associatedQuerier; } ~AdoNetBaseTransactionScope() { doDispose(); } public event EventHandler Began; public event EventHandler Committed; public event EventHandler RolledBack; protected virtual void OnBegan(EventArgs eventArgs) { var h = Began; if (h != null) { h(this, eventArgs); } } protected virtual void OnCommitted(EventArgs eventArgs) { var h = Committed; if (h != null) { h(this, eventArgs); } } protected virtual void OnRolledBack(EventArgs eventArgs) { var h = RolledBack; if (h != null) { h(this, eventArgs); } } public IAdoNetQuerier Querier { get { return _querier; } } public virtual void BeginTransaction() { if (_transaction != null) { throw new ArgumentException(); } else { _transaction = GetConnection().BeginTransaction(); OnBegan(EventArgs.Empty); } } public virtual void BeginTransaction(IsolationLevel isolationLevel) { if (_transaction != null) { throw new ArgumentException(); } else { _transaction = GetConnection().BeginTransaction(isolationLevel); OnBegan(EventArgs.Empty); } } private TConnection _connection; public DbConnection GetConnection() { return GetConnection(Querier.ConnectionString); } public DbConnection GetConnection( SmartConnectionString connectionString) { if (_connection == null) { _connection = new TConnection { ConnectionString = connectionString.ConnectionString }; _connection.Open(); } return _connection; } public DbTransaction Transaction { get { return _transaction; } } public bool HasTransaction { get { return _transaction != null; } } public virtual void CommitTransaction() { if (_transaction == null) { throw new ArgumentNullException(); } else { _transaction.Commit(); _wasCommitted = true; OnCommitted(EventArgs.Empty); } } public virtual void RollbackTransaction() { if (_transaction == null) { throw new ArgumentNullException(); } else { _transaction.Rollback(); OnRolledBack(EventArgs.Empty); } } private DbTransaction _transaction; private readonly IAdoNetQuerier _querier; private bool _wasCommitted; #region IDisposable Member public void Dispose() { doDispose(); GC.SuppressFinalize(this); } private void doDispose() { if (_transaction != null) { var t = _transaction; _transaction = null; if (!_wasCommitted) { t.Rollback(); } t.Dispose(); } if (_connection != null) { var c = _connection; _connection = null; c.Dispose(); } } #endregion } }
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.
This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)