Example #1
0
        private void AddSongHit(IDatabaseContext <Song> session, Song song, string hostname)
        {
            if (!PermissionContext.IsLoggedIn && string.IsNullOrEmpty(hostname))
            {
                return;
            }

            var agentNum = PermissionContext.IsLoggedIn ? PermissionContext.LoggedUserId : hostname.GetHashCode();

            if (agentNum == 0)
            {
                return;
            }

            using (var tx = session.BeginTransaction(IsolationLevel.ReadUncommitted)) {
                var songId = song.Id;
                var isHit  = session.Query <SongHit>().Any(h => h.Song.Id == songId && h.Agent == agentNum);

                if (!isHit)
                {
                    var hit = new SongHit(song, agentNum);
                    session.Save(hit);
                }

                try {
                    tx.Commit();
                } catch (TransactionException x) {
                    log.Error(x, "Unable to save song hit");
                }
            }
        }
Example #2
0
        public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
        {
            if (!context.HttpContext.Request.Method.Equals("Post", StringComparison.OrdinalIgnoreCase) &&
                !context.HttpContext.Request.Method.Equals("Put", StringComparison.OrdinalIgnoreCase) &&
                !context.HttpContext.Request.Method.Equals("Delete", StringComparison.OrdinalIgnoreCase))
            {
                _context.BeginTransaction(IsolationLevel.ReadCommitted);
                var executedContext = await next.Invoke();

                _context.EndTransaction(executedContext.Exception);
            }
            else
            {
                _context.BeginTransaction(IsolationLevel.Snapshot);
                var executedContext = await next.Invoke();

                _context.EndTransaction(executedContext.Exception);
            }
        }
        public void Write(string tableName, IBulkDataReader dataReader)
        {
            // Perform Bulk Copy
            SqlConnection sqlConn = _sqlContext.GetConnection();

            if (sqlConn.State != ConnectionState.Open)
            {
                sqlConn.Open();
            }

            using (ITransactionScope <SqlTransaction> sqlTransaction = _sqlContext.BeginTransaction())
            {
                Write(tableName, dataReader, sqlConn, sqlTransaction.Current);
                sqlTransaction.Commit();
            }
        }
Example #4
0
        public void CreateHit <TEntry, THit>(IDatabaseContext ctx, TEntry entry, string hostname, IUserPermissionContext userContext, Func <TEntry, int, THit> factory)
            where TEntry : class, IEntryBase where THit : GenericEntryHit <TEntry>
        {
            if (!userContext.IsLoggedIn && string.IsNullOrEmpty(hostname))
            {
                return;
            }

            var agentNum = userContext.IsLoggedIn ? userContext.LoggedUserId : hostname.GetHashCode();

            if (agentNum == 0)
            {
                return;
            }

            using (var tx = ctx.BeginTransaction(IsolationLevel.ReadUncommitted))
            {
                var entryId = entry.Id;
                var isHit   = ctx.Query <THit>().Any(h => h.Entry.Id == entryId && h.Agent == agentNum);

                if (!isHit)
                {
                    var hit = factory(entry, agentNum);
                    try
                    {
                        ctx.Save(hit);
                    }
                    catch (GenericADOException x)
                    {
                        // This can happen if the uniqueness constraint is violated. We could use pessimistic locking, but it's not important enough here.
                        s_log.Warn("Unable to save hit for {0}: {1}", entry, x.Message);
                        return;
                    }

                    try
                    {
                        tx.Commit();
                    }
                    catch (TransactionException x)
                    {
                        s_log.Warn(x, "Unable to save hit for {0}", entry);
                    }
                }
            }
        }
Example #5
0
 public IDbTransaction BeginTransaction()
 {
     if (Transaction == null)
         Transaction = _context.BeginTransaction();
     return Transaction;
 }
Example #6
0
 public Task <IDbContextTransaction> BeginTransaction(IsolationLevel isolationLevel)
 {
     return(_databaseContext.BeginTransaction(isolationLevel));
 }