コード例 #1
0
        private static SqlConnection OpenSqlConnection(string connectionKey, string transactionKey)
        {
            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 transaction = connection.BeginTransaction(IsolationLevel.ReadUncommitted);
                WebCallContext.SetData(transactionKey, transaction);
            }

            return(connection);
        }
コード例 #2
0
        public void Commit()
        {
            var transaction = (SqlTransaction)WebCallContext.GetData(TransactionKey);

            if (transaction != null)
            {
                transaction.Commit();
            }
        }
コード例 #3
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);
        }
コード例 #4
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);
        }
コード例 #5
0
 public SqlConnectionContext()
 {
     lock (LockObject)
     {
         if (WebCallContext.GetData(ConnectionKey) == null)
         {
             WebCallContext.SetData(ConnectionKey, new SqlConnection(ConnectionString));
         }
         else
         {
             throw new Exception("There is already a sql connection");
         }
     }
 }
コード例 #6
0
 private static void FreeTransaction()
 {
     WebCallContext.FreeNamedDataSlot(TransactionKey);
     WebCallContext.FreeNamedDataSlot(ConnectionKey);
 }