private void CloseTransaction() { if (Transaction != null) { IDbConnection connection = DAO.Transaction?.Connection; IDbTransaction transaction = DAO.Transaction; Transaction = null; DAO.Transaction = null; DAO.Connection = new SqlConnection(ConnectionString); Items = AutoCache; Access = AAO; transaction?.Dispose(); if (connection != null && connection.State == ConnectionState.Open) { connection.Close(); } } }
/// <summary> /// Adds the table to a transaction. /// </summary> /// <param name="transaction">The transaction</param> public void BeginTransaction(DbCacheTransaction transaction) { if (Transaction != null) { throw new InvalidOperationException("Cache is already part of a transaction."); } if (transaction.Connection.State != ConnectionState.Open) { throw new InvalidOperationException("The transaction is closed."); } DAO.Connection = transaction.Transaction.Connection; DAO.Transaction = transaction.Transaction; CacheTransactionStorage <T, R> storage = new CacheTransactionStorage <T, R>(Builder, AutoCache.Cache, transaction, CloseTransaction); Items = storage; transaction.TransactionStorage.Add(storage); Access = DAO; Transaction = transaction; }
/// <summary> /// Begins a transaction. /// </summary> /// <returns>The transaction.</returns> public DbCacheTransaction BeginTransaction() { try { DAO.Connection.Open(); DAO.Transaction = DAO.Connection.BeginTransaction(); DbCacheTransaction transaction = new DbCacheTransaction(DAO.Transaction); BeginTransaction(transaction); return(transaction); } catch (Exception ex) { DAO.Transaction = null; Transaction = null; if (DAO.Connection.State == ConnectionState.Open) { DAO.Connection.Close(); } throw; } }