Exemple #1
0
        private static SqlConnection OpenSqlConnection(string connectionKey, string transactionKey, string isolationLevelKey = null)
        {
            var connection = SqlConnection(connectionKey);

            if (connection.State != ConnectionState.Open)
            {
                try
                {
                    connection.Open();
                }
                catch (NullReferenceException exc)
                {
                    throw new Exception("OpenConnectionException", exc);
                }
                catch (InvalidOperationException exc)
                {
                    throw new Exception("OpenConnectionException", exc);
                }

                var isolationLevel = SqlIsolationLevel(isolationLevelKey);
                var transaction    = connection.BeginTransaction(isolationLevel);
                WebCallContext.SetData(transactionKey, transaction);
            }

            return(connection);
        }
Exemple #2
0
        private static IsolationLevel SqlIsolationLevel(string isolationLevelKey)
        {
            if (string.IsNullOrEmpty(isolationLevelKey))
            {
                return(DefaultIsolationLevel);
            }

            var isolationLevel = WebCallContext.GetData(isolationLevelKey);

            if (isolationLevel == null)
            {
                return(DefaultIsolationLevel);
            }

            var sqlIsolationLevel = (SqlIsolationLevel)isolationLevel;

            switch (sqlIsolationLevel)
            {
            case Infrastructure.SqlIsolationLevel.ReadCommitted:
                return(IsolationLevel.ReadCommitted);

            case Infrastructure.SqlIsolationLevel.Snapshot:
                return(IsolationLevel.Snapshot);

            case Infrastructure.SqlIsolationLevel.ReadUncommitted:
                return(IsolationLevel.ReadUncommitted);

            default:
                return(DefaultIsolationLevel);
            }
        }
Exemple #3
0
        public void Commit()
        {
            var transaction = (SqlTransaction)WebCallContext.GetData(TransactionKey);

            if (transaction != null)
            {
                // DomainEvents.Raise(new BeforeCommit());
                transaction.Commit();
                //DomainEvents.Raise(new AfterCommit());
            }
        }
Exemple #4
0
        private static SqlTransaction Transaction(string connectionKey, string transactionKey)
        {
            var transaction = WebCallContext.GetData(transactionKey);

            if (transaction == null)
            {
                OpenSqlConnection(connectionKey, transactionKey);
                transaction = WebCallContext.GetData(transactionKey);
            }

            return((SqlTransaction)transaction);
        }
Exemple #5
0
        private static SqlConnection SqlConnection(string connectionKey)
        {
            var sqlConnection = WebCallContext.GetData(connectionKey);

            if (sqlConnection == null)
            {
                throw new Exception("No sql connection available");
            }

            var connection = (SqlConnection)sqlConnection;

            return(connection);
        }
Exemple #6
0
 public SqlConnectionContext(SqlConnectionType connectiontype, SqlIsolationLevel sqlIsolationLevel)
 {
     lock (LockObject)
     {
         if (WebCallContext.GetData(ConnectionKey) == null)
         {
             WebCallContext.SetData(ConnectionKey, new  SqlConnection(ConnectionString));
             WebCallContext.SetData(IsolationLevelKey, sqlIsolationLevel);
         }
         else
         {
             throw new Exception("There is already a sql connection");
         }
     }
 }
Exemple #7
0
 private static void FreeTransaction()
 {
     WebCallContext.FreeNamedDataSlot(TransactionKey);
     WebCallContext.FreeNamedDataSlot(ConnectionKey);
 }